-
Notifications
You must be signed in to change notification settings - Fork 4
/
.zshrc
594 lines (478 loc) · 14.5 KB
/
.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# zinit {{{
if [[ ! -d $ZPLG_HOME/bin ]]; then
if whence git > /dev/null; then
git clone --depth 10 https://github.com/zdharma-continuum/zinit.git $ZPLG_HOME/bin
fi
fi
if [[ ! -d $ZPFX ]]; then
mkdir -p $ZPFX/bin
mkdir -p $ZPFX/man/man1
fi
if [[ ! -d $ZPLG_HOME/misc ]]; then
mkdir -p $ZPLG_HOME/misc
fi
source $ZPLG_HOME/bin/zinit.zsh
MANPATH="${ZPFX}/man:${MANPATH}"
# }}}
# sync loading {{{
zinit ice depth=1
zinit light romkatv/powerlevel10k
zinit ice depth=1
zinit light zsh-users/zsh-autosuggestions
zinit ice lucid
zinit light woefe/git-prompt.zsh
# zinit ice lucid
# zinit light romkatv/zsh-prompt-benchmark
# fuzzy finder
zinit ice lucid from"gh-r" as"program" mv"fzf -> ${ZPFX}/bin/fzf"
zinit light junegunn/fzf
zinit ice lucid as"program"
zinit snippet 'https://github.com/junegunn/fzf/blob/master/bin/fzf-tmux'
zinit light yukiycino-dotfiles/zsh-show-buffer-stack
zinit ice lucid
zinit snippet ~/.config/tabtab/zsh/__tabtab.zsh
# }}}
# async loading {{{
# PROMPT
zinit ice lucid wait"!0" depth"1" atinit"zpcompinit; zpcdreplay" atload"set_fast_theme"
zinit light zdharma-continuum/fast-syntax-highlighting
# completion
zinit ice lucid wait"0" depth"1" blockf
zinit light zsh-users/zsh-completions
zinit ice lucid wait"0" depth"1" blockf
zinit light yuki-yano/zsh-completions-anyenv
zinit ice lucid wait"0" as"completion"
zinit snippet OMZ::plugins/docker/_docker
zinit ice lucid wait"0" as"completion"
zinit snippet OMZ::plugins/docker-compose/_docker-compose
# git
# zinit ice lucid wait"0" as"program" pick"$ZPFX/bin/git-*" src"etc/git-extras-completion.zsh" make"PREFIX=$ZPFX"
# zinit light tj/git-extras
zinit ice lucid wait"0" depth"1" as"program"
zinit light yuki-yano/zsh-git-sync
zinit ice lucid wait"0" depth"1" as"program" src"tms.plugin.zsh" pick"tms"
zinit light yuki-yano/tms
zinit ice lucid wait"0" depth"1" as"program" src"tmk.plugin.zsh" pick"tmk"
zinit light yuki-yano/tmk
# man
zinit ice lucid wait"0" as"program" mv"fzf* -> ${ZPFX}/man/man1"
zinit snippet 'https://github.com/junegunn/fzf/blob/master/man/man1/fzf.1'
# util
zinit ice lucid wait"0" depth"1"
zinit light yukiycino-dotfiles/fancy-ctrl-z
# }}}
# zsh-autosuggestions {{{
ZSH_AUTOSUGGEST_USE_ASYNC=1
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(accept-line)
# }}}
# fast-syntax-highlighting {{{
function set_fast_theme() {
FAST_HIGHLIGHT_STYLES[alias]='fg=blue'
FAST_HIGHLIGHT_STYLES[suffix-alias]='fg=blue'
FAST_HIGHLIGHT_STYLES[builtin]='fg=blue'
FAST_HIGHLIGHT_STYLES[function]='fg=blue'
FAST_HIGHLIGHT_STYLES[command]='fg=blue'
FAST_HIGHLIGHT_STYLES[precommand]='fg=blue,underline'
FAST_HIGHLIGHT_STYLES[hashed-command]='fg=blue'
FAST_HIGHLIGHT_STYLES[path]='fg=green'
FAST_HIGHLIGHT_STYLES[globbing]='fg=green,bold'
FAST_HIGHLIGHT_STYLES[history-expansion]='fg=green,bold'
}
# }}}
# show-buffer-stack {{{
add-zsh-hook precmd check-buffer-stack
# }}}
# alias {{{
# exa {{{
if whence exa > /dev/null; then
alias ls="exa"
alias ll="exa -lh --git --time-style long-iso"
alias la="exa -alh --git --time-style long-iso"
elif whence gls > /dev/null; then
alias ls='gls --color=auto'
alias ll='ls -lh'
alias la='ls -alh'
else
alias ll='ls -lh'
alias la='ls -alh'
fi
# }}}
# bat {{{
if whence bat > /dev/null; then
export BAT_THEME='gruvbox-dark'
alias bat='bat'
alias c='bat --color=always --style=plain'
fi
# }}}
# sed {{{
if whence gsed > /dev/null; then
alias sed='gsed'
fi
# }}}
# find {{{
if whence gfind > /dev/null; then
alias find='gfind'
fi
# }}}
# du {{{
if whence dust > /dev/null; then
alias du=dust
fi
# }}}
# yq {{{
if whence gojq > /dev/null; then
alias yq='gojq --yaml-input --yaml-output'
fi
# }}}
# }}}
# }}}
# autoload {{{
autoload -Uz add-zsh-hook
autoload -Uz colors; colors
autoload -Uz edit-command-line
autoload -Uz history-search-end
autoload -Uz smart-insert-last-word
autoload -Uz terminfo
# }}}
# Color Definition {{{
DEFAULT="${reset_color}"
RED="${fg[red]}"
GREEN="${fg[green]}"
YELLOW="${fg[yellow]}"
BLUE="${fg[blue]}"
MAGENTA="${fg[magenta]}"
CYAN="${fg[cyan]}"
WHITE="${fg[white]}"
# }}}
# Basic {{{
# default settings
setopt always_last_prompt
setopt append_history
setopt auto_list
setopt auto_menu
setopt auto_param_keys
setopt auto_param_slash
setopt auto_pushd
setopt brace_ccl
setopt complete_aliases
setopt complete_in_word
setopt extended_glob
setopt extended_history
setopt hist_ignore_all_dups
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_no_store
setopt hist_reduce_blanks
setopt hist_save_no_dups
setopt hist_verify
setopt interactive_comments
setopt list_types
setopt long_list_jobs
setopt magic_equal_subst
setopt mark_dirs
setopt menu_complete
setopt multios
setopt no_beep
setopt no_flow_control
setopt no_list_beep
setopt no_no_match
setopt notify
setopt numeric_glob_sort
setopt print_eight_bit
setopt prompt_subst
setopt pushd_ignore_dups
setopt share_history
setopt transient_rprompt
if whence vivid > /dev/null; then
export LS_COLORS="$(vivid generate ~/.config/vivid/themes/catppuccin.yml)"
fi
# history
HISTFILE=${HOME}/.zsh_history
HISTSIZE=100000
SAVEHIST=100000
# C-w target chars
WORDCHARS="*?_-[]~=&!#$%^(){}<>"
# smart-insert-last-word
zstyle :insert-last-word match '*([^[:space:]][[:alpha:]/\\]|[[:alpha:]/\\][^[:space:]])*'
zle -N insert-last-word smart-insert-last-word
# Show details automatically
REPORTTIME=10
# }}}
# Completion {{{
LISTMAX=1000
# Basic Configuration
zstyle ':completion:*' verbose yes
zstyle ':completion:*' menu select=2
zstyle ':completion:*' group-name ''
zstyle ':completion:*' completer _expand _complete _match _prefix _approximate _list
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' special-dirs true
zstyle ':completion:*' auto-description 'Specify: %d'
zstyle ':completion:*' select-prompt %SScrolling Active: Current selection at %p%s
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
# Highlight
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*:messages' format "$YELLOW" '%d' "$DEFAULT"
zstyle ':completion:*:warnings' format "$RED" 'No matches for:' "$YELLOW" '%d' "$DEFAULT"
zstyle ':completion:*:descriptions' format "$YELLOW" 'Completing %B%d%b' "$DEFAULT"
zstyle ':completion:*:corrections' format "$YELLOW" '%B%d% ' "$RED" '(Errors: %e)%b' "$DEFAULT"
# Separator
zstyle ':completion:*' list-separator ' ==> '
zstyle ':completion:*:manuals' separate-sections true
# Cache
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path "$HOME/.zsh/cache"
# Others
zstyle ':completion:*' remote-access false
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
# }}}
# fzf {{{
# Project
function f() {
local project dir repository session current_session
dir=$(ghq list -p | sed -e "s|${HOME}|~|" | fzf-tmux -p 70%,70% --prompt='Project> ' --preview "bat \$(eval echo {})/README.md" --bind ctrl-d:preview-page-down,ctrl-u:preview-page-up --no-separator)
if [[ $dir == "" ]]; then
return 1
fi
if [[ ! -z ${TMUX} ]]; then
repository=${dir##*/}
session=${repository//./-}
current_session=$(tmux list-sessions | grep 'attached' | cut -d":" -f1)
if [[ $current_session =~ ^[0-9]+$ ]]; then
eval cd "${dir}"
tmux rename-session $session
else
tmux list-sessions | cut -d":" -f1 | grep -e "^${session}\$" > /dev/null
if [[ $? != 0 ]]; then
tmux new-session -d -c $(eval echo "${dir}") -s $session
fi
tmux switch-client -t $session
fi
else
eval cd "${dir}"
fi
}
# }}}
# Prompt {{{
local grey='246'
local red='1'
local yellow='3'
local blue='4'
local magenta='5'
local cyan='6'
local white='7'
function prompt_anyenv() {
local ruby_version python_version node_version
if which rbenv > /dev/null 2>&1; then
ruby_version="Ruby-$(rbenv version-name)"
fi
if which pyenv > /dev/null 2>&1; then
if [[ -n "$VIRTUAL_ENV" ]]; then
python_version="Python-venv"
else
python_version="Python-$(pyenv version-name)"
fi
fi
if which nodenv > /dev/null 2>&1; then
node_version="Node-$(nodenv version-name)"
fi
p10k segment -f white -t "[%{$MAGENTA%}${ruby_version}%{$DEFAULT%} %{$GREEN%}${python_version}%{$DEFAULT%} %{$BLUE%}${node_version}%{$DEFAULT%}]"
}
function prompt_venv() {
local venv
venv=""
if [[ -n "$VIRTUAL_ENV" ]]; then
venv="venv:$(basename $VIRTUAL_ENV)"
p10k segment -f white -t "[%F{72}${venv}%f]"
fi
}
function prompt_rebasing() {
if [[ -d ".git/rebase-merge" ]] || [[ -d ".git/rebase-apply" ]]; then
p10k segment -f red -e -t ' Rebasing '
fi
}
function prompt_conflicting() {
if [[ -f ".git/MERGE_HEAD" ]]; then
p10k segment -f red -e -t ' Conflicting '
fi
}
function prompt_show_buffer_stack() {
p10k segment -f white -e -t '$COMMAND_BUFFER_STACK'
}
typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
dir
vcs
conflicting
rebasing
newline
venv
prompt_char
)
typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
show_buffer_stack
)
function p10k-on-pre-prompt() { p10k display '1'=show }
function p10k-on-post-prompt() { p10k display '1'=hide }
typeset -g POWERLEVEL9K_BACKGROUND=
typeset -g POWERLEVEL9K_{LEFT,RIGHT}_{LEFT,RIGHT}_WHITESPACE=
typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SUBSEGMENT_SEPARATOR=
typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SEGMENT_SEPARATOR=
typeset -g POWERLEVEL9K_VISUAL_IDENTIFIER_EXPANSION=
typeset -g POWERLEVEL9K_PROMPT_ADD_NEWLINE=true
typeset -g POWERLEVEL9K_PROMPT_CHAR_OK_{VIINS,VICMD,VIVIS}_FOREGROUND=$white
typeset -g POWERLEVEL9K_PROMPT_CHAR_ERROR_{VIINS,VICMD,VIVIS}_FOREGROUND=$red
typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIINS_CONTENT_EXPANSION='$'
typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VICMD_CONTENT_EXPANSION='$'
typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIVIS_CONTENT_EXPANSION='$'
typeset -g POWERLEVEL9K_DIR_FOREGROUND=$blue
typeset -g POWERLEVEL9K_VCS_CONTENT_EXPANSION='$(gitprompt)'
ZSH_THEME_GIT_PROMPT_PREFIX=""
ZSH_THEME_GIT_PROMPT_SUFFIX=" "
ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
ZSH_THEME_GIT_PROMPT_DETACHED="%{$fg_bold[cyan]%}:"
ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[white]%} "
ZSH_THEME_GIT_PROMPT_UPSTREAM_PREFIX="%{$fg[magenta]%}->%{$fg[cyan]%}"
ZSH_THEME_GIT_PROMPT_UPSTREAM_SUFFIX=" "
ZSH_THEME_GIT_PROMPT_BEHIND="%{$fg_bold[cyan]%}↓ "
ZSH_THEME_GIT_PROMPT_AHEAD="%{$fg_bold[cyan]%}↑ "
ZSH_THEME_GIT_PROMPT_UNMERGED=" %{$fg[red]%}X:"
ZSH_THEME_GIT_PROMPT_STAGED=" %{$fg[green]%}M:"
ZSH_THEME_GIT_PROMPT_UNSTAGED=" %{$fg[red]%}M:"
ZSH_THEME_GIT_PROMPT_UNTRACKED=" %{$fg[red]%}?:"
ZSH_THEME_GIT_PROMPT_CLEAN=" %{$fg_bold[green]%}✔ "
ZSH_THEME_GIT_PROMPT_STASHED=" %{$fg[blue]%}Stash:"
ZSH_GIT_PROMPT_SHOW_UPSTREAM=full
ZSH_GIT_PROMPT_SHOW_STASH=1
ZSH_GIT_PROMPT_FORCE_BLANK=1
# }}}
# tmux {{{
function _left-pane() {
tmux select-pane -L
}
zle -N left-pane _left-pane
function _down-pane() {
tmux select-pane -D
}
zle -N down-pane _down-pane
function _up-pane() {
tmux select-pane -U
}
zle -N up-pane _up-pane
function _right-pane() {
tmux select-pane -R
}
zle -N right-pane _right-pane
function _backspace-or-left-pane() {
if [[ $#BUFFER -gt 0 ]]; then
zle backward-delete-char
elif [[ ! -z ${TMUX} ]]; then
zle left-pane
fi
}
zle -N backspace-or-left-pane _backspace-or-left-pane
function _kill-line-or-up-pane() {
if [[ $#BUFFER -gt 0 ]]; then
zle kill-line
elif [[ ! -z ${TMUX} ]]; then
zle up-pane
fi
}
zle -N kill-line-or-up-pane _kill-line-or-up-pane
function _accept-line-or-down-pane() {
if [[ $#BUFFER -gt 0 ]]; then
zle accept-line
elif [[ ! -z ${TMUX} ]]; then
zle down-pane
fi
}
zle -N accept-line-or-down-pane _accept-line-or-down-pane
# }}}
# Bindkey {{{
bindkey -e
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
# My ZLE bind
bindkey '^]' insert-last-word
bindkey "^[u" redo
bindkey '^[f' forward-word
bindkey "^[b" backward-word
# Add tmux bind
bindkey '^h' backspace-or-left-pane
bindkey '^j' accept-line-or-down-pane
# common bind
bindkey '^a' beginning-of-line
bindkey '^b' backward-char
bindkey '^d' delete-char-or-list
bindkey '^e' end-of-line
bindkey '^f' forward-char
bindkey '^g' send-break
bindkey '^k' kill-line-or-up-pane
bindkey '^w' backward-kill-word
bindkey '^p' history-beginning-search-backward
bindkey '^n' history-beginning-search-forward
bindkey '^y' yank
bindkey '^q' show-buffer-stack
bindkey "$terminfo[kcbt]" reverse-menu-complete
# Completion bind
zmodload zsh/complist
bindkey -M menuselect '^g' .send-break
bindkey -M menuselect '^i' forward-char
bindkey -M menuselect '^j' .accept-line
bindkey -M menuselect '^k' accept-and-infer-next-history
bindkey -M menuselect '^n' down-line-or-history
bindkey -M menuselect '^p' up-line-or-history
bindkey -M menuselect '^h' undo
# Command Line Edit
zle -N edit-command-line
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line
# }}}
# Misc {{{
# Auto execute rehash when executing anyenv command
add-zsh-hook preexec env_rehash
function env_rehash() {
if echo "$1" | grep rbenv > /dev/null ; then
rbenv rehash
elif echo "$1" | grep pyenv > /dev/null ; then
pyenv rehash
elif echo "$1" | grep nodenv > /dev/null ; then
nodenv rehash
fi
}
# Automatically save the current git state to reflog
add-zsh-hook preexec git_auto_save
function git_auto_save() {
if [[ -d .git ]] && [[ -f .git/auto-save ]] && [[ $(find .git/auto-save -mmin -$((60)) | wc -l) -eq 0 ]]; then
if [[ ! -f ".git/MERGE_HEAD" ]] && [[ $(git --no-pager diff --cached | wc -l) -eq 0 ]] && [[ ! -f .git/index.lock ]] && [[ ! -d .git/rebase-merge ]] && [[ ! -d .git/rebase-apply ]]; then
touch .git/auto-save && git add --all && git commit --no-verify --message "Auto save: $(date -R)" >/dev/null && git reset HEAD^ >/dev/null
echo "Git auto save!"
fi
fi
}
# }}}
# fzf {{{
export FZF_DEFAULT_COMMAND='rg --files --hidden --follow --glob "!.git/*"'
export FZF_DEFAULT_OPTS='--reverse --no-separator'
export FZF_COMPLETION_TRIGGER=';'
# }}}
# Profile {{{
if whence zprof > /dev/null ;then
zprof | less
fi
# }}}
# zcompile {{{
# .zshrc
if [[ ! -f ~/.zshrc.zwc ]] || [[ "$(readlink ~/.zshrc)" -nt ~/.zshrc.zwc ]]; then
zcompile ~/.zshrc
fi
# zinit
# if [[ ! -f ~/.zinit/bin/zinit.zsh.zwc ]] || [[ ~/.zinit/bin/zinit.zsh -nt ~/.zinit/bin/zinit.zsh.zwc ]]; then
# zcompile ~/.zinit/bin/zinit.zsh
# fi
# }}}
# Load local {{{
[[ -f ~/.zshrc.local ]] && source ~/.zshrc.local
# }}}
# vim:set expandtab shiftwidth=2 softtabstop=2 tabstop=2 foldenable foldmethod=marker: