-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc
454 lines (354 loc) · 11.4 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# .bashrc, run by every non-login bash
# aliases and transient shell options that are not inherited by child processes
## Shell Options (See man bash) #############################################
# Don't wait for job termination notification
set -o notify
# Don't use ^D to exit
set -o ignoreeof
# Make bash append rather than overwrite the history on disk
shopt -s histappend
# Check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# save multi-line commands as a single line in the history.
shopt -s cmdhist
# and use newlines to separate rather than semi-colons
shopt -s lithist
# don't autocomplete if command-line is empty
shopt -s no_empty_cmd_completion
## utility functions for this script
error() (
IFS=' '
awk -v msg="$*" 'BEGIN { print "Error: " msg > "/dev/stderr" }'
)
## Readline and key binds ####################################################
if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then
# If there are multiple matches for completion,
# Tab should cycle through them
bind 'TAB':menu-complete
# Display a list of the matching files
bind "set show-all-if-ambiguous on"
# Perform partial completion on the first Tab press,
# only start cycling full results on the second Tab press
bind "set menu-complete-display-prefix on"
fi
## Interactive shell tweaks ################################################
case $- in
# interactive shell
*i*)
# PS1 related env-vars (other env vars are in .profile)
pre='\[\e['
post='m\]'
green="${pre}32${post}"
magenta="${pre}95${post}"
dim_cyan="${pre}96${post}"
bright_yellow_inverse="${pre}93;1;7${post}"
red_inverse="${pre}97;41;01${post}"
reset="${pre}0${post}"
pwd="$dim_cyan\w$reset"
prompt="$bright_yellow_inverse\$$reset"
if [[ "$USER" =~ ^(jhartley|jonathan)$ ]]; then
usercol="${green}"
else
usercol="${magenta}"
fi
user="${usercol}${USER}${reset}"
host="${HOSTNAME%%.*}"
if [[ "$host" =~ ^(asus|gazelle|t460|x1)$ ]]; then
hostcol="${green}"
else
hostcol="${magenta}"
fi
host="${hostcol}${host}${reset}"
. ~/.ps1_vcs
# Whenever displaying the prompt, append history to disk
PROMPT_COMMAND='history -a'
# To have all terminals sync their history after every command
# PROMPT_COMMAND='history -a; history -n'
# if exit value isn't zero, display it with red background, and a bell
PROMPT_COMMAND='exitval=$?; '$PROMPT_COMMAND
get_exitval="\$(if [[ \$exitval != 0 ]]; then echo \"${red_inverse} \$exitval ${reset}\"; fi)"
export PS1="$get_exitval\n${user}@${host} $pwd\$(${dvcs_function})\n$prompt "
unset pre post green magenta dim_cyan bright_yellow_inverse reset pwd prompt
# turn off flow control, mapped to ctrl-s, so that we regain use of that key
# for searching command line history forwards (opposite of ctrl-r)
stty -ixon
;;
# non-interactive shell
*)
;;
esac
# Set dircols if terminal can handle it
if [ "$TERM" != "dumb" ]; then
eval `dircolors -b $HOME/.dircolors`
fi
## options to coreutils #####################################################
# Requires latest gnu coreutils, maybe don't work with Mac's old built-in ones
alias cd-='cd -'
alias cd..='cd ..'
alias cp='cp -i'
alias df='df -h'
alias du='du -h'
alias histread='history -c; history -r'
alias less='less -R' # display raw control characters for colors only
alias ls='LC_COLLATE="C" ls $LS_OPTIONS'
command -v eza >/dev/null && \
alias ll='eza --long --git --no-quotes --color-scale=age' || \
alias ll='ls -lGh'
alias la='ll -A'
alias mv='mv -i'
alias rm='rm -i'
alias ssh='TERM=xterm-color ssh'
alias timee='/usr/bin/time -f %E'
## Other aliases #########################################
alias whence='type -a' # like where, but also describes aliases and functions
# if colordiff is installed, use it
if type colordiff &>/dev/null ; then
alias diff=colordiff
fi
## Functions ##############################################
# TODO: maybe all these should only be defined if it is an interactive shell?
# Maybe lots of things in this file are like that?
beep() {
paplay /usr/share/sounds/sound-icons/xylofon.wav &
}
# Generate n busyloops to keep n CPUs busy.
# See also 'killalljobs'
busyloop() {
# Usage: busyloop N
# Where N is number of parallel busy processes to start.
# I forget why we loop over args rather than just using the first.
number=1 # Default to creating 1 busyloop process.
while [ $# -gt 0 ]; do
case "$1" in
*) number=$1
esac
shift
done
# validate args
re='^[0-9]+$'
if ! [[ $number =~ $re ]] ; then
error "Not a number" >&2
return 1
fi
# Start $number busyloops
for (( job=0 ; job<$number; job++ ));
do
while true; do
:
done &
done
}
# See 'bzr functions', below.
# cd into a directory, resolving any symlinks to give the full actual directory name
cdr() {
if [ -n "$1" ]; then in="$1"; else in="."; fi
cd $(readlink -e "$in")
}
colout_traceroute() {
colout '(^ ?\d+)|(\([\d\.]+\))|([\d\.]+ ms)|(!\S+)' white,cyan,yellow,magenta bold,normal,normal,reverse
}
# docker ps
dps() {
docker container ls --format 'table {{.Names}}\t{{.Image}}\t{{.ID}}\t{{.Status}}' "$@" | colout '(^NAMES .+$)|(.+Up .+)|(.+Exited .+)' white,green,red bold,normal
}
etime() {
/usr/bin/time -f"%E" "$@"
}
# See 'git functions', below.
# This is good at cleaning up the results of 'busyloop'
killalljobs() {
# TODO: There is probably a simpler version of this using 'jobs -p' (output just the PID)
for jid in $(jobs | grep '\[' | cut -d']' -f1 | cut -c2-); do
kill %$jid
done
}
# previously known as lxc-ll
lll() {
lxc list -cns4 "$@" | grep '\w' | tr -d '|' | colout '(^\s+NAME\s.+)|(RUNNING)|(STOPPED)' white,green,red bold,normal
}
nh() {
nohup "$@" 1>/dev/null 2>&1 &
}
# Parent of given PID, or else of current shell
ppid() {
ps -p ${1:-$$} -o ppid=
}
# show man pages rendered using postscript
psman() {
SLUG=$(echo $@ | tr ' ' '-')
FNAME="/tmp/man-$SLUG.pdf"
set -o pipefail
man -t "$@" | ps2pdf - "$FNAME" && \
nohup evince "$FNAME" >/dev/null 2>/dev/null
set +o pipefail
}
# Use 'pytags $(pydirs) .' to tag with all stdlib and venv symbols
pydirs() {
python -c "import os, sys; print(' '.join(os.path.relpath(d) for d in sys.path if d))"
}
pytree() {
tree -AC -I '*.pyc|__pycache__' "$@"
}
pywait() {
find -name 'env' -prune -o -name '*.py' -print | entr "$@"
}
# call given command every second until a key is pressed
repeat_until_key() {
command="$@"
while true; do
$command
read -s -n1 -t1 && break
done
}
# Set the terminal window name
termname() {
# Use window title specified by caller, with a fallback if they didn't specify.
if [ "$#" -ne 0 ]; then
name="$@"
else
name="$USER@$(hostname)"
fi
printf "\e]0;${name}\a"
}
# Set our terminal window name, but only for interactive sessions.
# Printing to stdout in non-interactive sessions breaks scp copies to this host.
[[ $- == *i* ]] && termname
trash() {
destdir="$HOME/docs/trash/$(date --iso)"
mkdir -p "$destdir"
exitval=0
for src; do
if [ -e "$src" ]; then
dest="$destdir/$(basename "$src").$(date +%H%M%S.%N)"
printf "$src -> $(echo "$dest" | humanize)\n" >&2
mv "$src" "$dest"
else
printf "Not found: $src\n" >&2
exitval=1
fi
done
return $exitval
}
# -- virtualenvs
ve_root="$HOME/.virtualenvs"
ve() {
if [ "$#" -eq 0 ]; then
ls -1 "$ve_root"
return 0
elif [ "$#" -ne 1 ]; then
error "ve: Error: more than one virtualenv name given."
error "USAGE: ve [virtualenv]"
error "Omit virtualenv to list existing content of ~/.virtualenvs."
return 1
fi
ve="$ve_root/$1"
if [ -e "$ve" ]; then
error "ve: Error: \"$ve\" exists."
return 2
fi
python3 -m venv "$ve"
"$ve/bin/pip" --quiet install --upgrade pip
}
workon() {
if [ "$#" -eq 0 ]; then
ls -1 "$ve_root"
return 0
elif [ "$#" -ne 1 ]; then
error "workon: Error: more than one virtualenv name given."
error "USAGE: workon [virtualenv]"
error "Omit virtualenv to list existing content of ~/.virtualenvs."
return 1
fi
if type deactivate &>/dev/null; then
deactivate
fi
source "$ve_root/$1/bin/activate"
if [ -d "$HOME/$1" ]; then
cdr "$HOME/$1"
fi
}
# --
# Allows use of 'watch' with aliases or functions
watcha() {
watch -ctn1 "bash -i -c \"$@\""
}
## bzr functions ####
alias bs='bzr status && bzr show-pipeline'
alias bt='bzr log -r-1' # tip
bl() {
bzr log "$@" | less
}
blp() {
bzr log -v -p "$@" | colordiff | colout -- '^.{7}(-{5,})' cyan reverse | less
}
bup() {
bzr unshelve --preview "$@" | colordiff
}
## Tool setup #################################################################
## FZF
hash fzf 2>/dev/null && eval "$(fzf --bash)"
# Solarized colors
export FZF_DEFAULT_OPTS='
--color bg+:#073642,bg:#002b36,spinner:#719e07,hl:#618e04
--color fg:#839496,header:#586e75,info:#000000,pointer:#719e07
--color marker:#719e07,fg+:#839496,prompt:#719e07,hl+:#719e17
'
# Bash Completion features
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# stderred wraps stderr of processes in your terminal with a color
stderred_so="$HOME/.local/share/stderred/libstderred.so"
if [ -f ${stderred_so} ]; then
export LD_PRELOAD="${stderred_so}${LD_PRELOAD:+:$LD_PRELOAD}"
export STDERRED_ESC_CODE=$(tput setaf 222)
fi
# direnv (installed using apt) exports env vars from .envrc in PWD or above
# I'm using it to define 'refactor' and 'impl', which are used above.
eval "$(direnv hook bash)"
## On exit ####################################################################
# Backup .bash_history
historyc() {
history "$@" | colout '^ *(\d+) +([0-9-]+ [0-9:]+)' white,cyan bold,normal
}
bash_history_backups=~/docs/config/bash_history
history_backup() {
(
if [ ! -d "$bash_history_backups" ] ; then
error "bash history save directory not found: '$bash_history_backups'"
# Normally we'd return 1 here but it doesn't help in this case,
# since we're executing on shell exit.
fi
# make a backup, overwriting other backups from today
(
cd "$bash_history_backups"
\cp ~/.bash_history bash_history_$(date +%F)
# rm backups older than N days
ls -1 . | head -n -100 | xargs rm -f
)
)
}
if [ ! -d "$bash_history_backups" ]; then
error "bash history save directory not found: '$bash_history_backups'"
else
trap history_backup EXIT
fi
dedupe_history() {
# Now remove duplicate lines from history file
dedupe-bash-history >/tmp/bash_history
mv /tmp/bash_history ~/.bash_history
# (previous solutions, using 'tac', to keep the most recent duplicate,
# then filtering using line-based tools like awk, don't work with
# history files containing timestamps or multi-line commands)
# tac < ~/.bash_history | awk '!a[$0]++' | tac >/tmp/deduped \
# && mv -f /tmp/deduped ~/.bash_history
}
## Source all ~/.bashrc.* files. ############################################
for fname in $(ls ~/.bashrc.*); do
. $fname
done