-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path.bashrc
610 lines (539 loc) · 16.5 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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# Define function for creating directory aliases. This is defined before exiting
# for non-interactive sessions since the function may be called from ~/.bashrc.
function aliasDir {
# When multiple directories are given, create alias for first that exists.
for dir in "${@:2}"; do
if [[ -d "$dir" ]]; then
alias $1="c '$dir'"
return
fi
done
}
# Exit if not interactive, such as when transfering files via scp.
if [[ $- != *i* ]]; then
return
fi
# Set default editor. On most platforms, Vi and Vim both refer to the same
# editor. When using Ctrl-X Ctrl-E, Zsh's edit-command-line tries to advance the
# cursor to the same position as it was in terminal, which is not the behavior
# that we want since Bash doesn't do that. Per https://bit.ly/2PZ3iVn, Zsh does
# this with Vim but not Vi.
export EDITOR=vi
# Define alias for changing to the dotfiles directory. The logic for getting the
# current file path comes from https://bit.ly/33OR2Lh. This works with both Bash
# and Zsh.
dotDir=$(dirname "${BASH_SOURCE[0]:-${(%):-%x}}")
aliasDir cdot "$dotDir"
# Set less configuration. A couple arguments are specified using the short form
# rather than long form due to how i3-sensible-pager strips -E and -F
# https://bit.ly/4ekPUmM.
export LESS="-FNRX --chop-long-lines --shift 20 --tabs 4"
export LESSKEYIN=$dotDir/.lesskeyin
# Load shell specific code.
if [[ $SHELL == "/bin/bash" ]]; then
source "$dotDir/shell/bash.sh"
elif [[ $SHELL == "/bin/zsh" ]]; then
source "$dotDir/shell/zsh.sh"
fi
# Python pip can install packages without root into ~/.local/bin on Linux and
# ~/Library/Python/*/bin on macOS. If directory exists, add to $PATH.
pythonBins=(~/.local/bin ~/Library/Python/*/bin)
for dir in "${pythonBins[@]}"; do
if [[ -d $dir ]]; then
export PATH=$dir:$PATH
fi
done
# Define aliases for checksums on macOS to match the commands on Linux.
uname=$(uname)
if [[ $uname == "Darwin" ]]; then
alias md5sum="md5"
alias sha1sum="shasum"
alias sha224sum="shasum -a 224"
alias sha256sum="shasum -a 256"
alias sha384sum="shasum -a 384"
alias sha512sum="shasum -a 512"
fi
# Define function to prevent piping to Vim or piping Vim to somewhere. This can
# happen accidentally when working with large amounts of text and using Vim as a
# pager. This comes from https://bit.ly/3FG7m65.
function vi {
if [[ ! -t 1 ]]; then
echo "Vim must run with TTY as standard output" >&2
elif [[ ! -t 0 ]]; then
echo "Vim must run with TTY as standard input"
else
command vi "$@"
fi
}
# Define function for piping to Vim.
function vid {
if [[ ! -t 1 ]]; then
echo "Vim must run with TTY as standard output" >&2
elif [[ -t 0 ]]; then
echo "Vim expecting pipe as standard input"
else
command vi -
fi
}
# Define ping function wrapper to lookup SSH host before pinging.
function ping {
# Look up host using "ssh" command with the last argument being the host.
addr="${@: -1}"
addr=$(ssh -G "$addr" | grep "^hostname " | awk '{print $2}')
# Call "ping" with the lookup result replacing the last argument.
args=("$@")
args[-1]="$addr"
command ping "${args[@]}"
}
# The ls command is different on Linux and macOS. Set color scheme for macOS
# per https://goo.gl/1ps44T.
if [[ $uname == "Darwin" ]]; then
export CLICOLOR=1
export LSCOLORS=ExGxBxDxCxEgEdxbxgxcxd
alias l="ls"
else
alias l="LC_ALL=C.UTF-8 ls --group-directories-first --color=auto"
fi
# Set environment variables for using Vim as man pager.
if command -v man > /dev/null; then
export MANWIDTH=80
if [[ $uname != "Darwin" ]]; then
export MANOPT="--no-hyphenation --no-justification"
fi
export MANPAGER="sh -c 'col -bx | vi - -c set\ filetype=man'"
fi
# Define alias for df. Display human-readable size and show filesystem type.
if [[ $uname == "Darwin" ]]; then
alias df="df -h -Y"
else
alias df="df --human-readable --print-type"
fi
# Define alias for ps.
if [[ $uname == "Darwin" ]]; then
alias p="ps aux"
else
alias p="ps auxf"
fi
alias pg="ps aux | ep"
alias pim="p | vid"
# Define aliases for top.
if [[ $uname == "Darwin" ]]; then
alias tom="top -o mem"
else
alias top="top -E m -e m -o %CPU"
alias tom="top -E m -e m -o %MEM"
fi
alias autk="vi ~/.ssh/authorized_keys"
alias c="cd"
alias cm="c - > /dev/null"
alias crt="crontab -e"
alias di="date -Iseconds"
alias ep="grep --color"
alias epi="ep -i"
alias epr="ep -Rn"
alias epri="epr -i"
alias eprtt="epr 'TODO TODO'"
alias fep="find . | ep"
alias fm="free -m"
alias fmd="python3 $dotDir/python/format_md.py"
alias fms="fm -s 1"
alias hig="history | ep"
alias ig="python3 $dotDir/python/gen_index.py"
alias phttp="python3 -m http.server"
alias pwdc="pwd | xc"
alias tf="tail -f"
alias tm="touch -m"
alias usm="useradd -s /bin/bash -m"
alias vidi="vimdiff"
alias vie="vi -c Explore"
alias vrc="vi .vimrc"
alias wl="wc -l"
alias xc="bash $dotDir/clipboard/copy.sh"
# The "reset" Bash command takes a full second to run. Use "tput reset" to
# achieve the same functionality without the delay. See https://bit.ly/3An1bo2
# for details.
alias r="tput reset"
# Define realpath function for converting relative path to absolute path. The
# command exists natively in Linux but not macOS.
if [[ $uname == "Darwin" ]]; then
function realpath {
echo $(cd $(dirname "$1") && pwd)/$(basename "$1")
}
fi
alias rp="realpath"
function rpc { rp "$@" | xc; }
# Define alias for listing ports that are in use.
if [[ $uname == "Darwin" ]]; then
alias n="lsof -i -n -P -s TCP:LISTEN"
elif command -v netstat > /dev/null; then
alias n="netstat -nlp"
fi
# Define aliases for file listing. When sorting by time, default to newest files
# at the end as they are most likely the relevant ones.
alias ll="l -hlA"
alias ltr="ll -t"
alias lt="ll -rt"
# Define alias for cutting words. Use "tr" to squeeze multiple consecutive
# spaces into one. "cutw n" prints the n-th word on every line. "cutw n-" prints
# every word starting from the n-th word on every line.
alias cutw="tr -s ' ' | cut -d ' ' -f"
# Define aliases for sorting. By default, sort using binary. Sorting with
# English locale is useful when the result should be case insensitive. The
# second alias uses "command" so it doesn't call into the first alias.
alias sort="LC_ALL=C sort"
alias sorten="LC_ALL=en_US.UTF-8 command sort"
# Define alias for xargs. This replaces \n with \x00 so filenames containing
# spaces are handled correctly.
alias xargs="tr '\n' '\0' | xargs -0"
# Use dfmt to format Go code using gofmt. Use wfmt to format it in the working
# directory. Use gfmt to do it across the entire Git repository.
alias dfmt="gofmt -w=true -s"
alias wfmt="dfmt ."
alias gfmt='dfmt $(git rev-parse --show-toplevel)'
# Define alias for insecure SSH. This is useful before we reserve a static IP
# for a new device. Generally we use SSH keys for authentication so there's
# limited security risk of not checking the host key.
alias sins="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
alias sinsr="sins -l root"
# Define aliases for tar. When extracting as root, tar will extract with the
# setuid/setgid attribute by default. This can be a problem when untrusted
# archives are extracted to a location accessible to other users so prevent this
# with --no-same-permissions. As root tar also defaults to maintaining file
# ownership when extracting. This is disabled with --no-same-owner.
alias tarcf="tar --create --file"
alias tarzcf="tar --gzip --create --file"
alias tarxf="tar --keep-old-files --no-same-permissions --no-same-owner --extract --file"
# Create an alias for cp and mv as to prompt before overwriting existing files.
alias cp="cp -i"
alias mv="mv -i"
# Create function to undo mv. This can be easily used by editing the initial
# "mv" command and adding the "un" prefix. This exists for convenience and
# doesn't handle many cases such as moving multiple files.
function unmv {
# If destination is a file, swap arguments to undo.
if [[ -f "$2" ]]; then
mv -v "$2" "$1"
return
fi
# If destination is a directory, it's not possible to determine with 100%
# certainty if the previous operation involved renaming or moving. For
# simplicity, assume move if the destination directory contains an entry
# matching the source.
dst="$2/$(basename "$1")"
if [[ -e "$dst" ]]; then
mv -v "$dst" "$(dirname "$1")"
else
mv -v "$2" "$1"
fi
}
# Define function to rename file based on modification time.
function mvmod {
for src in "$@"; do
ext="${src##*.}"
dir=$(dirname "$src")
file=$(date -r "$src" "+%Y-%m-%d %H.%M.%S.$ext")
dst="$dir/$file"
mv -v "$src" "$dst"
done
}
# Define function to delete working directory.
function rmpwd {
# Print directory and number of items in red as an additional precaution.
count=$(find . | wc -l)
echo -e "Directory is \e[1;31m$PWD\e[0m with \e[1;31m$count\e[0m items"
# Require confirmation before deletion.
rm -rfIv $PWD && cd ..
}
# Create aliases for calculating size of directories and files. Use -s so
# subdirectories are not listed individually. The apparent size alias is useful
# for showing the theoretical size of sparse files instead of the size on disk.
# These are defined with short flags as long flags are not supported on macOS.
alias duh="du -sh"
if [[ $uname == "Darwin" ]]; then
alias duha="du -sAh"
else
alias duha="du -sbh"
fi
# Define function for looking up the SSL certificate information for a domain.
function sslinfo {
hostname=$1
port=$2
if [[ ! $port ]]; then
port=443
fi
connect="$hostname:$port"
openssl s_client -connect "$connect" < /dev/null | openssl x509 -noout -text
}
# In WSL2, determine path to native home directory.
if command -v powershell.exe > /dev/null; then
userHome=/mnt/c/Users/$(powershell.exe -Command 'Write-Host -NoNewline $env:USERNAME')
aliasDir cw $userHome
else
userHome=$HOME
fi
# Create aliases for common directories.
aliasDir cde $userHome/Desktop
aliasDir cdl $userHome/Downloads
aliasDir cdoc $userHome/Documents
aliasDir csh ~/.ssh
aliasDir csr ~/src
aliasDir css ~/Documents/screenshot $userHome/Pictures/Screenshots
# If screenshot is organized by month, follow "current" symlink. Otherwise print
# main screenshot directory.
function ssdir {
current=~/Documents/screenshot/current
if [[ -e $current ]]; then
readlink $current
else
css
pwd
cm
fi
}
alias csd='c "$(ssdir)"'
alias lsd="csd && ll"
function viargs {
for file in "$@"; do
if [[ -e "$file" ]]; then
vi "$file"
return
fi
done
echo "unable to find matching file"
}
# Define function to edit various files in the working directory.
alias src="viargs .bashrc .zshrc"
alias vrm="viargs readme.* README.*"
# Use cu to travel up multiple parent directories. If no arguments, go up one
# directory.
function cu {
count="$1"
if [[ ! $count ]]; then
count=1
fi
str=$(seq -f "..%g" -s / $count | sed "s/[0-9]//g")
cd $str
}
# Define xe as an alternative for fc. The name was chosen since Ctrl-X Ctrl-E is
# used for editing the current command. This is preferred over fc as that
# requires two consecutive presses with the same finger.
function xe {
fc
}
# Define xv to rerun the last command and pipe the output to Vim.
function xv {
# Both standard input and standard output must be TTY and not a pipe.
# Otherwise display error. This prevents xv from calling xv recursively.
if [[ -t 1 ]] && [[ -t 0 ]]; then
fc -s | vid
else
echo "xv cannot be used with a pipe"
fi
}
# Use cdn to go to the directory containing a given file. This makes it easy to
# double click a path in grep output (assuming it does not contain spaces) and
# navigate to the folder containing it.
function cdn {
cd $(dirname "$1")
}
# Similar to cdn above, use ndc to do edit the file.
function ndc {
files=()
for file in "$@"; do
file=$(cut -d : -f 1 <<< "$file")
files+=("$file")
done
vi "${files[@]}"
}
function dotcommit { git -C "$dotDir" rev-parse master; }
# Use dotc to copy the dotfiles commit hash. This is useful for pulling changes
# on machines that lack GPG.
alias dotc="dotcommit | xc"
# Use dotC to print and copy the command for setting up dotfiles on a new server
# or user.
function dotC {
cmds=()
if [[ "$1" == "apt" ]]; then
cmds+=("apt update")
cmds+=("apt install -y git vim tmux")
fi
cmds+=("git init dot")
cmds+=("cd dot")
cmds+=("git remote add origin https://github.com/xudongzheng/dotfiles.git")
cmds+=("git fetch origin")
cmds+=("git checkout $(dotcommit)")
cmds+=("bash setup.sh")
cmds+=("exit")
combined=""
for cmd in "${cmds[@]}"; do
if [[ $combined ]]; then
combined+=" && "
fi
combined+="$cmd"
done
echo $combined | xc
}
# Use pub to print Ed25519 public key. It will generate a new key if one does
# not exist.
function pub {
if [[ ! -f ~/.ssh/id_ed25519 ]]; then
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N "" > /dev/null
fi
cat ~/.ssh/id_ed25519.pub
}
# Use pubc to print and copy the local SSH public key.
alias pubc="pub | xc"
# Use pubC to print and copy command for adding the local SSH public key to
# .ssh/authorized_keys.
function pubC {
cmd='mkdir -p ~/.ssh && echo "'$(pub)'" >> ~/.ssh/authorized_keys'
echo $cmd | xc
}
# Use mkc to create and change to a directory. Create parent directories if
# necessary.
function mkc {
mkdir -p "$1" && cd "$1"
}
# Define function to sort directory from smallest to largest.
function lsort {
if [[ $1 ]]; then
dir="$1"
else
dir="."
fi
find "$dir" -mindepth 1 -maxdepth 1 | xargs du -sh | sort -h | column -t -s $'\t'
}
# Use s to run script to avoid accidentally using the incorrect interpreter or
# compiler.
function s {
ext="${1##*.}"
case $ext in
go)
go run "$@"
;;
php)
php "$@"
;;
pl)
perl "$@"
;;
py)
python3 "$@"
;;
sh)
bash "$@"
;;
swift)
swift "$@"
;;
*)
echo "Failed to recognize script extension"
;;
esac
}
# Explicitly set tab width to 8 spaces. While I generally prefer 4 spaces, many
# commands such as "time" and "lsb_release" assume that the tab width is 8.
tabs -8
# Define function for converting spaces to tabs.
function space2tab {
width=$1
if [[ ! $width ]]; then
width=4
fi
perl -pe 's/^( +)/"\t" x (length($1)\/'$width')/e'
}
# Define function to format JSON. Indent with tabs instead of spaces.
function jfmt {
python3 -m json.tool "$@" | space2tab
}
# Define function to format JSON and write output to file.
function jpretty {
for file in "$@"; do
jfmt "$file" > "${file%.json}.pretty.json"
done
}
# Define alias for tmux or GNU Screen.
if command -v tmux > /dev/null; then
alias t="tmux new-session -t 0 || tmux"
elif command -v screen > /dev/null; then
alias t="screen -x || screen"
fi
# If wget is not available but cURL is available such as on macOS, allow cURL to
# be invoked using the wget command.
if ! command -v wget > /dev/null; then
if command -v curl > /dev/null; then
function wget {
# Do not overwrite existing file.
name=$(basename "$1")
if [[ -f "$name" ]]; then
name=$(mktemp "${name}XXXXXX")
fi
# Include -L to follow redirects.
curl -o "$name" -L "$1"
}
fi
fi
function ipa {
if [[ $uname == "Darwin" ]]; then
ifconfig | grep -o "^[a-z0-9]\+:" | sed "s/://" | while read ifce; do
addr=$(ifconfig "$ifce" | grep -w inet | awk '{print $2}')
if [[ $addr ]]; then
echo -e "$ifce\t$addr"
fi
done
else
ip -brief addr | while read line; do
ifce=$(awk '{print $1}' <<< "$line")
addr=$(awk '{print $3}' <<< "$line" | awk -F / '{print $1}')
if [[ $addr ]]; then
echo -e "$ifce\t$addr"
fi
done
fi | column -t
}
function osver {
if [[ $uname == "Darwin" ]]; then
sw_vers
elif command -v lsb_release > /dev/null; then
lsb_release -a
else
cat /etc/debian_version
fi
}
# If apt is available, define related aliases. Some are only necessary of the
# user is root.
if command -v apt > /dev/null; then
# Use aps instead of acs for alias since c and s use the same finger.
alias aps="apt-cache search"
if [[ $USER == "root" ]]; then
alias apar="apt autoremove"
alias apd="apt update"
alias apg="apt upgrade --with-new-pkgs"
alias api="apt install"
alias apr="apt remove"
alias apu="apd && apg"
fi
# Use ali to list all installed packages. This writes standard error of "apt
# list" to /dev/null since it give a warning about not having a stable CLI
# interface when the output goes to a pipe instead of a terminal.
alias ali="apt list --installed 2>/dev/null"
alias aliep="ali | ep"
fi
# Define aliases for Go.
if command -v go > /dev/null; then
alias gob="go build"
alias gog="go generate"
alias got="go test -c"
alias gotn="got -o /dev/null"
fi
if command -v git > /dev/null; then
source "$dotDir/shell/git.sh"
fi
if [[ $uname == "Darwin" ]]; then
firefox=/Applications/Firefox.app/Contents/MacOS/firefox
else
firefox=firefox
fi
if command -v $firefox > /dev/null; then
source "$dotDir/shell/mozilla.sh"
fi