-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_aliases
498 lines (419 loc) · 11.6 KB
/
.bash_aliases
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
alias g='git'
# Copy to/paste from clipboard
alias getclip='xclip -selection clipboard -o'
alias setclip='xclip -selection clipboard'
# Run docker container with files mounted in /host (*d*ocker *r*un *m*ount)
drm()
{
docker run --rm -it -v "$PWD":/host -w /host $@
}
# Remove and watch logfile
rmtail()
{
rm $1 && touch $1 && tail -f $1
}
# Used to access git completion functions from within this script
if [ -f /usr/share/bash-completion/completions/git ]; then
source /usr/share/bash-completion/completions/git
fi
# Make completion work with git alias
# Source: https://stackoverflow.com/questions/39506941/alias-g-git-and-have-bash-completion-still-work
__git_complete g _git
# Checks whether a function exists
function_exists()
{
declare -f -F $1 >/dev/null
return $?
}
# Add all defined git aliases as bash aliases, e.g. git co will become gco.
# Source: https://gist.github.com/EQuimper/d875df92ef0ddaabf00636c90dbc9d25
if function_exists __git_aliases; then
aliases=$(__git_aliases)
else
aliases=$(git --list-cmds=alias)
fi
for al in $aliases; do
alias g$al="git $al"
# Make sure this does not break completion
complete_func=_git_$(__git_aliased_command $al)
function_exists $complete_func && __git_complete g$al $complete_func
done
# gcd: cd relative to the git repository's root
gcd()
{
if ! git rev-parse --show-toplevel >/dev/null 2>/dev/null; then
echo 'Error: Not in a git repository.'
return 1
fi
local toplevel="$(git rev-parse --show-toplevel)"
cd "$toplevel/$1"
}
_gcd()
{
if ! git rev-parse --show-toplevel >/dev/null 2>/dev/null; then
return
fi
local toplevel="$(git rev-parse --show-toplevel)"
cd "$toplevel"
_cd
}
# We use the same options that are used for cd (obtained via complete -p cd).
complete -o nospace -F _gcd gcd
o()
{
if [ "$#" -eq "0" ]; then
command xdg-open . >/dev/null 2>&1
else
command xdg-open "$@" >/dev/null 2>&1
fi
}
v()
{
if command -v nvim >/dev/null 2>&1; then
# neovim version
if command -v nvr >/dev/null 2>&1; then
command nvr -s "$@"
else
echo "nvr is not found. Install via pip3 install neovim-remote."
fi
else
# vim version
num_servers=$(gvim --serverlist | wc -l)
if [ "$num_servers" -eq "0" ]; then
command gvim "$@"
elif [ "$#" -eq "0" ]; then
echo "Vim is already running"
else
command gvim --remote-silent "$@"
fi
fi
}
# pygments
function pygmentize-html()
{
if [[ $# -eq 0 ]]; then
echo "Usage: pygmentize-html <language> [-o <output file>] [option...]"
return 1
fi;
lexer="$1"
shift
pygmentize -l $lexer -f html -O noclasses -O nobackground $@
}
function pygmentize-svg()
{
if [[ $# -eq 0 ]]; then
echo "Usage: pygmentize-svg <language> [-o <output file>] [option...]"
return 1
fi;
lexer="$1"
shift
pygmentize -l $lexer -f svg $@
}
# Prompt
# Based on Ubuntu default ~/.bashrc
red='\[\033[01;31m\]'
green='\[\033[01;32m\]'
yellow='\[\033[01;33m\]'
blue='\[\033[01;34m\]'
nc='\[\033[00m\]'
# Print return code of last command if non-zero
PS1="${red}\$(retval="\$?" ; if [[ \$retval -ne 0 ]]; then echo \"[\${retval}] \"; fi)${nc}"
# user@host:~/directory (master)$ |
PS1="${PS1}${green}\u@\h${nc}:${blue}\w${yellow}\$(__git_ps1)${nc}\$ "
# If this is an xterm set the title
# Based on Ubuntu default ~/.bashrc
title_begin="\[\e]0;"
title_end="\a\]"
case "$TERM" in
xterm*|rxvt*)
PS1="${title_begin}\u@\h: \w${title_end}${PS1}"
;;
*)
;;
esac
# Print LLVM bitcode for CUDA
alias cuda-llvm='clang++ --cuda-gpu-arch=sm_70 --cuda-device-only -emit-llvm -S'
# Julia
alias jl='julia'
# Python
alias py='python'
# Tail follow entire file. (tail follow all)
alias tfa='tail --lines=+1 --follow=name --retry'
# Utility functions to manage symlinks in ~/bin.
# Garbage collection: remove dangling
bin-gc()
{
usage()
{
cat <<EOF >&2
Usage: bin-gc [OPTIONS]
Remove dangling symlinks from ~/bin.
Options:
-h, --help Show this help.
-n, --dry-run Only show what would be removed, but do not actually remove anything.
-f, --force bin-gc will refuse to remove anything unless this option is given.
EOF
}
local dry_run=false
local force=false
local positional=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage; return 0
;;
-n|--dry-run)
dry_run=true
shift
;;
-f|--force)
force=true
shift
;;
-*)
echo "Unknown command-line option '$1'."
echo "Try 'bin-gc --help' for more information."
return 1
;;
*)
positional+=("$1")
shift
;;
esac
done
set -- "${positional[@]}"
if [[ $# -ne 0 ]]; then
echo "Expected 0 positional arguments, but got $#."
echo "Try 'bin-gc --help' for more information."
return 1
fi
if [[ "$dry_run" = true ]]; then
find ~/bin -maxdepth 1 -xtype l
return 0
elif [[ "$force" = true ]]; then
find ~/bin -maxdepth 1 -xtype l -delete
return 0
else
usage; return 1
fi
}
# Rm: remove symlinks
bin-rm()
{
usage()
{
cat <<EOF >&2
Usage: bin-rm [OPTIONS] <DIRECTORY>
Remove all symlinks from ~/bin that point to the given DIRECTORY.
Options:
-h, --help Show this help.
-n, --dry-run Only show what would be removed, but do not actually remove anything.
EOF
}
local dry_run=false
local positional=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage; return 0
;;
-n|--dry-run)
dry_run=true
shift
;;
-*)
echo "Unknown command-line option '$1'."
echo "Try 'bin-rm --help' for more information."
return 1
;;
*)
positional+=("$1")
shift
;;
esac
done
set -- "${positional[@]}"
if [[ $# -ne 1 ]]; then
echo "Expected 1 positional argument, but got $#."
echo "Try 'bin-rm --help' for more information."
return 1
fi
local directory="$(readlink -f "$1")"
if [[ "$dry_run" = true ]]; then
find ~/bin -maxdepth 1 -type l -print0 |
while IFS= read -r -d '' f; do
if [[ "$(readlink -f $f)" == ${directory}/* ]]; then
echo $f
fi
done
return 0
else
find ~/bin -maxdepth 1 -type l -print0 |
while IFS= read -r -d '' f; do
if [[ "$(readlink -f $f)" == ${directory}/* ]]; then
rm $f
fi
done
return 0
fi
}
# Add: add symlinks
bin-add()
{
usage()
{
cat <<EOF >&2
Usage: bin-add [OPTIONS] <PATH...>
Add symlinks to ~/bin.
Options:
-h, --help Show this help.
-n, --dry-run Only show what would be added, but do not actually add anything.
-p, --prefix PREFIX Prefix to add before every symlink's name (default is empty string).
-s, --suffix SUFFIX Suffix to add after every symlink's name (default is empty string).
EOF
}
local dry_run=false
local prefix=""
local suffix=""
local positional=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage; return 0
;;
-n|--dry-run)
dry_run=true
shift
;;
-p|--prefix)
prefix=$2
shift; shift
;;
-s|--suffix)
suffix=$2
shift; shift
;;
-*)
echo "Unknown command-line option '$1'."
echo "Try 'bin-add --help' for more information."
return 1
;;
*)
positional+=("$1")
shift
;;
esac
done
set -- "${positional[@]}"
if [[ $# -eq 0 ]]; then
echo "Expected at least one positional argument, but got $#."
echo "Try 'bin-add --help' for more information."
return 1
fi
if [[ "$dry_run" = true ]]; then
for path in "$@"; do
find "$path" -type f -executable
done
else
for path in "$@"; do
find "$path" -type f -executable -exec sh -c "ln -rs \$(readlink -f {}) ~/bin/${prefix}\$(basename {})${suffix}" \;
done
fi
}
# Ls: list symlinks
bin-ls()
{
usage()
{
cat <<EOF >&2
Usage: bin-ls [OPTIONS]
List symlinks in ~/bin.
Options:
-h, --help Show this help.
EOF
}
positional=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage; return 0
;;
-*)
echo "Unknown command-line option '$1'."
echo "Try 'bin-ls --help' for more information."
return 1
;;
*)
positional+=("$1")
shift
;;
esac
done
set -- "${positional[@]}"
if [[ $# -ne 0 ]]; then
echo "Expected 0 positional arguments, but got $#."
echo "Try 'bin-ls --help' for more information."
return 1
fi
local bold_cyan="\033[1;36m"
local bold_green="\033[1;32m"
local nc="\033[0m"
local filename_width=40
find ~/bin -maxdepth 1 -type l -printf "${bold_cyan}%-${filename_width}P${nc} -> ${bold_green}%l\n" | sort -k 3 | less -R
}
# direnv
if command -v direnv >/dev/null 2>&1; then
eval "$(direnv hook bash)"
fi
# preserve newlines in history, instead of adding semicolons
shopt -s cmdhist lithist
vim()
{
# use neovim instead of vim (if it's installed)
if command -v nvim >/dev/null 2>&1; then
command nvim $@
else
command vim $@
fi
}
# convert SVG link to rasterised graphics (e.g. to include in Google Slides)
iconify()
{
# Read URL from argument or from clipboard.
if [[ $# -eq 0 ]]; then
URL="$(xclip -selection clipboard -o)"
else
URL="$1"
fi
# If we haven't provided a URL, regard argument as iconify ID.
if [[ $URL != http?(s)://* ]]; then
URL="${URL/:/\/}"
URL="https://api.iconify.design/$URL.svg?download=1"
fi
wget -O /tmp/img.svg "$URL"
inkscape /tmp/img.svg --export-type=png --export-filename=/tmp/img.png -w 500
xclip -selection clipboard -t image/png /tmp/img.png
notify-send "Icon available in clipboard."
}
# Eternal bash history (see https://stackoverflow.com/questions/9457233/unlimited-bash-history).
# Undocumented feature which sets the size to "unlimited".
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
# Append to history; do not overwrite it.
shopt -s histappend
# Use zoxide as cd
if command -v zoxide >/dev/null 2>&1; then
eval "$(zoxide init --cmd cd bash)"
fi
# ripgrep + delta
rgd()
{
rg --json "$@" | delta
}