-
Notifications
You must be signed in to change notification settings - Fork 7
/
bashrc
337 lines (284 loc) · 7.72 KB
/
bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env bash
#
# Best bashrc in history
#
# Author: Dave Eddy <dave@daveeddy.com>
# Date: Sometime in 2011
# License: MIT
# If not running interactively, don't do anything
[[ -n $PS1 ]] || return
# Load bics, plugins found in bics-plugins
. ~/.bics/bics || echo '> failed to load bics' >&2
# Set environment
export EDITOR='vim'
export GREP_COLOR='1;36'
export HISTCONTROL='ignoredups'
export HISTSIZE=5000
export HISTFILESIZE=5000
export LSCOLORS='ExGxbEaECxxEhEhBaDaCaD'
export PAGER='less'
export TZ='America/New_York'
export VISUAL='vim'
# Support colors in less
export LESS_TERMCAP_mb=$(tput bold; tput setaf 1)
export LESS_TERMCAP_md=$(tput bold; tput setaf 1)
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_se=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4)
export LESS_TERMCAP_ue=$(tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 2)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
export LESS_TERMCAP_ZN=$(tput ssubm)
export LESS_TERMCAP_ZV=$(tput rsubm)
export LESS_TERMCAP_ZO=$(tput ssupm)
export LESS_TERMCAP_ZW=$(tput rsupm)
# PATH
path_add ~/bin before
# Shell Options
shopt -s cdspell
shopt -s checkwinsize
shopt -s extglob
# Bash Version >= 4
shopt -s autocd 2>/dev/null || true
shopt -s dirspell 2>/dev/null || true
# Aliases
alias ..='echo "cd .."; cd ..'
alias ag='rg' # sorry silver searcher
alias chomd='chmod'
alias externalip='curl -sS https://www.daveeddy.com/ip'
alias gerp='grep'
alias hl='rg --passthru'
alias suod='sudo'
grep --color=auto < /dev/null &>/dev/null && alias grep='grep --color=auto'
xdg-open --version &>/dev/null && alias open='xdg-open'
# Enable color support of ls
if ls --color=auto &>/dev/null; then
alias ls='ls -p --color=auto'
else
alias ls='ls -p -G'
fi
# Git Aliases
alias nb='git checkout -b "$USER-$(date +%s)"' # new branch
alias ga='git add . --all'
alias gb='git branch'
alias gc='git clone'
alias gci='git commit -a'
alias gco='git checkout'
alias gd="git diff ':!*lock'"
alias gdf='git diff' # git diff (full)
alias gi='git init'
alias gl='git log'
alias gp='git push origin HEAD'
alias gr='git rev-parse --show-toplevel' # git root
alias gs='git status'
alias gt='git tag'
alias gu='git pull' # gu = git update
# because `master` is sometimes `main` (or others), these must be functions.
gmb() { # git main branch
local main
main=$(git symbolic-ref --short refs/remotes/origin/HEAD)
main=${main#origin/}
[[ -n $main ]] || return 1
echo "$main"
}
# show the diff from inside a branch to the main branch
gbd() { # git branch diff
local mb=$(gmb) || return 1
git diff "$mb..HEAD"
}
# checkout the main branch and update it
gcm() { # git checkout $main
local mb=$(gmb) || return 1
git checkout "$mb" && git pull
}
# merge the main branch into our branch
gmm() { # git merge $main
local mb=$(gmb) || return 1
git merge "$mb"
}
# Prompt
# Store `tput` colors for future use to reduce fork+exec
# the array will be 0-255 for colors, 256 will be sgr0
# and 257 will be bold
COLOR256=()
COLOR256[0]=$(tput setaf 1)
COLOR256[256]=$(tput sgr0)
COLOR256[257]=$(tput bold)
# Colors for use in PS1 that may or may not change when
# set_prompt_colors is run
PROMPT_COLORS=()
# Change the prompt colors to a theme, themes are 0-29
set_prompt_colors() {
local h=${1:-0}
local color=
local i=0
local j=0
for i in {22..231}; do
((i % 30 == h)) || continue
color=${COLOR256[$i]}
# cache the tput colors
if [[ -z $color ]]; then
COLOR256[$i]=$(tput setaf "$i")
color=${COLOR256[$i]}
fi
PROMPT_COLORS[$j]=$color
((j++))
done
}
# Construct the prompt
# [(exit code)] <user> - <hostname> <uname> <cwd> [git branch] <$|#>
# exit code of last process
PS1='$(ret=$?;(($ret!=0)) && echo "\[${COLOR256[0]}\]($ret) \[${COLOR256[256]}\]")'
# username (red for root)
PS1+='\[${PROMPT_COLORS[0]}\]\[${COLOR256[257]}\]$(((UID==0)) && echo "\[${COLOR256[0]}\]")\u\[${COLOR256[256]}\] - '
# zonename (global zone warning)
PS1+='\[${COLOR256[0]}\]\[${COLOR256[257]}\]'"$(zonename 2>/dev/null | grep -q '^global$' && echo 'GZ:')"'\[${COLOR256[256]}\]'
# hostname
PS1+='\[${PROMPT_COLORS[3]}\]\h '
# uname
PS1+='\[${PROMPT_COLORS[2]}\]'"$(uname | tr '[:upper:]' '[:lower:]')"' '
# cwd
PS1+='\[${PROMPT_COLORS[5]}\]\w '
# optional git branch
PS1+='$(branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); [[ -n $branch ]] && echo "\[${PROMPT_COLORS[2]}\](\[${PROMPT_COLORS[3]}\]git:$branch\[${PROMPT_COLORS[2]}\]) ")'
# prompt character
PS1+='\[${PROMPT_COLORS[0]}\]\$\[${COLOR256[256]}\] '
# set the theme
set_prompt_colors 24
# Prompt command
_prompt_command() {
local user=$USER
local host=${HOSTNAME%%.*}
local pwd=${PWD/#$HOME/\~}
local ssh=
[[ -n $SSH_CLIENT ]] && ssh='[ssh] '
printf "\033]0;%s%s@%s:%s\007" "$ssh" "$user" "$host" "$pwd"
}
PROMPT_COMMAND=_prompt_command
PROMPT_DIRTRIM=6
# print a colorized diff
colordiff() {
local red=$(tput setaf 1 2>/dev/null)
local green=$(tput setaf 2 2>/dev/null)
local cyan=$(tput setaf 6 2>/dev/null)
local reset=$(tput sgr0 2>/dev/null)
diff -u "$@" | awk "
/^\-/ {
printf(\"%s\", \"$red\");
}
/^\+/ {
printf(\"%s\", \"$green\");
}
/^@/ {
printf(\"%s\", \"$cyan\");
}
{
print \$0 \"$reset\";
}"
return "${PIPESTATUS[0]}"
}
# Print all supported colors with raw ansi escape codes
colors() {
local i
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolor %d\n" "$i"
done
tput sgr0
}
# Copy stdin to the clipboard
copy() {
pbcopy 2>/dev/null ||
xsel 2>/dev/null ||
clip.exe
}
# Convert epoch to human readable
epoch() {
local num=${1//[^0-9]/}
(( ${#num} < 13 )) && num=${num}000
node -pe "new Date(+process.argv[1]);" "$num"
}
# open the current path or file in GitHub
gho() {
local file=$1
local remote=${2:-origin}
# get the git root dir, branch, and remote URL
local gr=$(git rev-parse --show-toplevel)
local branch=$(git rev-parse --abbrev-ref HEAD)
local url=$(git config --get "remote.$remote.url")
[[ -n $gr && -n $branch && -n $remote ]] || return 1
# construct the path
local path=${PWD/#$gr/}
[[ -n $file ]] && path+=/$file
# extract the username and repo name
local a
IFS=:/ read -a a <<< "$url"
local len=${#a[@]}
local user=${a[len-2]}
local repo=${a[len-1]%.git}
local url="https://github.com/$user/$repo/tree/$branch$path"
echo "$url"
open "$url"
}
# Platform-independent interfaces
interfaces() {
node <<-EOF
var os = require('os');
var i = os.networkInterfaces();
Object.keys(i).forEach(function(name) {
i[name].forEach(function(int) {
if (int.family === 'IPv4') {
console.log('%s: %s', name, int.address);
}
});
});
EOF
}
# Calculate CPU load / Core Count
load() {
node -p <<-EOF
var os = require('os');
var c = os.cpus().length;
os.loadavg().map(function(l) {
return (l/c).toFixed(2);
}).join(' ');
EOF
}
# Platform-independent memory usage
meminfo() {
node <<-EOF
var os = require('os');
var free = os.freemem();
var total = os.totalmem();
var used = total - free;
console.log('memory: %dmb / %dmb (%d%%)',
Math.round(used / 1024 / 1024),
Math.round(total / 1024 / 1024),
Math.round(used * 100 / total));
EOF
}
# print lines over X columns (defaults to 80)
over() {
awk -v c="${1:-80}" 'length($0) > c {
printf("%4d %s\n", NR, $0);
}'
}
# Follow redirects to untiny a tiny url
untiny() {
local location=$1 last_location=
while [[ -n $location ]]; do
[[ -n $last_location ]] && echo " -> $last_location"
last_location=$location
read -r _ location < \
<(curl -sI "$location" | grep 'Location: ' | tr -d '[:cntrl:]')
done
echo "$last_location"
}
# Load external files
. ~/.bash_aliases 2>/dev/null || true
. ~/.bashrc.local 2>/dev/null || true
# load completion
. /etc/bash/bash_completion 2>/dev/null ||
. ~/.bash_completion 2>/dev/null
path_clean
true