-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdot-zshrc
executable file
·488 lines (431 loc) · 15.4 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
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
# BENCHMARKING
# zmodload zsh/zprof
#----------------------------------------------------------------------------
## Interactive shell ##
skip_global_compinit=1
zmodload zsh/mathfunc
autoload -Uz compaudit compinit run-help
if [[ -n ${ZDOTDIR}/.zcompdump(#qN.mh+24) ]]; then
compinit;
else
compinit -C;
fi
autoload -Uz run-help-git
export HELPDIR=/usr/local/share/zsh/helpfiles
# search on zshall man page
zman() { MANPAGER="less -g -s '+/^ "$1"'" man zshall }
autoload -Uz zmv
alias mmv='noglob zmv -W'
# Path to your oh-my-zsh configuration.
ZSH=$HOME/.oh-my-zsh
# Nicer history
HISTSIZE=100000
SAVEHIST=$HISTSIZE
REPORTTIME=10
#WORDCHARS=${WORDCHARS//[&=\/;\!#%\{]}
WORDCHARS='*?_[]~=&;!#$%^(){}'
HISTFILE=~/.zsh_history
# Uncomment following line if you want to disable autosetting terminal title.
DISABLE_AUTO_TITLE="true"
# Uncomment following line if you want red dots to be displayed while waiting for completion
COMPLETION_WAITING_DOTS="true"
#-------------------------------------------------------------- gcloud sdk ---
# The next line updates PATH for the Google Cloud SDK.
[[ -f "${HOME}/prj/google-cloud-sdk/path.zsh.inc" ]] && source "${HOME}/prj/google-cloud-sdk/path.zsh.inc"
# The next line enables shell command completion for gcloud.
[[ -f "${HOME}/prj/google-cloud-sdk/completion.zsh.inc" ]] && source "${HOME}/prj/google-cloud-sdk/completion.zsh.inc"
#----------------------------------------------------------------- plugins ---
plugins=(
1password
aliases
cp colored-man-pages command-not-found common-aliases copybuffer
dirpersist
history
jsontools
sudo
z
)
# plugins for binaries
for p in aws emacs fzf gcloud docker docker-compose gem git \
gpg-agent mise mix pyenv rails redis-cli rsync \
stack starship tig tmux yum
do
(( $+commands[$p] )) && plugins+=($p)
done
# conditional plugins
if (( $+commands[hostname] )); then
[[ `hostname` != "ip-172-31-37-67" ]] && plugins+=(ssh-agent)
elif (( $+commands[hostnamectl] )); then
[[ `hostnamectl hostname` != "ip-172-31-37-67" ]] && plugins+=(ssh-agent)
fi
(( $+commands[brew] )) && plugins+=(brew macos gnu-utils)
(( $+commands[bundle] )) && plugins+=(bundler)
(( $+commands[dpkg] )) && plugins+=(ubuntu)
(( $+commands[git] )) && plugins+=(git-escape-magic github gitignore)
(( $+commands[go] )) && plugins+=(golang)
[[ -d "$HOME/.nvm" ]] && plugins+=(nvm)
(( $+commands[pacman] )) && plugins+=(archlinux)
(( $+commands[rake] )) && plugins+=(rake-fast)
(( $+commands[systemctl] )) && plugins+=(systemd)
# zshlovers(https://grml.org/zsh/zsh-lovers.html)
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle -e ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'
zstyle ':completion:*:functions' ignored-patterns '_*'
# loadup ssh-agent (<3)
if (( $+commands[hostname] )); then
# TODO: change to uname ?
if [[ `hostname` != "ip-172-31-37-67" ]]; then
zstyle :omz:plugins:ssh-agent agent-forwarding yes
zstyle :omz:plugins:ssh-agent ssh-add-args --apple-load-keychain
zstyle :omz:plugins:ssh-agent identities id_ed25519 id_rsa github_rsa
zstyle :omz:plugins:ssh-agent lazy yes
zstyle :omz:plugins:ssh-agent quiet yes
fi
elif (( $+commands[hostnamectl] )); then
if [[ `hostnamectl hostname` != "ip-172-31-37-67" ]]; then
zstyle :omz:plugins:ssh-agent agent-forwarding yes
zstyle :omz:plugins:ssh-agent identities id_ed25519 id_rsa
zstyle :omz:plugins:ssh-agent lazy yes
fi
fi
# .ssh/config FTW!
if [[ -r ~/.ssh/config ]]; then
h=($h $(awk '/^[hH]ost/ {for(i=2;i<=NF;++i) print $i}' ~/.ssh/config))
if [[ $#h -gt 0 ]]; then
zstyle ':completion:*:ssh:*' hosts $h
zstyle ':completion:*:slogin:*' hosts $h
fi
unset h
fi
unset SCRIPT_NAME
#---------------------------------------------------------------- load OMZ ---
[[ -s "$ZSH/oh-my-zsh.sh" ]] && source $ZSH/oh-my-zsh.sh
# unalias fd
# set these after omz
(( $+commands[bat] )) && export MANPAGER="sh -c 'col -bx | bat -l man -p'"
# export MANROFFOPT="-c"
export BAT_THEME="Catppuccin Mocha"
export BAT_STYLE="plain"
export LESSCHARSET=utf-8
export LESS="-FRmX"
export PAGER="less ${LESS}"
export EDITOR=$( (( $+commands[nvim] )) && echo nvim || echo vim )
export ERL_AFLAGS="-kernel shell_history enabled"
#----------------------------------------------------------------- aliases ---
# TMUX
unalias t
alias t=tmux
compdef _tmux t
alias c='. ccpp'
alias ls='ls --color=auto --group-directories-first'
alias ri='ri -Tf ansi'
alias rm='rm -I'
alias tree='tree -SA'
alias gd="git difftool --no-prompt --tool=mvimdiff"
alias gs='git status -sb'
alias od='od -Ax -tx1z'
alias hexdump='hexdump -C'
alias where="command -v"
alias mv='nocorrect mv -v'
alias cp='nocorrect cp -v'
alias mkdir='nocorrect mkdir'
alias burniso='hdiutil makehybrid -iso -joliet -o $1.iso $2'
alias lg="lazygit --use-config-file=~/.config/lazygit/config.yml"
export LG_CONFIG_FILE="$HOME/.config/lazygit/config.yml"
alias icat='kitty +kitten icat'
(( `uname` == darwin )) && alias lscpu="system_profiler SPHardwareDataType"
#alias ssh='TERM=xterm ssh'
#alias ssh="kitty +kitten ssh"
alias yt="yt-dlp -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4'"
alias afd='aws --profile akad_front_dev'
alias afp='aws --profile akad_front_prod'
alias abp='aws --profile akad_back_prod'
alias afh='aws --profile akad_front_homol'
alias ass='aws --profile akad_shared_services'
#[ -f /usr/local/bin/bat -o -f /bin/bat ] && alias cat='bat -pp'
(( $+commands[defaults] )) && alias dockspaceleft='defaults write com.apple.dock persistent-apps -array-add '\''{tile-data={}; tile-type="spacer-tile";}'\''; killall Dock'
(( $+commands[defaults] )) && alias dockspaceright='defaults write com.apple.dock persistent-others -array-add '\''{tile-data={}; tile-type="spacer-tile";}'\''; killall Dock'
[[ -x /usr/bin/fdfind ]] && alias fd=fdfind
[[ $USER == ubuntu ]] && alias aws-cli='docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli'
(( $+commands[nvim] )) && alias vim=nvim
(( $+commands[tree] )) && alias tree='tree --gitignore'
(( $+commands[entr] )) && alias mt='find apps/*/lib apps/*/test lib test | entr -cdp mix test --color'
(( $+commands[yazi] )) && alias ya=yazi
# function ya() {
# local tmp="$(mktemp -t "yazi-cwd.XXXXX")"
# yazi "$@" --cwd-file="$tmp"
# if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
# cd -- "$cwd"
# fi
# rm -f -- "$tmp"
# }
#---------------------------------------------------------- global aliases ---
alias -g A="| awk"
alias -g G="| grep"
alias -g GV="| grep -v"
alias -g H="| head"
alias -g L="| $PAGER"
alias -g P=' --help | less'
alias -g R="| ruby -e"
alias -g S="| sed"
alias -g T="| tail"
alias -g V="| vim -R -"
alias -g U=' --help | head'
alias -g W="| wc"
alias -g Z='| fzf'
#---------------------------------------------------------- suffix aliases ---
alias -s zip=zipinfo
alias -s tgz=gzcat
alias -s gz=gzcat
alias -s tbz=bzcat
alias -s bz2=bzcat
alias -s java=vim
alias -s c=vim
alias -s h=vim
alias -s C=vim
alias -s cpp=vim
alias -s php=vim
# alias -s py=vim
# alias -s rb=vim
alias -s txt=vim
alias -s xml=vim
#-------------------------------------------------------------- EMACS emul ---
bindkey -e
bindkey '\e[1;3C' forward-word
bindkey '\e[1;3D' backward-word
bindkey '\e\e[C' forward-word
bindkey '\e\e[D' backward-word
#------------------------------------------------------ History completion ---
autoload -Uz history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey '\eOA' up-line-or-beginning-search
bindkey '\eOB' down-line-or-beginning-search
bindkey '^P' up-line-or-beginning-search
bindkey '^N' down-line-or-beginning-search
# edit command line in $EDITOR
autoload -Uz edit-command-line
zle -N edit-command-line
bindkey "^X^E" edit-command-line
#---------------------------------------------------------------- ZSH opts ---
setopt auto_cd
setopt auto_list
setopt auto_param_keys
setopt auto_param_slash
setopt auto_pushd
setopt auto_resume
setopt brace_ccl
#setopt complete_aliases
setopt extended_glob
setopt extended_history
setopt hash_cmds
setopt hist_expand
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_no_store
setopt hist_reduce_blanks
setopt inc_append_history
setopt list_packed
setopt list_rows_first
setopt list_types
setopt long_list_jobs
setopt magic_equal_subst
setopt mark_dirs
setopt multios
setopt no_beep
setopt no_hup
#setopt no_clobber
setopt no_menu_complete
setopt numeric_glob_sort
setopt path_dirs
setopt print_eight_bit
setopt pushd_ignore_dups
setopt pushd_minus
setopt pushd_silent
setopt rm_star_wait
setopt share_history
setopt transient_rprompt
unsetopt promptcr
unsetopt correctall
unsetopt hist_verify
unsetopt print_exit_value
#--------------------------------------------------------------------- FZF ---
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
[ -f ~/bin/fzf-git.sh ] && source ~/bin/fzf-git.sh
# Setting fd as the default source for fzf
# export FZF_DEFAULT_COMMAND="fdfind --hidden --follow --exclude '.git' --exclude 'node_modules'"
BACKGROUND="#232136"
FOREGROUND="#e0def4"
BG_PLUS="#393552"
BORDER="#9ccfd8"
MARKER="#eb6f92"
INFO="#eb6f92"
HL="#f6c177"
LABEL="#6e6a86"
PROMPT="#3e8fb0"
FZF_THEME="
--color=fg:${FOREGROUND},bg:${BACKGROUND},hl:${HL}
--color=fg+:bold:${FOREGROUND},bg+:${BG_PLUS},hl+:${HL}
--color=border:${BORDER},header:${INFO},gutter:${BACKGROUND}
--color=spinner:${HL},info:${INFO},separator:${BACKGROUND}
--color=pointer:bold:${HL},marker:${MARKER},prompt:${PROMPT}
--color=preview-fg:${FOREGROUND},preview-bg:${BACKGROUND}
--color=label:${LABEL}"
export FZF_DEFAULT_OPTS="\
-m --cycle --preview-window wrap --no-height \
--preview '([[ -f {} ]] && (bat --style=numbers --color=always {} || cat {})) || ([[ -d {} ]] && (tree -C {} | less)) || echo {} 2> /dev/null | head -200'"
export FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS} ${FZF_THEME}"
export FZF_CTRL_T_OPTS="
--walker-skip .git,node_modules,target
--preview 'bat -n --color=always {}'
--bind 'ctrl-/:change-preview-window(down|hidden|)'"
# export FZF_ALT_C_COMMAND="fdfind --hidden --follow --exclude '.git' --exclude 'node_modules' --type d ."
export FZF_ALT_C_OPTS="
--walker-skip .git,node_modules,target
--preview 'tree -C {}'"
export FZF_CTRL_R_OPTS="--bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort' --color header:italic --header 'Press CTRL-Y to copy command into clipboard'"
# for more info see fzf/shell/completion.zsh
_fzf_compgen_path() {
fdfind . "$1"
}
_fzf_compgen_dir() {
fdfind --type d . "$1"
}
# like normal z when used with arguments but displays an fzf prompt when used without.
unalias z 2> /dev/null
alias z=zshz
zz() {
local dir=$(
zshz 2>&1 |
fzf --height 40% --layout reverse --info inline \
--nth 2.. --tac --no-sort --query "$*" \
--bind 'enter:become:echo {2..}'
) && cd "$dir"
}
fzfrmimage() {
docker image rm $(docker image ls | tail -n +2 | fzf | while read l; do echo $l | awk '{printf "%s:%s ", $1,$2}'; done)
}
fzfrmservice() {
docker service rm $(docker service ls | tail -n +2 | fzf | while read l; do echo $l | awk '{printf "%s ", $2}'; done)
}
fzfbrew() {
brew rm `brew ls | fzf --preview='brew info {}'`
}
fzffn() {
declare -f |
perl -0 -pe 's/^}\n/}\n\0/gm' |
bat --plain --language bash --color always |
fzf --read0 --ansi --layout reverse --multi --highlight-line
}
fzfrg() {
rg --pretty $* |
perl -0 -pe 's/\n\n/\n\0/gm' |
fzf --read0 --ansi --multi --highlight-line --layout reverse |
perl -ne '/^([0-9]+:|$)/ or print'
}
# ripgrep->fzf->vim [QUERY]
rfv() (
RELOAD='reload:rg --column --color=always --smart-case {q} || :'
OPENER='if [[ $FZF_SELECT_COUNT -eq 0 ]]; then
$EDITOR {1} +{2} # No selection. Open the current line in Vim.
else
$EDITOR +cw -q {+f} # Build quickfix list for the selected items.
fi'
fzf --disabled --ansi --multi \
--bind "start:$RELOAD" --bind "change:$RELOAD" \
--bind "enter:become:$OPENER" \
--bind "ctrl-o:execute:$OPENER" \
--bind 'alt-a:select-all,alt-d:deselect-all,ctrl-/:toggle-preview' \
--delimiter : \
--preview 'bat --style=full --color=always --highlight-line {2} {1}' \
--preview-window '~4,+{2}+4/3,<80(up)' \
--query "$*"
)
#-----------------------------------------------------------------------------
listen() {
lsof -nP -i":$1" | grep LISTEN
}
nstat() {
sudo lsof -iTCP -sTCP:LISTEN -n -P | awk 'NR>1 {print $9, $1, $2}' | sed 's/.*://' | while read port process pid
do echo "Port $port: $(ps -p $pid -o command= | sed 's/^-//') (PID: $pid)"
done | sort -n
}
#-----------------------------------------------------------------------------
n ()
{
# Block nesting of nnn in subshells
if [ -n $NNNLVL ] && [ "${NNNLVL:-0}" -ge 1 ]; then
echo "nnn is already running"
return
fi
# The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set)
# To cd on quit only on ^G, remove the "export" as in:
# NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd"
# NOTE: NNN_TMPFILE is fixed, should not be modified
NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd"
# Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn
# stty start undef
# stty stop undef
# stty lwrap undef
# stty lnext undef
#export ICONLOOKUP=1
export NNN_PLUG='f:fzcd;o:fzopen;p:mocplay;d:diffs;t:preview-tui;v:preview-tabbed'
nnn -a -A -R -Te -Pt "$@"
if [ -f "$NNN_TMPFILE" ]; then
. "$NNN_TMPFILE"
rm -f "$NNN_TMPFILE" > /dev/null
fi
}
#------------------------------------------------------------ chpwd pyvenv ---
python_venv() {
MYVENV=./venv
[[ -d $MYVENV ]] && source $MYVENV/bin/activate
[[ ! -d $MYVENV ]] && deactivate > /dev/null 2>&1
}
autoload -U add-zsh-hook
add-zsh-hook chpwd python_venv
python_venv
#--------------------------------------------------------- starship prompt ---
eval "$(starship init zsh)"
#------------------------------------------------------------- emacs vterm ---
if [[ "$INSIDE_EMACS" = 'vterm' ]]; then
vterm_printf() {
if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ]); then
# Tell tmux to pass the escape sequences through
printf "\ePtmux;\e\e]%s\007\e\\" "$1"
elif [ "${TERM%%-*}" = "screen" ]; then
# GNU screen (screen, screen-256color, screen-256color-bce)
printf "\eP\e]%s\007\e\\" "$1"
else
printf "\e]%s\e\\" "$1"
fi
}
alias clear='vterm_printf "51;Evterm-clear-scrollback";tput clear'
vterm_prompt_end() {
vterm_printf "51;A$(whoami)@$(hostname):$(pwd)"
}
vterm_cmd() {
local vterm_elisp
vterm_elisp=""
while [ $# -gt 0 ]; do
vterm_elisp="$vterm_elisp""$(printf '"%s" ' "$(printf "%s" "$1" | sed -e 's|\\|\\\\|g' -e 's|"|\\"|g')")"
shift
done
vterm_printf "51;E$vterm_elisp"
}
f() { vterm_cmd find-file "$(realpath "${@:-.}")" }
say() { vterm_cmd message "%s" "$*" }
autoload -U add-zsh-hook
add-zsh-hook -Uz chpwd (){ print -Pn "\e]2;%m:%2~\a" }
setopt PROMPT_SUBST
PROMPT=$PROMPT'%{$(vterm_prompt_end)%}'
fi
[[ -f $HOME/lscolors.sh ]] && source $HOME/lscolors.sh
# carapace autocomplete
(( $+commands[carapace] )) && source <(carapace _carapace zsh)
#----------------------------------------------------------------------------
# BENCHMARKING (END)
# zprof