-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
.bashrc
380 lines (316 loc) · 11.2 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
#!/bin/bash
# shellcheck disable=SC1090,SC1091
case $- in
*i*) ;; # interactive
*) return ;;
esac
# ---------------------- local utility functions ---------------------
_have() { type "$1" &>/dev/null; }
_source_if() { [[ -r "$1" ]] && source "$1"; }
# ----------------------- environment variables ----------------------
export LANG=en_US.UTF-8
export USER="${USER:-$(whoami)}"
export GITUSER="$USER"
export TZ=America/New_York
export REPOS="$HOME/Repos"
export GHREPOS="$REPOS/github.com/$GITUSER"
export DOTFILES="$GHREPOS/dot"
export HELP_BROWSER=lynx
export DESKTOP="$HOME/Desktop"
export DOCUMENTS="$HOME/Documents"
export DOWNLOADS="$HOME/Downloads"
export TEMPLATES="$HOME/Templates"
export SCRIPTS="$HOME/Scripts"
export PUBLIC="$HOME/Public"
export PRIVATE="$HOME/Private"
export PICTURES="$HOME/Pictures"
export MUSIC="$HOME/Music"
export VIDEOS="$HOME/Movies"
export PDFS="$HOME/usb/pdfs"
export VIRTUALMACHINES="$HOME/VirtualMachines"
export CLIP_DIR="$VIDEOS/clips"
export CLIP_DATA="$GHREPOS/clip/data"
export CLIP_VOLUME=0
export CLIP_SCREEN=0
export TERM=xterm-256color
export CLICOLOR=1
export HRULEWIDTH=73
export GOPATH="$HOME/.local/go"
#export GOBIN="$HOME/.local/bin"
export GOPROXY=direct
export NVIM_SCREENKEY=1
export CGO_ENABLED=0
export PYTHONDONTWRITEBYTECODE=2
export LC_COLLATE=C
export CFLAGS="-Wall -Wextra -Werror -O0 -g -fsanitize=address -fno-omit-frame-pointer -finstrument-functions"
# gruvbox-material
export LS_COLORS="di=38;5;245:fi=38;5;223:ln=38;5;179:ex=38;5;108:*.txt=38;5;223"
export LESS="-FXR"
export LESS_TERMCAP_md=$'\e[1;33m' # start bold (yellow)
export LESS_TERMCAP_mb=$'\e[1;35m' # start blinking (magenta)
export LESS_TERMCAP_me=$'\e[0m' # end bold/blinking
export LESS_TERMCAP_so=$'\e[38;5;108;1m' # start standout (green bold)
export LESS_TERMCAP_se=$'\e[0m' # end standout
export LESS_TERMCAP_us=$'\e[4m' # start underline
export LESS_TERMCAP_ue=$'\e[0m' # end underline
export ANSIBLE_CONFIG="$HOME/.config/ansible/config.ini"
export ANSIBLE_INVENTORY="$HOME/.config/ansible/inventory.yaml"
export ANSIBLE_LOAD_CALLBACK_PLUGINS=1
#export ANSIBLE_STDOUT_CALLBACK=json
export K8SAPP_CLUSTER=dev
[[ -d /.vim/spell ]] && export VIMSPELL=("$HOME/.vim/spell/*.add")
# ----------------------------- PostgreSQL ----------------------------
export PGPASS=~/.pgpass
# export PGHOST=localhost
# export PGUSER=myuser
# export PGDATABASE=mydatabase
# -------------------------------- gpg -------------------------------
GPG_TTY=$(tty)
export GPG_TTY
# ------------------------------- pager ------------------------------
if [[ -x /usr/bin/lesspipe ]]; then
export LESSOPEN="| /usr/bin/lesspipe %s"
export LESSCLOSE="/usr/bin/lesspipe %s %s"
fi
# ----------------------------- dircolors ----------------------------
if _have dircolors; then
if [[ -r "$HOME/.dircolors" ]]; then
eval "$(dircolors -b "$HOME/.dircolors")"
else
eval "$(dircolors -b)"
fi
fi
# ------------------------------- path -------------------------------
pathappend() {
declare arg
for arg in "$@"; do
test -d "$arg" || continue
PATH=${PATH//":$arg:"/:}
PATH=${PATH/#"$arg:"/}
PATH=${PATH/%":$arg"/}
export PATH="${PATH:+"$PATH:"}$arg"
done
} && export -f pathappend
pathprepend() {
for arg in "$@"; do
test -d "$arg" || continue
PATH=${PATH//:"$arg:"/:}
PATH=${PATH/#"$arg:"/}
PATH=${PATH/%":$arg"/}
export PATH="$arg${PATH:+":${PATH}"}"
done
} && export -f pathprepend
# remember last arg will be first in path
pathprepend \
"$HOME/.local/bin" \
"$HOME/.local/go/bin" \
"$GHREPOS/cmd-"* \
/usr/local/go/bin \
/usr/local/opt/openjdk/bin \
/usr/local/bin \
/opt/homebrew/bin \
"$SCRIPTS"
pathappend \
/usr/local/opt/coreutils/libexec/gnubin \
'/mnt/c/Windows' \
'/mnt/c/Program Files (x86)/VMware/VMware Workstation' \
/mingw64/bin \
/usr/local/bin \
/usr/local/sbin \
/usr/local/games \
/usr/games \
/usr/sbin \
/usr/bin \
/snap/bin \
/sbin \
/bin
# ------------------------------ cdpath ------------------------------
export CDPATH=".:$GHREPOS:$DOTFILES:$REPOS:/media/$USER:$HOME"
# ------------------------ bash shell options ------------------------
# shopt is for BASHOPTS, set is for SHELLOPTS
shopt -s checkwinsize # enables $COLUMNS and $ROWS
shopt -s expand_aliases
shopt -s globstar
shopt -s dotglob
shopt -s extglob
# -------------------------- stty annoyances -------------------------
stty -ixon # disable control-s/control-q tty flow control
# ------------------------------ history -----------------------------
export HISTCONTROL=ignoreboth
export HISTSIZE=5000
export HISTFILESIZE=10000
set -o vi
shopt -s histappend
# --------------------------- smart prompt ---------------------------
# (keeping in bashrc for portability)
PROMPT_LONG=20
PROMPT_MAX=95
PROMPT_AT=@
__ps1() {
local P='$' dir="${PWD##*/}" B countme short long double \
r='\[\e[31m\]' h='\[\e[34m\]' \
u='\[\e[33m\]' p='\[\e[34m\]' w='\[\e[35m\]' \
b='\[\e[36m\]' x='\[\e[0m\]' \
g="\[\033[38;2;90;82;76m\]"
[[ $EUID == 0 ]] && P='#' && u=$r && p=$u # root
[[ $PWD = / ]] && dir=/
[[ $PWD = "$HOME" ]] && dir='~'
B=$(git branch --show-current 2>/dev/null)
[[ $dir = "$B" ]] && B=.
countme="$USER$PROMPT_AT$(hostname):$dir($B)\$ "
[[ $B == master || $B == main ]] && b="$r"
[[ -n "$B" ]] && B="$g($b$B$g)"
short="$u\u$g$PROMPT_AT$h\h$g:$w$dir$B$p$P$x "
long="${g}╔$u\u$g$PROMPT_AT$h\h$g:$w$dir$B\n${g}╚$p$P$x "
double="${g}╔$u\u$g$PROMPT_AT$h\h$g:$w$dir\n${g}║$B\n${g}╚$p$P$x "
if ((${#countme} > PROMPT_MAX)); then
PS1="$double"
elif ((${#countme} > PROMPT_LONG)); then
PS1="$long"
else
PS1="$short"
fi
if _have tmux && [[ -n "$TMUX" ]]; then
tmux rename-window "$(wd)"
fi
}
wd() {
dir="${PWD##*/}"
parent="${PWD%"/${dir}"}"
parent="${parent##*/}"
echo "$parent/$dir"
} && export wd
PROMPT_COMMAND="__ps1"
# ----------------------------- keyboard -----------------------------
# only works if you have X and are using graphic Linux desktop
_have setxkbmap && test -n "$DISPLAY" &&
setxkbmap -option caps:escape &>/dev/null
# ------------------------------ aliases -----------------------------
# (use exec scripts instead, which work from vim and subprocs)
unalias -a
alias todo='vi ~/.todo'
alias ip='ip -c'
alias '?'=gpt
alias '??'=duck
alias '???'=google
alias dot='cd $DOTFILES'
alias scripts='cd $SCRIPTS'
alias snippets='cd $SNIPPETS'
alias ls='ls -h --color=auto'
alias free='free -h'
alias tree='tree -a'
alias df='df -h'
alias chmox='chmod +x'
alias diff='diff --color'
alias sshh='sshpass -f $HOME/.sshpass ssh '
alias temp='cd $(mktemp -d)'
alias view='vi -R' # which is usually linked to vim
alias clear='printf "\e[H\e[2J"'
alias c='printf "\e[H\e[2J"'
alias env='env -u LESS_TERMCAP_mb -u LESS_TERMCAP_md -u LESS_TERMCAP_me -u LESS_TERMCAP_so -u LESS_TERMCAP_se -u LESS_TERMCAP_us -u LESS_TERMCAP_ue'
alias neo="neo -D -c gold"
alias more="less"
alias gitl="git log -n 5 --graph --decorate --oneline"
alias gp="git push"
alias gptags="git push origin --tags"
alias km="kimono"
_have vim && alias vi=vim && export EDITOR=vim && export VISUAL=vim
_have nvim && alias vi=nvim && export EDITOR=nvim && export VISUAL=nvim
# ----------------------------- functions ----------------------------
envx() {
local envfile="${1:-"$HOME/.env"}"
[[ ! -e "$envfile" ]] && echo "$envfile not found" && return 1
while IFS= read -r line; do
name=${line%%=*}
value=${line#*=}
[[ -z "${name}" || $name =~ ^# ]] && continue
export "$name"="$value"
done <"$envfile"
} && export -f envx
[[ -e "$HOME/.env" ]] && envx "$HOME/.env"
new-from() {
local template="$1"
local name="$2"
! _have gh && echo "gh command not found" && return 1
[[ -z "$name" ]] && echo "usage: $0 <name>" && return 1
[[ -z "$GHREPOS" ]] && echo "GHREPOS not set" && return 1
[[ ! -d "$GHREPOS" ]] && echo "Not found: $GHREPOS" && return 1
cd "$GHREPOS" || return 1
[[ -e "$name" ]] && echo "exists: $name" && return 1
gh repo create -p "$template" --public "$name"
gh repo clone "$name"
cd "$name" || return 1
} && export -f new-from
clone() {
local repo="$1" user
local repo="${repo#https://github.com/}"
local repo="${repo#[email protected]:}"
if [[ $repo =~ / ]]; then
user="${repo%%/*}"
else
user="$GITUSER"
[[ -z "$user" ]] && user="$USER"
fi
local name="${repo##*/}"
local userd="$REPOS/github.com/$user"
local path="$userd/$name"
[[ -d "$path" ]] && cd "$path" && return
mkdir -p "$userd"
cd "$userd"
echo gh repo clone "$user/$name" -- --recurse-submodule
gh repo clone "$user/$name" -- --recurse-submodule
cd "$name"
} && export -f clone
# ------------- source external dependencies / completion ------------
# for mac
if [[ "$OSTYPE" == "darwin"* ]]; then
brew_prefix="$(brew --prefix)"
if [[ -r "$brew_prefix/etc/profile.d/bash_completion.sh" ]]; then
source "$brew_prefix/etc/profile.d/bash_completion.sh"
fi
fi
owncomp=(
pdf zet keg kn yt gl auth pomo config live iam sshkey ws x clip
./build build b ./k8sapp k8sapp ./setup ./cmd run ./run
foo ./foo cmds ./cmds z bon openapi obs vault var sunrise multicall
kimono tag cm
)
for i in "${owncomp[@]}"; do complete -C "$i" "$i"; done
_have gh && . <(gh completion -s bash)
_have glow && . <(glow completion bash)
_have goreleaser && . <(goreleaser completion bash 2>/dev/null)
_have klogin && . <(klogin completion bash 2>/dev/null)
_have pandoc && . <(pandoc --bash-completion)
_have kubectl && . <(kubectl completion bash 2>/dev/null)
_have kubeadm && . <(kubeadm completion bash 2>/dev/null)
_have k && complete -o default -F __start_kubectl k
_have istioctl && . <(istioctl completion bash 2>/dev/null)
_have kind && . <(kind completion bash)
_have kompose && . <(kompose completion bash)
_have helm && . <(helm completion bash)
_have minikube && . <(minikube completion bash)
_have conftest && . <(conftest completion bash)
_have yq && . <(yq completion bash)
_have mk && complete -o default -F __start_minikube mk
_have podman && _source_if "$HOME/.local/share/podman/completion" # d
_have ansible && . <(register-python-argcomplete3 ansible)
_have ansible-config && . <(register-python-argcomplete3 ansible-config)
_have ansible-console && . <(register-python-argcomplete3 ansible-console)
_have ansible-doc && . <(register-python-argcomplete3 ansible-doc)
_have ansible-galaxy && . <(register-python-argcomplete3 ansible-galaxy)
_have ansible-inventory && . <(register-python-argcomplete3 ansible-inventory)
_have ansible-playbook && . <(register-python-argcomplete3 ansible-playbook)
_have ansible-pull && . <(register-python-argcomplete3 ansible-pull)
_have ansible-vault && . <(register-python-argcomplete3 ansible-vault)
#_have ssh-agent && test -z "$SSH_AGENT_PID" && . <(ssh-agent)
# -------------------- personalized configuration --------------------
_source_if "$HOME/.bash_personal"
_source_if "$HOME/.bash_private"
_source_if "$HOME/.bash_work"
_have terraform && complete -C /usr/bin/terraform terraform
_have terraform && complete -C /usr/bin/terraform tf
# ------------------------- NVM trash ahead ------------------------
# (keep as is or nvm idiotic installer will re-add to bashrc next time)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion