-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
598 lines (500 loc) · 16.1 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
#!/usr/bin/env bash
# BASE CONFIGURATION ##########################################################
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# Used to source various files
source_if_readable() {
# shellcheck disable=1090
[ -f "$1" ] && [ -r "$1" ] && source "$1"
}
# Some work machines have this....
# shellcheck disable=1091
source_if_readable /usr/local/lib/bashrc.source
# Vi mode
set -o vi
bind -m vi-insert "\C-l":clear-screen
# Check window size after each command and update $LINES and $COLUMNS as needed
[ "$DISPLAY" ] && shopt -s checkwinsize
# Recursive **/* and `cd` when only entering a path (does not work on OS X)
if [ "$(uname -s)" != "Darwin" ]; then
shopt -s globstar
shopt -s autocd
fi
# Fix perl complaining about LC_ALL
export LC_ALL=en_US.UTF-8
# Fix spelling
shopt -s cdspell
[ "$(uname -s)" != 'Darwin' ] && shopt -s dirspell
# Fix gpg agent
GPG_TTY="$(tty)"
export GPG_TTY
# Fix ssh agent
# http://rabexc.org/posts/pitfalls-of-ssh-agents
if [ -x "$(command -v ssh-agent)" ]; then
# Test if agent is running
ssh-add -l >/dev/null 2>/dev/null
if [ "$?" -gt 1 ]; then # agent not running
# Test if agent config exists in file
test -r ~/.ssh-agent &&
eval "$(<~/.ssh-agent)" >/dev/null
# Test again if running
ssh-add -l &>/dev/null
if [ "$?" -gt 1 ]; then # agent not running
(
umask 066
ssh-agent -t 600 >~/.ssh-agent
)
eval "$(<~/.ssh-agent)" >/dev/null
fi
fi
fi
# Tabs default to four spaces wide
[ -x "$(command -v tabs)" ] && tabs 4
# BASH PROMPT #################################################################
source_if_readable "$HOME"/.config/gitstatus/gitstatus.plugin.sh
# shellcheck disable=2120
gitstatus_prompt_update() {
GITSTATUS_PROMPT=""
gitstatus_query "$@" || return 1 # error
[[ "$VCS_STATUS_RESULT" == ok-sync ]] || return 0 # not a git repo
local reset=$'\e[0m' # no color
local clean=$'\e[0;36m' # green foreground
local pbranch=$'\e[0;35m' # purple foreground
local otag=$'\e[0;93m' # orange foreground
local untracked=$'\e[0;94m' # blue foreground
local modified=$'\e[0;92m' # yellow foreground
local conflicted=$'\e[0;91m' # red foreground
local p="${pbranch}"
local where # branch name, tag or commit
if [[ -n "$VCS_STATUS_LOCAL_BRANCH" ]]; then
where="$VCS_STATUS_LOCAL_BRANCH"
elif [[ -n "$VCS_STATUS_TAG" ]]; then
p+="${otag}#"
where="$VCS_STATUS_TAG"
else
p+="${otag}@"
where="${VCS_STATUS_COMMIT:0:8}"
fi
((${#where} > 32)) && where="${where:0:12}…${where: -12}" # truncate long branch names and tags
p+="${where}"
# ⇣42 if behind the remote.
((VCS_STATUS_COMMITS_BEHIND)) && p+=" ${clean}⇣${VCS_STATUS_COMMITS_BEHIND}"
# ⇡42 if ahead of the remote; no leading space if also behind the remote: ⇣42⇡42.
((VCS_STATUS_COMMITS_AHEAD && !VCS_STATUS_COMMITS_BEHIND)) && p+=" "
((VCS_STATUS_COMMITS_AHEAD)) && p+="${clean}⇡${VCS_STATUS_COMMITS_AHEAD}"
# *42 if have stashes.
((VCS_STATUS_STASHES)) && p+=" ${clean}*${VCS_STATUS_STASHES}"
# 'merge' if the repo is in an unusual state.
[[ -n "$VCS_STATUS_ACTION" ]] && p+=" ${conflicted}${VCS_STATUS_ACTION}"
# ~42 if have merge conflicts.
((VCS_STATUS_NUM_CONFLICTED)) && p+=" ${conflicted}~${VCS_STATUS_NUM_CONFLICTED}"
# +42 if have staged changes.
((VCS_STATUS_NUM_STAGED)) && p+=" ${modified}+${VCS_STATUS_NUM_STAGED}"
# !42 if have unstaged changes.
((VCS_STATUS_NUM_UNSTAGED)) && p+=" ${modified}!${VCS_STATUS_NUM_UNSTAGED}"
# ?42 if have untracked files. It's really a question mark, your font isn't broken.
((VCS_STATUS_NUM_UNTRACKED)) && p+=" ${untracked}?${VCS_STATUS_NUM_UNTRACKED}"
GITSTATUS_PROMPT="${p}${reset}"
}
# Start gitstatusd in the background.
gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1
__prompt_command() {
# Start with exit status of previous command, and date
PS1="\\n \\D{%a %Y.%m.%d %T} [\\j][\${PIPESTATUS[@]}]\\n"
gitstatus_prompt_update
[ -n "$GITSTATUS_PROMPT" ] && GITSTATUS_PROMPT=" ($GITSTATUS_PROMPT)"
# How many characters of the $PWD should be kept
local pwdmaxlen=$((COLUMNS - 32 - ${#HOSTNAME} - ${#USER} - ${#GITSTATUS_PROMPT}))
local dir=${PWD##*/}
pwdmaxlen=$(((pwdmaxlen < ${#dir}) ? ${#dir} : pwdmaxlen))
CPWD=${PWD/#$HOME/\~}
local pwdoffset=$((${#CPWD} - pwdmaxlen))
if [ ${pwdoffset} -gt "0" ]; then
CPWD=${CPWD:$pwdoffset:$pwdmaxlen}
CPWD='.../'"${CPWD#*/}"
fi
# Colors
local U="\\[\\e[0m\\]" # default foreground color
## regular colors
local R="\\[\\e[0;31m\\]" # red
local G="\\[\\e[0;32m\\]" # green
local Y="\\[\\e[0;33m\\]" # yellow
local B="\\[\\e[0;34m\\]" # blue
local M="\\[\\e[0;35m\\]" # magenta
local C="\\[\\e[0;36m\\]" # cyan
## emphasized (bolded) colors
local ER="\\[\\e[1;31m\\]" # bold red
local UC=$R # user's color
if [ "$USER" == 'stick' ] || [ "$USER" == 'nstickney' ]; then
UC=$Y
elif [ "$USER" == 'emma' ] || [ "$USER" == 'emmafreester' ]; then
UC=$M
elif [ "$(id -u)" -eq '0' ]; then
UC=$ER # root's color
fi
# Next line shows username, hostname, current working directory, git status
PS1+=" ${UC}\\u${U}@${C}\\h${U}:${B}\${CPWD}${U}\${GITSTATUS_PROMPT}\\n"
# Last line shows bash version and prompt level (root vs nonroot)
PS1+="[${G}\\s \\V${U}] ${UC}\\$ ${U}"
}
PROMPT_COMMAND=__prompt_command
# COLORIZE EVERYTHING #########################################################
# the tty/framebuffer console
if [ "$TERM" = 'linux' ]; then
# printf "\\e]P01A1A1A" # black
printf "\\e]P1DF5363" # red (red)
printf "\\e]P29E7E3D" # green (yellow)
printf "\\e]P3EB4E00" # brown (orange)
printf "\\e]P40081FA" # blue (blue)
printf "\\e]P5C75CA3" # magenta (purple)
printf "\\e]P62D956D" # cyan (green)
printf "\\e]P7C7C7C7" # light gray
printf "\\e]P8363636" # gray
printf "\\e]P9EA8B96" # bright red
printf "\\e]PAC2A161" # bright green
printf "\\e]PBFF8142" # yellow
printf "\\e]PC52ABFF" # bright blue
printf "\\e]PDD88DBF" # bright magenta
printf "\\e]PE37B897" # bright cyan
# printf "\\e]PE6E6E6" # white
clear # fix artifacts
fi
# bat
if [ -x "$(command -v bat)" ]; then
alias cat='bat --paging=never'
alias less='bat --paging=auto'
fi
# ls
if [ "$(uname -s)" == 'Darwin' ]; then
# shellcheck disable=2032
alias ls='ls -G'
else
alias ls='ls --color=auto'
fi
if [ -x "$(command -v exa)" ]; then
alias ls='exa --group-directories-first'
alias lg='ls --long --git'
alias ll='lg --all'
fi
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
[ -n "$(command -v dircolors)" ] && eval "$(dircolors -b)"
# grep
export GREP_COLORS='mt=1;33'
if grep --color 'a' <<<'a' &>/dev/null; then
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# diff
[ -x "$(command -v colordiff)" ] && alias diff='colordiff'
# less
if [ -x /usr/bin/source-highlight-esc.sh ]; then
export LESSOPEN="| /usr/bin/source-highlight-esc.sh %s"
export LESS='-R'
fi
# make
[ -x "$(command -v colormake)" ] && alias make='colormake'
# man - colored, and with help
man() {
env \
LESS_TERMCAP_mb="$(printf '\e[1;32m')" \
LESS_TERMCAP_md="$(printf '\e[1;33m')" \
LESS_TERMCAP_me="$(printf '\e[0m')" \
LESS_TERMCAP_se="$(printf '\e[0m')" \
LESS_TERMCAP_so="$(printf '\e[1;45;30m')" \
LESS_TERMCAP_ue="$(printf '\e[0m')" \
LESS_TERMCAP_us="$(printf '\e[0;34m')" \
man "$@" || (help "$@" 2>/dev/null && help "$@" | less)
}
# ping
[ -x "$(command -v prettyping)" ] && alias ping='prettyping'
# COMPLETION ##################################################################
# Bash completion where available
source_if_readable /usr/share/bash-completion/bash_completion
# Fuzzy Finder
source_if_readable /usr/share/fzf/completion.bash
source_if_readable /usr/share/fzf/key-bindings.bash
# Git
source_if_readable /usr/share/git/completion/git-completion.bash
# History search
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\e[5~": previous-history'
bind '"\e[6~": next-history'
# Sudo Completion
[ -n "$(command -v sudo)" ] && complete -cf sudo
# DEFEAT LOGGING ##############################################################
# Defeat Snoopy logging
# http://blog.rchapman.org/posts/Bypassing_snoopy_logging/
# [ ! -f "$HOME"/bin/bypass.so ] &&
# [ -x "$(command -v gcc)" ] &&
# [ -f "$HOME"/bin/bypass.c ] &&
# gcc -nostartfiles -shared -O3 -fPIC "$HOME"/bin/bypass.c -o \
# "$HOME"/bin/bypass.so -ldl -Wall -Wextra
# [ -x "$HOME"/bin/bypass.so ] &&
# export LD_PRELOAD=$HOME/bin/bypass.so
# HISTORY #####################################################################
# Colon seperated list of exact commands to ignore
export HISTIGNORE="clear:bg:fg:cd:pd:exit:date:poweroff:reboot:* --help"
# Avoid duplicates, and share history across terminals
export HISTCONTROL=ignoredups:erasedups
shopt -s histappend
PROMPT_COMMAND="history -n;history -w;history -c;history -r;$PROMPT_COMMAND"
# Date and time format
#export HISTTIMEFORMAT='%Y.%m.%d %T '
# Max history size
export HISTFILESIZE=8192
# PATH ########################################################################
# Remove duplicate items from path
# https://unix.stackexchange.com/a/338737
remove_dups() {
local D=${2:-:}
local path=
local dir=
while IFS= read -r -d"$D" dir; do
[[ $path$D =~ .*$D$dir$D.* ]] || path+="$D$dir"
done <<<"$1$D"
printf %s "${path#$D}"
}
PATH="$(remove_dups "$PATH")"
# Cleanly add an item to the end of $PATH
# https://unix.stackexchange.com/a/124447
path_append() {
case ":${PATH:=$1}:" in
*:$@:*) ;;
*) PATH="$PATH:$1" ;;
esac
}
# Cleanly add an item to the beginning of $PATH
path_override() {
case ":${PATH:=$1}:" in
*:$@:*) ;;
*) PATH="$1:$PATH" ;;
esac
}
[ -d "$HOME/.local/bin" ] && path_override "$HOME/.local/bin"
[ -d "$HOME/.config/overrides" ] && path_override "$HOME/.config/overrides"
path_append "$HOME/bin"
# Add ~/.local/lib to library path (cleanly)
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$HOME/.local/lib"
LD_LIBRARY_PATH="$(remove_dups "$LD_LIBRARY_PATH")"
# NVM
source_if_readable /usr/share/nvm/init-nvm.sh
# KEYBOARD SHORTCUTS ##########################################################
# Ctrl-Z sends to background, Ctrl-F brings to foreground!
bind -x '"\C-f":fg'
# USER ALIASES ################################################################
# Alias to directly cd into sub-directory of given path, from anywhere
alias_directories() {
for i in "$1"/*; do
_dir="${i##*/}"
_dir="${_dir%% *}"
if [ -d "$1/$_dir" ] && [ -z "$(command -v "$_dir")" ]; then
# shellcheck disable=2139,2140
alias "$_dir"="cd -- $1/$_dir || exit"
fi
done
}
alias_directories "$HOME"
[ -d "$HOME"/projects ] && alias_directories "$HOME"/projects
# apt
if [ -x "$(command -v apt)" ]; then
if [ -x "$(command -v apt-fast)" ] || [ -x /usr/sbin/apt-fast ]; then
alias apt='sudo apt-fast'
else
alias apt='sudo apt'
fi
alias aptup='apt update && apt dist-upgrade && apt autoremove'
fi
# aurvote
[ -x "$(command -v aurvote)" ] && [ -z "$(command -v aurvoteall)" ] &&
alias aurvoteall='pacman -Qm | cut -f1 -d" " | xargs aurvote'
# bats
[ -x "$(command -v bats)" ] && [ -x "$(command -v time)" ] &&
alias bats='time bats'
# cd
dir_memo() {
_d="$1"
shift
builtin "$_d" "$@" && {
# shellcheck disable=1003
[ ! -f .dir-memo ] ||
sed -e '1i\\' -e 's/^/ \o033[36m=>\o033[0m /' .dir-memo >&2
}
}
alias cd='dir_memo cd'
alias pushd='dir_memo pushd'
alias popd='dir_memo popd'
alias .='cd -'
alias cd..='cd ..'
if [ -z "$(command -v pd)" ]; then
# shellcheck disable=2164
pd() {
if [ -n "$1" ]; then
pushd "$1"
else
popd
fi
}
fi
# dig
if [ -x "$(command -v dog)" ] && [ -z "$(command -v dig)" ]; then
alias dig=dog
fi
# dnf (and yum)
if [ -x "$(command -v dnf)" ]; then
alias dnf='sudo dnf'
elif [ -x "$(command -v yum)" ]; then
alias dnf='sudo yum'
fi
[ "$(type -t dnf)" == 'alias' ] && alias dnfup='dnf -y update'
# editor
[ -x "$(command -v vi)" ] && export EDITOR='vi'
if [ -x "$(command -v vim)" ]; then
# shellcheck disable=2139
alias vi='$(command -v vim)'
alias vimrc='$(command -v vim) ~/.vim/vimrc'
EDITOR="$(command -v vim)"
export EDITOR
fi
[ -x "$(command -v sudo)" ] && alias se='sudo -e'
# ls
[ -z "$(command -v ll)" ] && alias ll='ls -ahl'
[ -z "$(command -v sl)" ] && alias sl='ls'
# mkdir
alias mkdir='mkdir -pv'
# makepkg
[ -n "$(command -v makepkg)" ] &&
alias mksrcinfo='makepkg --printsrcinfo > .SRCINFO'
# mosh
[ -n "$(command -v mosh)" ] && [ ! -v "$(command -v mop)" ] &&
alias mop='mosh -p 59999'
# networking
[ -z "$(command -v ipme)" ] && alias ipme="curl ifconfig.me; printf '\n'"
[ -z "$(command -v pinc)" ] && alias pinc='ping -c'
# pacman
if [ -x "$(command -v reflector)" ] && [ -z "$(command -v mirrors)" ]; then
mirrors() {
local OPTS='--age 12 --country US --fastest 50 --protocol https --sort rate'
if [ -x "$(command -v spinner)" ]; then
sudo spinner "reflector $OPTS --save /etc/pacman.d/mirrorlist" \
"Updating mirrorlist:" 'unicode'
else
printf 'Updating mirrorlist:'
# shellcheck disable=2086
reflector $OPTS --save /etc/pacman.d/mirrorlist
printf ' Done\n'
fi
}
fi
if [ -x "$(command -v paru)" ]; then
[ -z "$(command -v pac)" ] && alias pac='paru'
[ -z "$(command -v pup)" ] && alias pup='pac -Syu --noconfirm --devel'
if [ -z "$(command -v pin)" ]; then
pin() {
paru -Slq |
fzf -m --preview "cat <(paru -Si {1}) <(paru -Fl {1} | awk \"{print \$2}\")" |
xargs -ro paru -S
}
fi
fi
if [ -x "$(command -v pacman)" ]; then
[ -z "$(command -v pac)" ] && alias pac='sudo pacman'
[ -z "$(command -v pup)" ] && alias pup='sudo pacman -Syyu --noconfirm'
pacowns() {
pacman -Qo "$(command -v "$1")"
}
fi
[ "$(type -t mirrors)" == 'function' ] && [ "$(type -t pup)" == 'alias' ] &&
[ -z "$(command -v rup)" ] && alias rup='mirrors && pup'
# ps
[ -z "$(command -v pf)" ] && alias pf='ps auxf'
[ -z "$(command -v pg)" ] && alias pg='ps aux | grep -v grep | grep -i -e'
if [ -z "$(command -v kp)" ] && [ -x "$(command -v fzf)" ]; then
kp() {
local pid
pid=$(ps -ef | sed 1d | eval "fzf -m --header='[kill:process]'" | awk \
'{print $2}')
if [ -n "$pid" ]; then
echo "$pid" | xargs kill -"${1:-9}"
kp "$@"
fi
}
fi
# ssh
if [ -x "$(command -v ssh)" ]; then
# Fix "'alacritty': unknown terminal type"
alias ssh='TERM=xterm-256color ssh'
# Easy tunnel creation
if [ -z "$(command -v ssh_tunnel)" ]; then
# Create ssh tunnel on port $2 (default 19998) to server $1
ssh_tunnel() {
ssh -f -N -D "${2:-19998}" -4 "$1"
}
# Kill the ssh tunnel running on server or port $1 (default all tunnels)
kill_tunnel() {
local PID
PID="$(pgrep -a ssh | grep "\-f \-N \-D .*$1.*" | cut -f1 -d' ' |
xargs)"
if ((${#PID} > 0)); then
kill "$PID"
else
printf 'No ssh tunnels running.\n'
fi
}
fi
fi
# su and sudo
[ -x "$(command -v su)" ] && [ -x "$(command -v sudo)" ] && alias su='sudo su'
# systemctl
[ -x "$(command -v systemctl)" ] && [ -z "$(command -v sctl)" ] &&
alias sctl='sudo systemctl'
# tmux
# https://www.nathankowald.com/blog/2014/03/tmux-attach-session-alias/
if [ -x "$(command -v tmux)" ]; then
if [ -z "$(command -v tmax)" ]; then
tmax() {
if (($# > 0)); then
tmux attach -t "$1" 2>/dev/null || tmux new -s "$@"
else
tmux attach 2>/dev/null || tmux new "$@"
fi
}
_tmax() {
# shellcheck disable=2033
read -ra COMPREPLY <<<"$(compgen -W "$(tmux ls -F '#S' | xargs)" \
-- "${COMP_WORDS[COMP_CWORD]}")"
}
complete -F _tmax tmax
fi
[ -z "$(command -v tmls)" ] && alias tmls='tmux ls'
if [ "$(type -t tmax)" == 'function' ] &&
[ -z "$(command -v tv)" ] &&
[ -n "$EDITOR" ]; then
tv() {
tmax 'editor' "$EDITOR $*"
}
fi
fi
# wget, curl, aria2c
[ -x "$(command -v wget)" ] && alias wget='wget -c'
if [ -z "$(command -v ttfb)" ]; then
ttfb() {
local line='Connect: %{time_connect}'
line+=' TTFB: %{time_starttransfer}'
line+=' Total time: %{time_total} \n'
curl -sSo /dev/null -w "$line" "$1"
}
fi
# Import bash secrets from protected file
source_if_readable "$HOME"/safe/bash-secrets.sh
# Dotfiles bare repository
if [ -x "$(command -v dots)" ]; then
printf '***** WARNING ***********************************'
# shellcheck disable=2016
printf 'The `dots` command exists on this system already!'
printf '*************************************************'
else
alias dots='git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
fi