-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot_bashrc
More file actions
570 lines (463 loc) · 16.7 KB
/
dot_bashrc
File metadata and controls
570 lines (463 loc) · 16.7 KB
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
# shellcheck shell=bash
# vim:ft=sh
# this file is sourced on any unix-like system and should remain under 400 LOC.
# 200 LOC is possible with extensive sourcing. no functions (except for
# GIT_PROMPT) should be declared here.
# POSIX compatibility is not a concern; if a script needs to obtain some env
# vars from here, it should be invoked with bash.
# startup time must remain <= 0.005s
# TIMEFORMAT='%lU' ; for i in $(seq 1 10) ; do time $SHELL -c exit 2>/dev/null ; done
# TIMEFORMAT='%lU' ; for i in $(seq 1 10) ; do time $SHELL -ic exit 2>/dev/null ; done
# return
# environment {{{
paths=(
/opt/local/bin # mac
/snap/go/current/bin # ubuntu
/usr/local/bin # dwm
~/.cargo/bin # arch installs are preferred over cargo
~/.diversion/bin
~/.local/bin
~/dita/.venv/bin # poetry config --local virtualenvs.in-project true
~/go/bin
~/rust/fours/target/release
~/rust/search/target/release
)
for p in "${paths[@]}"; do
echo "$PATH" | grep -Fq "$p" && continue
[[ -d $p ]] &&
# export PATH=$p:$PATH # prepend (more problematic, esp with venvs)
export PATH=$PATH:$p # append
done
# echo "${paths[@]}";
unset p paths
export PATH=$HOME/go125/go/bin:$PATH # why is arch taking so long to update go
# # exported by default -- check with 'go env'
# # https://github.com/golang/wiki/blob/master/GOPATH.md#gopath-variable
# # https://go.dev/wiki/SettingGOPATH#bash
# export GOPATH=$HOME/go
# note: TERM is set by x
export TERMINAL='wezterm start'
# export TERMINAL='ghostty -e'
if command -v nvim > /dev/null 2> /dev/null; then
export EDITOR=nvim
if
# /etc/issue (single line)
# /etc/lsb-release (DISTRIB_ID)
# /usr/lib/os-release (most verbose)
! < /etc/issue grep -Fq Ubuntu
then
# TODO: disable man sudo_root message -- https://askubuntu.com/a/22646
export MANPAGER="nvim +Man!"
fi
else
export EDITOR=vim
fi
export VISUAL=$EDITOR
alias vim="\$EDITOR -p" # equivalent to -c ':tab ball'
# }}}
# xdg {{{
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
# https://wiki.archlinux.org/index.php/XDG_Base_Directory
export XDG_CACHE_HOME=$HOME/.cache
export XDG_CONFIG_DIRS=/etc/xdg
export XDG_CONFIG_HOME=$HOME/.config
export XDG_DATA_DIRS=/usr/local/share/:/usr/share/
export XDG_DATA_HOME=$HOME/.local/share
export XDG_STATE_HOME=$HOME/.local/state
# not supported: mailcap, mozilla, vim (not worth the effort)
# requires flags: mbsync
# todo: move aliases here (then update the last line), texlive?, Xresources
# user: dict, cron
export INPUTRC=$XDG_CONFIG_HOME/readline/inputrc
export NMBGIT=$XDG_DATA_HOME/notmuch/nmbug
export NOTMUCH_CONFIG=$XDG_CONFIG_HOME/notmuch/config
# i have a feeling these must be in xinitrc
export XCOMPOSECACHE=$XDG_CACHE_HOME/X11/XCompose
export XCOMPOSEFILE=$XDG_CONFIG_HOME/X11/XCompose
# export HISTFILE="${XDG_STATE_HOME}"/bash/history
# export XAUTHORITY="$XDG_RUNTIME_DIR"/Xauthority
# export XINITRC="$XDG_CONFIG_HOME"/X11/xinitrc
# xdg-ninja rubbish
export ANDROID_HOME=$XDG_DATA_HOME/android
export GNUPGHOME=$XDG_DATA_HOME/gnupg
export JUPYTER_CONFIG_DIR=$XDG_CONFIG_HOME/jupyter
export KERAS_HOME=$XDG_STATE_HOME/keras
export LESSHISTFILE=$XDG_CACHE_HOME/less/history
export MEDNAFEN_HOME=$XDG_CONFIG_HOME/mednafen
export NODE_REPL_HISTORY=$XDG_DATA_HOME/node_repl_history
export PARALLEL_HOME=$XDG_CONFIG_HOME/parallel
export PYLINTRC=$XDG_CONFIG_HOME/pylint/pylintrc
export PYTHONSTARTUP=$XDG_CONFIG_HOME/python/pythonrc
export SQLITE_HISTORY=$XDG_CACHE_HOME/sqlite_history
export TEXMFVAR=$XDG_CACHE_HOME/texlive/texmf-var
export VD_CONFIG=$XDG_CONFIG_HOME/visidata/config
# # https://www.navidrome.org/docs/usage/configuration-options/#basic-configuration
# export ND_CONFIGFILE=$XDG_CONFIG_HOME/navidrome/navidrome.toml
# }}}
# user variables {{{
export FIREFOX_PROFILE=4clnophl.default
export INVID=invidious.nerdvpn.de
export NTFS_ILLEGAL='<>:"/\|?*'
# alias time='(TIMEFORMAT=" %1Uu %1Ss %1Re"; time m)'
# https://gist.github.com/linse/3737870#bash-time
# https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-TIMEFORMAT
# alias time="TIMEFORMAT='%lU' ; time"
# gnu time -- /usr/bin/time
# https://www.man7.org/linux/man-pages/man1/time.1.html
# https://github.com/amerlyq/airy/blob/cc367c02c4a321b292c2f106ce02010d9835f8d1/open/alias#L14
# GNUTIMEFORMAT="%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k %Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps" # default
GNUTIMEFORMAT="%ee"
GNUTIMEFORMAT+=" %Uu"
GNUTIMEFORMAT+=" %Ss"
GNUTIMEFORMAT+=" mem=%MkB"
# GNUTIMEFORMAT+=" fault=%F+%R" # page faults (major, minor)
GNUTIMEFORMAT+=" io=%I+%OkB/s" # input, output
# GNUTIMEFORMAT+=" ctx=%c+%w" # context switches, waits
GNUTIMEFORMAT+=" exit=%x" # exit code
# 0.00e 0.00u 0.00s mem=1792kB io=0+0kB/s exit=0
# drives are unnamed after mkfs, run e2label to rename
# changes take effect after remount
# basically all hardcoded paths have been removed from relevant scripts
# cd ~/scripts ; ack -o "[^#]*/run/media[^\"']+" | cut -d: -f1 | sort -u
# allowed: elem (must be hardcoded), driv (deprecated), checklib.py (deprecated)
if [[ -d "/run/media/$USER/Elements" ]]; then
export HDD="/run/media/$USER/Elements"
export MU="$HDD/music"
export SLSK="$HDD/music_slsk"
export SOURCE="$HDD/music_slsk/complete"
export UNTAGGED="$HDD/music_untagged"
fi
# array requires bash
# for files in <dir> to appear in o, <dir> must be a git repo
export WORK_DIRS=(
# ~/blog
# ~/febe
# ~/mappy
# ~/rust/discogs
# ~/rust/discoll
# ~/rust/fours
# ~/rust/search
~/disq
~/dita
~/gone
~/gripts
~/ibb
~/kilo
~/leaf
~/monkey
~/mpv-search
~/pg-neon
~/plaque
~/rust/lasttree
~/scripts
~/ts/handbook
~/vapl
~/ytm
)
# }}}
# misc {{{
# https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
# complete -c man which
# set -o ignoreeof
# export IGNOREEOF=1 # require 2 ctrl-d's to exit; must be declared after set
shopt -s checkwinsize # enabled by default
# shopt -s failglob # fail if glob matches nothing; note: cmds with failed globs will not be stored in history
# https://doc.qt.io/qt-5/highdpi.html#high-dpi-support-in-qt
# should be disabled (0) on prism solo
export QT_AUTO_SCREEN_SCALE_FACTOR=0
# TODO: to include this when sourcing, LINES needs to be set
# include hidden files, can be quite slow from $HOME
# note: ctrl-i mirrors tab, so it cannot be bound
# note: ctrl-shift not supported: https://github.com/junegunn/fzf/issues/2867
export FZF_DEFAULT_OPTS="
--bind ctrl-c:cancel
--bind ctrl-h:preview-page-down
--bind ctrl-l:preview-page-up
--cycle
--pointer='→'
--preview-window='bottom,cycle,wrap,border-top,5'
--preview='less -- {}'
--prompt='→ '
"
export STREAMLIT_SERVER_RUN_ON_SAVE=true
# export STREAMLIT_THEME_BASE=light
# https://python-poetry.org/docs/configuration/#virtualenvsin-project
# going to ~/.cache/pypoetry/virtualenvs is annoying
export POETRY_VIRTUALENVS_IN_PROJECT=true
export RIPGREP_CONFIG_PATH=$HOME/.config/ripgrep/config
# }}}
# when sourced from a script, stop here
case $- in
*i*) : ;;
*) return ;;
esac
export FZF_DEFAULT_COMMAND="find -L"
export LESS="-R --mouse --wheel-lines=$((LINES / 2))" # ensure mouse wheel works in delta
# aliases {{{
# alias tar="tar -vf"
# note: rsync trailing slash: https://unix.stackexchange.com/a/402556
alias cal="cal --monday --week" # util-linux (ubuntu uses bsd's)
alias cp="cp -rv"
alias dmesg="dmesg --time-format=iso"
alias du="du -b" # kb as default makes no sense
alias feh="feh --draw-filename --fullscreen --auto-zoom --sort name --version-sort"
alias grep="grep --color=auto"
alias inotifywait="inotifywait --format='%T %w%f %e' --timefmt='%Y-%m-%dT%H:%M:%S'"
alias journalctl="journalctl --boot --output=short-iso" # journalctl | sort -k3 -k1 | less
alias ln="ln -v"
alias mkdir="mkdir -p"
alias mv="mv -v"
alias parallel="parallel --quote"
alias pgrep="pgrep --exact --list-full"
alias pkill="pkill --exact" # non-exact match is dangerous
alias python=python3
alias reset="reset; exec \$SHELL" # TODO: reset undoes C-w (inputrc); bind -f doesn't fix it
alias rsync="rsync --partial --progress --recursive --archive --update" # all options are generally desired
alias top="top -w\$COLUMNS"
alias xargs="xargs --no-run-if-empty --verbose"
# alias gtime='env time --format="$GNUTIMEFORMAT" -- echo' # aliasing time to gnu time will disable the builtin
# alias t="wezterm start --new-tab --cwd \$(realpath .)" # just use ctrl-t?
alias ..="cd .."
alias c="curl --silent --location --fail"
alias cg="wget --continue"
alias duc="du -csh"
alias fehm="feh --sort mtime"
alias fehr="feh --randomize"
alias ftail="tail -f"
alias h="htop --user=\$USER"
alias qm="vim +Quickmath -c startinsert /tmp/foo"
alias r="yazi"
alias rg.="rg --multiline --multiline-dotall"
alias rgf="rg --files-with-matches"
alias rgl="rg --no-heading --with-filename"
alias sc=systemctl
alias sqj="sqlite3 -json"
alias tmp="cd \$(mktemp -d)"
alias xargs1="xargs --max-args=1"
rgt() { gls | largs rg "$@" 2> /dev/null; } # rg ignores gitignore, but still matches untracked
# { echo '**' ; gls | prepend '!'; } > .ignore # tends to wrongly ignore files
if command -v pacman > /dev/null; then
# alias lpt="pactree"
# alias pclean="paccache --remove && sudo pacman -Scc --noconfirm"
# alias rem="sudo pacman --remove --recursive" # --cascade
alias get="sudo pacman --sync"
alias lp="pacman --query --search"
alias lps="expac --timefmt='%Y-%m-%d %T' '%l\t%n' | sort -n"
alias pclean="paccache --remove && yes | sudo pacman -Scc" # TODO: does not clean /var/cache/pkgfile (500 mb), /var/cache/pacman/pkg/download-* (200 mb) requires root
alias pwhich="pacman -Qo"
alias sp="pacman --sync --search"
alias tz=trizen # if gpg fail, gpg --recv-keys <key>
# expac --timefmt='%Y-%m-%d %T' '%l\t%n' |
# while read -r x; do if command -v "$(<<< "$x" cut -f2)" > /dev/null; then echo "$x"; fi; done |
# sort -n
elif command -v apt > /dev/null; then
alias get="sudo apt install" # || snap
alias lp="apt list --installed 2>/dev/null | grep"
alias lpu="apt list --upgradable"
# alias rem="sudo apt --purge autoremove"
alias sp="apt-cache search . | grep -w"
alias u="sudo apt-get update && sudo apt-get upgrade && sudo apt autoremove"
alias uu="lpu | cut -d/ -f1 | xargs -o sudo apt-get --with-new-pkgs upgrade" # upgrade 'kept back' packages https://askubuntu.com/a/602
elif command -v port > /dev/null; then
alias get="sudo port install"
fi
rem() {
[[ $# -eq 0 ]] && return
if command -v apt > /dev/null; then
sudo apt --purge remove "$@"
return
fi
for pkg in "$@"; do
tree=$(pactree -r "$pkg") || return
if
[[ $(<<< "$tree" wc -l) -eq 1 ]] # no dependents
then
lp "^$pkg$"
else
echo "$tree"
fi
done
sudo pacman --remove --recursive "$@"
}
rema() { rem "$(lp "$1" | grep -Po '^\S+')"; } # remove all packages matching pattern
remsafe() {
: remove pacman package recursively, but reinstall optional deps
pkgs=("$(rem "$1" | grep requires | cut -d' ' -f5 | tr -d : | sort -u | xargs)")
get "${pkgs[@]}"
}
pacmir() {
: regenerate pacman mirrors
# TODO: pacman-mirrors may be more portable
# sudo pacman-mirrors --geoip
# # general purpose speedtest is not really relevant; better (and faster)
# # to check if current top mirror (in mirrorlist) matches current
# # country
# curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py |
# python3 - --json |
# jq -r '.download / 1000000'
curr=$(< /etc/pacman.d/mirrorlist grep -m1 '^Server' |
awk '{print $NF}' |
xargs curl -sL --head --verbose 2>&1 |
grep IPv4 |
awk '{print $NF}' |
xargs -I{} curl -sL ipinfo.io/{} |
jq -r .country)
country=$(curl -sL ipinfo.io | jq -r .country)
[[ $curr == "$country" ]] && return
url="https://archlinux.org/mirrorlist/?country=$country&protocol=https&use_mirror_status=on"
curl -s "$url" |
sed 's/^#Server/Server/; /^#/d' |
rankmirrors --max-time 1 --verbose -n 99 - |
sudo tee /etc/pacman.d/mirrorlist
}
# }}}
# history {{{
PROMPT_COMMAND='history -a' # save commands immediately after execution
export HISTCONTROL=ignoreboth:erasedups
export HISTFILESIZE=10000
export HISTSIZE=10000
shopt -s histappend
# }}}
# prompt {{{
# allow remapping of c-w, etc in inputrc
# man stty
# stty kill undef
stty lnext undef
stty werase undef
# https://linuxcommand.org/lc3_man_pages/seth.html
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
# https://ss64.com/bash/set.html
# https://stackoverflow.com/a/3058390 -- PS1 vs PROMPT_COMMAND
# https://tldp.org/LDP/abs/html/options.html
# https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Prompts
# https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
set -o vi
# set +h # always lookup command path on every invocation
GIT_PROMPT() {
# https://gist.github.com/matthewmccullough/47267
# https://gist.github.com/nickboldt/4055398#file-bashrc-L50
# vscode devc creates this (empty) dir in my home for some reason??
rmdir ~/.gitconfig 2> /dev/null || :
# set -x
# # don't check outside home
[[ $PWD == $HOME* ]] || return
{
[[ -d ./.git ]] ||
# https://stackoverflow.com/a/16925062
git rev-parse --is-inside-work-tree > /dev/null 2>&1
} || return
# git branch -r (remote)
# git branch -l (local)
if
CURRENT_LOCAL_BRANCH=$(git branch --show-current)
[[ -z $CURRENT_LOCAL_BRANCH ]]
then
CURRENT_LOCAL_BRANCH=detached
fi
# rev-parse fails if no remote yet
SHA=$(git rev-parse --short HEAD 2> /dev/null) || return
local prompt
prompt="< $CURRENT_LOCAL_BRANCH :: $SHA > "
# remote must exist
if [[ -z $(git remote -v) ]]; then
echo "$prompt"
return 0
fi
# set -x
if
# sorted branches, most recently committed first
REMOTE_BRANCHES=$(git branch --sort=-committerdate) # default: local branches only
[[ $REMOTE_BRANCHES == *upstream* ]] # `upstream` means repo is a fork (relatively rare)
then
MASTER=$(grep <<< "$REMOTE_BRANCHES" -m1 upstream | \xargs)
elif
# refs/remotes/origin/master (or main)
MASTER=$(git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | \xargs basename)
[[ -n $MASTER ]]
then
:
else
>&2 echo 'empty remote branch found; fixing...'
git remote set-head origin --auto > /dev/null
MASTER=$(git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | \xargs basename)
fi
state=""
# TODO: note: 'behind branch' is not handled (which is relatively rare,
# but will happen when other people commit to my branch) it is handled
# after checkout
read -r behind_m ahead_m < <(
# format is there \t here, where each int represents number of
# commits ahead_m of local HEAD
git rev-list --left-right --count \
"origin/$MASTER...HEAD" 2> /dev/null
)
if ((behind_m > 0 && ahead_m > 0)); then
state="$behind_m commits behind, $ahead_m commits ahead of $MASTER"
elif ((behind_m > 0)); then
state="$behind_m commits behind $MASTER"
elif ((ahead_m > 0)); then
state+="$ahead_m commits ahead of $MASTER"
fi
if
[[ $CURRENT_LOCAL_BRANCH != "$MASTER" ]] &&
dev_ahead=$(git log --oneline --first-parent \
"origin/$CURRENT_LOCAL_BRANCH...HEAD" 2> /dev/null |
wc -l) &&
((dev_ahead > 0))
then
state+=" ($dev_ahead unpushed)"
# TODO: newly cloned repos always end up here, so this is probably incorrect
# elif [[ ! -f $(git rev-parse --show-toplevel)/.git/refs/remotes/origin/$MASTER ]]; then
# state+="(no remote branch yet)"
fi
# list number of dead local branches
if dead=$(git branch -vv | grep -Fc ': gone]'); then
[[ -n $state ]] && state+=' '
state+="($dead dead branches)"
fi
prompt+="$state"
# set +x
echo "$prompt"
}
last_exit() {
# note: $? is only updated when a non-empty cmd is run
if
ex=$?
((ex > 0))
then
echo "$ex "
fi
}
# double quotes only evaluate once, while single quotes allow (repeated)
# expansion at runtime. this is similar to how aliases work
# https://stackoverflow.com/a/21219060
# https://stackoverflow.com/a/23879264
# PS1='[\A] \W/ $(GIT_PROMPT)> '
PS1=''
PS1+='\n' # newline
PS1+='$(last_exit)'
# TODO: parent can also be nvim
ps --pid "$PPID" | grep -Pwq '(yazi|sh)$' && PS1+="📁" # current shell was spawned from within file manager
PS1+='[\A] ' # time
PS1+='$(GIT_PROMPT)'
PS1+='\n' # newline
PS1+='\W/' # cwd with trailing slash -- TODO: relativise to project root
PS1+=' '
PS1+='> ' # prompt symbol
# note: modifying PS1 gives more control than modifying PROMPT_COMMAND, since
# we have 3 dynamic parts (and we already use PROMPT_COMMAND for history
# anyway)
# }}}
# shellcheck disable=SC1090
[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases
# shellcheck disable=SC1090
[[ -f ~/.bash_aliases_work ]] && . ~/.bash_aliases_work
# shellcheck disable=SC1090
[[ -f ~/.config/readline/fzf.bash ]] && . ~/.config/readline/fzf.bash
_complete_pacman_pkgs() {
# shellcheck disable=SC2329
f() { pacman --query --search | grep -P "^local/$1" | cut -d' ' -f1 | cut -d/ -f2; }
_base_completer f
}
complete -F _complete_pacman_pkgs lp
complete -F _complete_pacman_pkgs rem