-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_bashrc
510 lines (407 loc) · 15.2 KB
/
dot_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
# shellcheck shell=bash
# vim:ft=bash
# 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
# return
# environment {{{
paths=(
/opt/local/bin # mac
/snap/go/current/bin # ubuntu
/usr/local/bin # dwm
~/.cargo/bin # arch installs are preferred over cargo
~/.local/bin
~/dita/.venv/bin # poetry config --local virtualenvs.in-project true
~/go/bin
~/rust/fours/target/release
~/rust/search/target/release
)
# to see 'clean' sys.path, run 'python3 -S -m site'
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
# # 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'
alias v="vim"
# }}}
# 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 # these also need to be set in systemd
# 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=vid.puffyan.us
export NTFS_ILLEGAL='<>:"/\|?*'
# if [[ $(xdpyinfo | awk '/dimensions/ {print $2}') == 3840x2160 ]]; then
# export ROFI_FONT='"fira mono 12"'
# else
# export ROFI_FONT='"fira mono 9"'
# fi
# 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
~/dita
~/gone
~/gripts
~/ibb
~/kilo
~/monkey
~/mpv-search
~/pg-neon
~/plaque
~/rust/lasttree
~/scripts
~/ts/handbook
~/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
# }}}
# 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
# startup {{{
if [[ -f /tmp/updates ]] && ! pgrep pacman > /dev/null; then
cat /tmp/updates
< /tmp/updates grep '^python ' && echo -e "Beware: new Python\n"
# TODO: after major python update, update globally installed pips (scrobblerh)
if sudo pacman -Syu; then
rm /tmp/updates
# may get unset after update? not sure what causes it
setxkbmap -layout us -option -option compose:rctrl,caps:menu
elif
# if pacman hasn't been run in a long time, keys in keyring
# will expire. updating archlinux-keyring should fix this
command -v expac > /dev/null &&
last_update=$(expac --timefmt '%s' "%l" | sort -n | tail -n1) &&
# https://stackoverflow.com/a/6948865
[[ $(((last_update - $(date +%s)) / (60 * 60 * 24))) -gt 14 ]]
then
sudo pacman -Sy archlinux-keyring
fi
fi
# }}}
# macos settings {{{
if [[ $OSTYPE == darwin* ]]; then
# time desync cannot be fixed in system settings, even though the same
# server is used -- thanks apple!
# sudo sntp -sS time.apple.com
alias exa=eza # exa is unmaintained; on arch, eza is aliased to exa
alias xdg-open=open
export BASH_SILENCE_DEPRECATION_WARNING=1
export FZF_DEFAULT_COMMAND="find ." # path cannot be omitted
# osxkeychain is a fairly painless, https based (not ssh) way to access
# private repos from multiple accounts
# git config --list --show-origin
# file:/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig credential.helper=osxkeychain
# https://docs.github.com/en/get-started/getting-started-with-git/updating-credentials-from-the-macos-keychain#updating-your-credentials-via-keychain-access
# https://gist.github.com/nepsilon/0fd0c779f76d7172f12477ba9d71bb66#how-to-use-mac-keychain-to-store-github-repos-credentials
# https://git-scm.com/book/sv/v2/Git-Tools-Credential-Storage
# for gui (VNC Viewer)
# docker rm -vf $(docker ps -aq)
# docker rmi -f $(docker images -aq)
fi
# }}}
# aliases {{{
# note: rsync trailing slash: https://unix.stackexchange.com/a/402556
alias cp="cp -rv"
alias grep="grep --color=auto"
alias ln="ln -v"
alias mkdir="mkdir -p"
alias mv="mv -v"
alias pgrep="pgrep --list-full --exact"
alias python=python3
alias rsync="rsync --partial --progress --recursive --archive --update" # all options are generally desired
alias xargs1="xargs --max-args=1"
alias xargs="xargs --no-run-if-empty --verbose"
alias _exa="exa --all --long --links --dereference --color=always --git" # helper
alias c="curl --silent --location --fail"
alias cg=cget
alias cget="wget --continue"
alias cn="cargo new"
alias ftail="tail -f"
alias gtime="env time --format \"\$GNUTIMEFORMAT\" -- echo" # aliasing time to gnu time will disable the builtin
alias ls="_exa --group-directories-first --tree --level 2" # note: --tree is slow in large dirs
alias lsc="_exa --time-style=iso --sort created"
alias lsl="_exa --time-style=iso --sort modified --no-permissions --octal-permissions --no-user"
alias qm="vim +Quickmath -c startinsert /tmp/foo"
alias r="fm" # --cmd=\"sort ctime\" --cmd=\"set sort_directories_first false\""
# alias gD="git --no-pager diff --no-index" # default pager (not delta)
# alias gdw="git diff --color-words"
# alias gl="git log --stat --oneline"
alias gC="git reset --soft HEAD~1" # un-commit
alias ga="git add"
alias gap="git add --patch"
alias gdn="git diff --no-index" # https://stackoverflow.com/a/17433969 -- TODO: handle symlinks
alias gdnc="git diff --color-words=. --no-index" # diff by char; may be replaced with delta
alias gdnw="git diff --color-words --no-index"
alias gig="\$EDITOR .gitignore"
alias gun="git checkout --" # discard all local changes ("undo"); git revert is the same, but also creates a commit
if command -v pacman > /dev/null; then
alias get="sudo pacman --sync"
alias lp="pacman --query --search"
alias pclean="paccache --remove && sudo pacman -Scc"
alias pwhich="pacman -Qo"
alias rem="sudo pacman --remove --recursive" # --cascade
alias sp="pacman --sync --search"
elif command -v apt > /dev/null; then
alias get="sudo apt install" # || snap
alias rem="sudo apt --purge autoremove"
elif command -v port > /dev/null; then
alias get="sudo port install"
fi
# }}}
# 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 in inputrc
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
# GSHA=$(git rev-parse --short HEAD) # sha is cool but useless
GIT_PROMPT() {
# https://gist.github.com/matthewmccullough/47267
# https://gist.github.com/nickboldt/4055398#file-bashrc-L50
# 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
# remote must exist
[[ -z $(git remote -v) ]] && return
# git branch -r (remote)
# git branch -l (local)
CURRENT_LOCAL_BRANCH=$(git branch --show-current)
local prompt
prompt=''
if [[ $(git branch | wc -l) -ne 1 ]]; then
# rev-parse fails if no remote yet
prompt+="<$CURRENT_LOCAL_BRANCH:$(git rev-parse --short HEAD 2> /dev/null)> " || return
fi
# set -x
# sorted branches, most recently committed first
REMOTE_BRANCHES=$(git branch --sort=-committerdate) # default: local branches only
# REMOTE_BRANCHES=$(git branch --remotes --sort=-committerdate)
if
[[ $REMOTE_BRANCHES == *upstream* ]] # repo is a fork
then
REM_BRANCH=$(grep <<< "$REMOTE_BRANCHES" -m1 upstream | xargs)
else
REM_BRANCH=$(
# refs/remotes/origin/master (or main)
git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null |
cut -d/ -f4
# cut -d/ -f3-
)
if [[ -z $REM_BRANCH ]]; then
>&2 echo 'empty remote branch found; fixing...'
git remote set-head origin --auto > /dev/null
REM_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null | cut -d/ -f4)
fi
fi
# format is there \t here, where each int represents number of commits
# ahead of local HEAD
state=$(
git rev-list --left-right --count \
"origin/$REM_BRANCH...$CURRENT_LOCAL_BRANCH" 2> /dev/null |
\xargs
)
if [[ $state == '0 0' ]]; then
: up to date with master
state=""
else
# state=$(<<< "$state" tr -s '\t' :)
# probably better to destructure fields (readarray?), but this
# is fine for now
state=$(<<< "$state" sed -r '
s/^0 //g # not behind master
s/ / commits behind, /g
')
state+=" commits ahead of $REM_BRANCH"
fi
# list number of dead local branches
if dead=$(git branch -vv | grep -c ': gone]'); then
[[ -n $state ]] && state+=' '
state+="($dead dead branches)"
fi
prompt+="$state"
# set +x
echo "$prompt"
}
# apparently, double quotes only evaluate once, while single quotes allow
# (repeated) expansion at runtime
# https://stackoverflow.com/a/21219060
# https://stackoverflow.com/a/23879264
# PS1='[\A] \W/ $(GIT_PROMPT)> '
PS1=''
PS1+='\n' # newline
ps --pid $PPID | grep -q yazi && 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)
# # from now on, only poetry should be used
# if [[ -n $CONDA_PREFIX ]]; then
# PS1="($(basename "$CONDA_PREFIX")) $PS1"
# fi
# }}}
# shellcheck disable=SC1090
[[ -f ~/.opam/opam-init/init.sh ]] && . ~/.opam/opam-init/init.sh
# 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