-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_zshrc
414 lines (357 loc) · 10.7 KB
/
dot_zshrc
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
# -*- mode: shell-script; -*-
bindkey -v
export PAGER="less"
export LESS="-FXR"
export DFT_SYNTAX_HIGHLIGHT="off"
# Persist vi mode between commands
# https://unix.stackexchange.com/a/110877
accept-line() { prev_mode=$KEYMAP; zle .accept-line }
accept-search() { prev_mode=$KEYMAP; zle .accept-search }
zle-line-init() { zle -K ${prev_mode:-vicmd} }
zle -N accept-line
zle -N accept-search
zle -N zle-line-init
# Set cursor shape for vi modes
# http://lynnard.me/blog/2014/01/05/change-cursor-shape-for-zsh-vi-mode/
zle-keymap-select () {
if [ "$TERM" != "dumb" ]; then
if [ $KEYMAP = vicmd ]; then
# the command mode for vi
echo -ne "\e[2 q"
else
# the insert mode for vi
echo -ne "\e[4 q"
fi
fi
}
zle -N zle-keymap-select
# bracket text object
autoload -U select-bracketed
zle -N select-bracketed
for m in visual viopp; do
for c in {a,i}${(s..)^:-'()[]{}<>bB'}; do
bindkey -M $m $c select-bracketed
done
done
# quote text object
autoload -U select-quoted
zle -N select-quoted
for m in visual viopp; do
for c in {a,i}{\',\",\`}; do
bindkey -M $m $c select-quoted
done
done
# surround text object
# https://github.com/zsh-users/zsh/blob/master/Functions/Zle/surround
autoload -Uz surround
zle -N delete-surround surround
zle -N add-surround surround
zle -N change-surround surround
bindkey -a cs change-surround
bindkey -a ds delete-surround
bindkey -a s add-surround
bindkey -M visual s add-surround
# edit command line in EDITOR
autoload -U edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line
autoload -U zmv
export PDSH_SSH_ARGS='-A'
export LPASS_HOME="$HOME/.config/lpass"
export HOMEBREW_NO_AUTO_UPDATE=1
[ -z "$SSH_TTY" ] || export SSH_AUTH_SOCK=$HOME/.ssh/ssh_auth_sock
[ "$(uname)" = "Linux" ] && export SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/ssh-agent.socket
HISTFILE=$HOME/.zshistory
HISTSIZE=500000
SAVEHIST=500000
alias runapg="apg -M SNcl -m 8 -x 12 -s"
alias mpg123="mpg123 -vC"
alias tmux="tmux -u"
alias s="sudo -E"
alias dog='dog --colour=never'
alias tsz="date -u '+%Y-%m-%dT%H:%M:%SZ'"
[ "$(uname)" = "Darwin" ] && alias sudoedit="sudo -e"
if [ "$(uname)" != "Linux" ]; then
alias mls="stat -f '%A %N'"
else
alias mls="stat -c '%a %n'"
fi
# Python
export VIRTUAL_ENV_DISABLE_PROMPT='1'
export PIPENV_COLORBLIND=true
export PIPENV_HIDE_EMOJIS=true
export PIPENV_NOSPIN=true
export PIPENV_VENV_IN_PROJECT=true
export USE_EMOJI="false"
export PYTHONSTARTUP="$HOME/.pythonstartup"
[ "$(uname)" = "Darwin" ] && export PIPX_DEFAULT_PYTHON="python3.11"
# zoxide
eval "$(zoxide init zsh --cmd j)"
alias jj="ji"
alias jb="j -"
alias ju="j .."
setopt SHARE_HISTORY
setopt HIST_REDUCE_BLANKS
setopt HIST_SAVE_NO_DUPS
setopt HIST_IGNORE_DUPS
setopt NO_HIST_SAVE_BY_COPY
setopt INC_APPEND_HISTORY
setopt EXTENDED_HISTORY
setopt HIST_IGNORE_SPACE
setopt PROMPT_SUBST
WHOAMI=`whoami`
if [[ "$ITERM_PROFILE" != "Hotkey Window" ]]; then
source $HOME/bin/base16-default-light.sh
TMUX_SESSION_NAME="${WHOAMI}-full"
else
TMUX_SESSION_NAME="${WHOAMI}-hotkey"
fi
if [ "$TERM" != "screen-256color" ]\
&& [ "$TERM_PROGRAM" = "iTerm.app" ]\
&& [ "$(uname)" = "Darwin" ]; then
tmux list-sessions -F "#{session_name}:#{session_attached}"\
|grep -q "${TMUX_SESSION_NAME}"\
|| tmux new-session -s "$TMUX_SESSION_NAME"
tmux list-sessions -F "#{session_name}:#{session_attached}"\
|grep -q "${TMUX_SESSION_NAME}:1"\
|| tmux attach-session -t "$TMUX_SESSION_NAME"
fi
eval "$(direnv hook zsh)"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # This loads nvm
omz_kubectl_post_load() {
# extra kubectl aliases missing for the stock OMZ::plugins/kubectl
unalias kl1h
unalias kl1m
unalias kl1s
unalias klf
unalias klf1h
unalias klf1m
unalias klf1s
# Seconds
alias kl1s='k logs --since 1s'
alias kl5s='k logs --since 5s'
alias kl10s='k logs --since 10s'
alias kl30s='k logs --since 10s'
# Seconds + follow
alias kl1sf='k logs --since 1s -f'
alias kl5sf='k logs --since 5s -f'
alias kl10sf='k logs --since 10s -f'
alias kl30sf='k logs --since 10s -f'
# Minutes
alias kl1m='k logs --since 1m'
alias kl5m='k logs --since 5m'
alias kl10m='k logs --since 10m'
alias kl30m='k logs --since 10m'
# Minutes + follow
alias kl1mf='k logs --since 1m -f'
alias kl5mf='k logs --since 5m -f'
alias kl10mf='k logs --since 10m -f'
alias kl30mf='k logs --since 10m -f'
# Hours
alias kl1h='k logs --since 1h'
alias kl5h='k logs --since 5h'
alias kl10h='k logs --since 10h'
# Hours + follow
alias kl1hf='k logs --since 1h -f'
alias kl5hf='k logs --since 5h -f'
alias kl10hf='k logs --since 10h -f'
# PersistentVolumes
alias kgpv='k get pv'
alias kgpva='kgpv --all-namespaces'
alias kgpvw='kgpv --watch'
alias kgpvwa='kgpv --watch --all-namespaces'
alias kgpvwide='kgpv -o wide'
alias kdespv='k describe pv'
alias kdelpv='k delete pv'
}
# zplug on macOS
if [ "$(uname)" = "Darwin" ]; then
export ZPLUG_HOME=$HOMEBREW_PREFIX/opt/zplug
source $ZPLUG_HOME/init.zsh
zplug romkatv/powerlevel10k, as:theme, depth:1
zplug "hlissner/zsh-autopair", defer:2
zplug "zsh-users/zsh-history-substring-search"
zplug "plugins/sudo", from:oh-my-zsh
zplug "plugins/kubectl", from:oh-my-zsh, hook-load:"omz_kubectl_post_load"
zplug "plugins/terraform", from:oh-my-zsh
zplug "johanhaleby/kubetail"
zplug "kovetskiy/zsh-add-params"
zplug load
fi
# zinit on Linux
if [ "$(uname)" = "Linux" ] || [ "$(uname)" = "FreeBSD" ]; then
source "$HOME/.zinit/bin/zinit.zsh"
zinit ice depth=1; zinit light romkatv/powerlevel10k
zinit ice wait lucid; zinit load hlissner/zsh-autopair
zinit ice wait"1"; zinit load zsh-users/zsh-history-substring-search
zinit ice wait"0"; zinit snippet OMZ::plugins/sudo
zinit ice wait"0"; zinit snippet OMZ::plugins/kubectl atload"omz_kubectl_post_load"
zinit ice wait"0"; zinit snippet OMZ::plugins/terraform
zinit ice wait lucid; zinit load kovetskiy/zsh-add-params
zinit ice wait lucid; zinit load johanhaleby/kubetail
fi
# zsh-history-substring-search
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
bindkey '^K' history-substring-search-up
bindkey '^J' history-substring-search-down
# sudo
# reclaim ^S, thanks https://superuser.com/a/385290
stty start undef
stty stop undef
bindkey -r '^[^['
bindkey -M vicmd '^S' sudo-command-line
bindkey -M viins '^S' sudo-command-line
# zsh-add-params
bindkey -v '^P' add-params
bindkey -a '^P' add-params
# create Terraform plugin cache dir if doesn't exist
# https://www.terraform.io/cli/config/config-file#provider-plugin-cache
[ -d "$HOME/.terraform.d/plugin-cache" ] || mkdir -p "$HOME/.terraform.d/plugin-cache"
function _tmuxw() {
[ -z "$TMUX" ] && $* && return
cmd=$1
shift
cur=$(tmux lsw | grep -m1 "[:] ${cmd##*/} $*"|cut -f1 -d:)
if [ -n "$cur" ];then
tmux selectw -t :$cur
else
tmux neww -n "${cmd##*/} $*" "$cmd $*"
fi
}
function ssh() {
_tmuxw =ssh $*
}
function telnet() {
_tmuxw =telnet $*
}
function cu() {
_tmuxw =cu $*
}
# @re
# run command elsewhere (on all .hosts except this one)
function re() {
if [ "$#" -lt "1" ]; then
echo 'ERROR: no command specified'
return
fi
h=$(grep -v "$HOSTNAME" "$HOME/.hosts"|tr '\n' ',')
pdsh -w "${h%?}" "$*"
}
# helpers for go/python q module
qq() {
clear
logpath="$TMPDIR/q"
if [ -z "$TMPDIR" ]; then
logpath="/tmp/q"
fi
if [ ! -f "$logpath" ]; then
echo 'Q LOG' > "$logpath"
fi
tail -100f -- "$logpath"
}
rmqq() {
logpath="$TMPDIR/q"
if [ -z "$TMPDIR" ]; then
logpath="/tmp/q"
fi
if [ -f "$logpath" ]; then
rm "$logpath"
fi
qq
}
# abbrpwd returns abbreviated $PWD if it's within $HOME and full otherwise
function abbrpwd() {
if [ "${PWD##$HOME}" != "$PWD" ]; then
echo "~${PWD##$HOME}"
else
echo $PWD
fi
}
# jf opens directory in Finder.app: $PWD if called without args, $1 otherwise
function jf() {
if [ -z "$1"]; then
open "$PWD"
else
open "$1"
fi
}
# mkpipenv runs pipenv install and adds pipenv layout to .envrc in $PWD
function mkpipenv() {
if [ ! -f "$PWD/Pipfile" ]; then
echo "Pipfile is not present in current directory"
return -1
fi
if grep -q "layout pipenv" .envrc; then
echo "pipenv layout is already enabled in .envrc, quitting"
return -1
fi
echo 'layout pipenv' >> "$PWD/.envrc" && direnv allow
}
# mkpipenv runs pipenv --rm and removes pipenv layout from .envrc
function rmpipenv() {
if [ "$PIPENV_ACTIVE" -ne 1 ] || [ ! -f "$PWD/Pipfile" ]; then
echo "Should be run from root of Pipenv managed project"
return -1
fi
pipenv --rm && sed -i '' -e '/layout pipenv/d' .envrc
}
# cookiecutter-update updates all previously used cookiecutters
function cookiecutter-update() {
ls -1d "$HOME"/.cookiecutters/*|while read -r cc; do
( cd "$cc" && git pull )
done
}
# htmlenc converts UTF-8 text to HTML characters
function htmlenc() {
recode u8..h
}
# htmldec converts HTML characters to UTF-8 text
function htmldec() {
recode h..u8
}
# rename specified <filenames> to <filename>.YYYYMMDDTHHMMSS.bak
function bmv() {
zmv "($1)" '$1.$(date +%Y%m%dT%H%M%S).bak'
}
# copy specified <filenames> to <filename>.YYYYMMDDTHHMMSS.bak
function bcp() {
zmv -C "($1)" '$1.$(date +%Y%m%dT%H%M%S).bak'
}
# pipe the last command through the pager
function pagerlast() {
eval "${history[@]:0:1}|$PAGER"
accept-line
}
zle -N pagerlast{,}
bindkey -v '^R' pagerlast
# copy vimium-options.json to .config dir and chezmoi add it
function save-vimium-config() {
vimiumdir="$HOME/.config/vimium"
vimiumcfg="$HOME/Downloads/vimium-options.json"
[ -d "$vimiumdir" ] || mkdir "$vimiumdir"
if [ -f "$vimiumcfg" ]; then
mv "$vimiumcfg" "$vimiumdir"
chezmoi add "${vimiumdir}/${vimiumcfg##*/}"
else
echo "There is no such file $vimiumcfg"
return 1
fi
}
# shows current versions of installed zplug plugins
function zplug-versions() {
find $ZPLUG_HOME/repos -type d -name ".git"|while read -r plug; do
dir="${plug%%/.git}"
name="${dir#$ZPLUG_HOME/repos/}"
pushd -q $dir
version=$(git rev-parse --short head)
popd -q
printf "$name\t$version\n"
done
}
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# GCP
export USE_GKE_GCLOUD_AUTH_PLUGIN=True
source "/opt/homebrew/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc"