-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_zshrc
576 lines (489 loc) · 14.6 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
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
# IMPORTS {{{
[[ -f $HOME/.zshrc.local ]] && source "$HOME/.zshrc.local"
# }}}
# ZCOMPILE {{{
function source {
ensure_zcompiled $1
builtin source $1
}
function ensure_zcompiled {
local compiled="$1.zwc"
local target="$1"
if [[ -L "$1" ]]; then
target=$(readlink "$1")
fi
if [[ ! -r "$compiled" || "$target" -nt "$compiled" ]]; then
echo "Compiling $1"
zcompile $1
fi
}
ensure_zcompiled ~/.zshrc
ensure_zcompiled ~/.zshrc.local
ensure_zcompiled ~/.zprofile
ensure_zcompiled ~/.zshenv
ensure_zcompiled ~/.zshenv.local
() {
local src
for src in $@; do
([[ ! -e $src.zwc ]] || [ ${src:A} -nt $src ]) && zcompile $src
done
} ~/.zshrc ~/.zprofile ~/.zshrc.local
# }}}
# INITIALIZE {{{
bindkey -e
if [[ -n "$ZSHRC_PROFILE" ]]; then
zmodload zsh/zprof && zprof > /dev/null
fi
# }}}
# {{{ ENVVARS
# Setting PATH
#export PYENV_ROOT="$HOME/.pyenv"
#export NODENV_ROOT="$HOME/.nodenv"
# sccache
if exists sccache; then
export SCCACHE_REDIS="redis://localhost:6379/1"
#export RUSTC_WRAPPER="$(which sccache)"
fi
export WORDCHARS='*?_.[]~-=&;!#$%^(){}<>'
export PURE_GIT_PULL=0
# }}}
# {{{ PLUGINS
export ZENO_ENABLE_SOCK=1
export ZENO_ENABLE_FZF_TMUX=1
exists bat && export ZENO_GIT_CAT="bat"
if exists sheldon; then
if [[ ! -e "${HOME}/.sheldon.lock.zsh" ]] || [[ "${XDG_CONFIG_HOME}/sheldon/plugins.toml" -nt "${HOME}/.sheldon.lock.zsh" ]]; then
sheldon source > "${HOME}/.sheldon.lock.zsh"
fi
source "${HOME}/.sheldon.lock.zsh"
fi
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+="zeno-auto-snippet-and-accept-line-fallback"
setopt nonomatch
function set_fast_theme() {
FAST_HIGHLIGHT_STYLES[path]='fg=cyan,underline'
FAST_HIGHLIGHT_STYLES[path-to-dir]='fg=cyan,underline'
FAST_HIGHLIGHT_STYLES[suffix-alias]='fg=blue'
FAST_HIGHLIGHT_STYLES[alias]='fg=blue'
FAST_HIGHLIGHT_STYLES[precommand]='fg=blue'
FAST_HIGHLIGHT_STYLES[command]='fg=blue'
FAST_HIGHLIGHT_STYLES[arg0]='fg=025'
FAST_HIGHLIGHT_STYLES[globbing]='fg=green,bold'
FAST_HIGHLIGHT_STYLES[single-hyphen-option]='fg=cyan'
FAST_HIGHLIGHT_STYLES[double-hyphen-option]='fg=cyan'
FAST_HIGHLIGHT_STYLES[default]='fg=cyan'
FAST_HIGHLIGHT_STYLES[unknown-token]='fg=196'
FAST_HIGHLIGHT_STYLES[builtin]='fg=blue'
FAST_HIGHLIGHT_STYLES[global-alias]='fg=green'
}
export ZSH_AUTOSUGGEST_STRATEGY=(match_prev_cmd history)
export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
export FZF_PREVIEW_ENABLE_TMUX=1
FZF_PREVIEW_DEFAULT_SETTING="--sync --height='80%' --preview-window='right:50%' --expect='ctrl-space' --header='C-Space: continue fzf completion'"
FZF_PREVIEW_DEFAULT_BIND="ctrl-d:preview-page-down,ctrl-u:preview-page-up,?:toggle-preview"
set_fast_theme
# }}}
# KEY {{{
typeset -A key
if [[ -n "${terminfo}" ]]; then
key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
# setup key accordingly
[[ -n "${key[Home]}" ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey "${key[Insert]}" overwrite-mode
[[ -n "${key[Delete]}" ]] && bindkey "${key[Delete]}" delete-char
[[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-history
[[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-history
[[ -n "${key[Left]}" ]] && bindkey "${key[Left]}" backward-char
[[ -n "${key[Right]}" ]] && bindkey "${key[Right]}" forward-char
[[ -n "${key[PageUp]}" ]] && bindkey "${key[PageUp]}" beginning-of-buffer-or-history
[[ -n "${key[PageDown]}" ]] && bindkey "${key[PageDown]}" end-of-buffer-or-history
fi
# word
bindkey "\e[1;5C" forward-word
bindkey "\e[1;5D" backward-word
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
function zle-line-init () {
printf '%s' "${terminfo[smkx]}"
}
function zle-line-finish () {
printf '%s' "${terminfo[rmkx]}"
}
zle -N zle-line-init
zle -N zle-line-finish
fi
function zeno-auto-snippet-fallback() {
if [[ -n "$ZENO_LOADED" ]]; then
zeno-auto-snippet
else
zle self-insert
fi
}
function zeno-auto-snippet-and-accept-line-fallback() {
if [[ -n "$ZENO_LOADED" ]]; then
zeno-auto-snippet-and-accept-line
else
zle accept-line
fi
}
function zeno-insert-snippet-fallback() {
if [[ -n "$ZENO_LOADED" ]]; then
zeno-insert-snippet
fi
}
function zeno-completion-fallback() {
if [[ -n "$ZENO_LOADED" ]]; then
zeno-completion
else
zle expand-or-complete
fi
}
function zeno-history-selection-fallback() {
if [[ -n "$ZENO_LOADED" ]]; then
zeno-history-selection
else
zle history-incremental-search-backward
fi
}
function zeno-ghq-cd-fallback() {
if [[ -n $ZENO_LOADED ]]; then
zeno-ghq-cd
else
zle vi-find-next-char
fi
}
zle -N zeno-auto-snippet-fallback
zle -N zeno-auto-snippet-and-accept-line-fallback
zle -N zeno-insert-snippet-fallback
zle -N zeno-completion-fallback
zle -N zeno-history-selection-fallback
zle -N zeno-ghq-cd-fallback
# zeno.zsh
bindkey ' ' zeno-auto-snippet-fallback
bindkey '^m' zeno-auto-snippet-and-accept-line-fallback
bindkey '^x^s' zeno-insert-snippet-fallback
bindkey '^i' zeno-completion-fallback
bindkey '^r' zeno-history-selection-fallback
bindkey '^x^f' zeno-ghq-cd-fallback
bindkey '\e[Z' reverse-menu-complete
bindkey '^[k' tmk
bindkey '^[t' tms
autoload -z edit-command-line
zle -N edit-command-line
bindkey "^xe" edit-command-line
bindkey "^x^e" edit-command-line
# }}}
# ALIAS {{{
# OS Settings
if is_osx; then
alias arm="exec arch -arch arm64e /bin/zsh --login"
alias x86="exec arch -arch x86_64 /bin/zsh --login"
alias ubuntu="limactl shell docker"
else
alias mozc_dic='/usr/lib/mozc/mozc_tool --mode=dictionary_tool'
alias mozc_word='/usr/lib/mozc/mozc_tool --mode=word_register_dialog'
alias open='xdg-open'
fi
if is_wsl; then
alias pbcopy='/mnt/c/Windows/System32/clip.exe'
elif is_osx; then
:
else
alias pbcopy='xsel --clipboard --input'
fi
# ls alias
if exists lsd; then
alias ls='lsd'
alias ll='lsd -l'
alias la='lsd -la'
alias tree='lsd --tree'
fi
if exists gojq; then
alias jq='gojq'
fi
# Utilities
alias du='du -sh *'
alias filecount='/usr/bin/ls -lF | grep -v / | wc -l'
alias ..='cd ..'
alias q='exit'
alias top='htop'
exists gomi && alias rm='gomi'
# editors
alias vim='nvim'
alias vi='vim'
alias v='vi'
alias vimdiff='nvim -d'
alias vimmerge='nvim -c vnew -c "windo diffthis" -c "setl scrollbind" -c "windo setl buftype=nofile"'
# dotfiles
alias dot='cd ~/.local/share/chezmoi'
alias zshconfig='chezmoi edit ~/.zshrc'
alias cedit='chezmoi edit'
alias vimconfig='chezmoi edit ~/.config/nvim/init/core/init.vim'
alias tmuxconfig='chezmoi edit ~/.tmux.conf'
alias sshconfig='vim ~/.ssh/config'
alias gitconfig='chezmoi edit ~/.config/git/config'
alias reload='source ~/.zshrc'
alias path='echo $path'
alias p='paru'
# TMUX
alias ssh='TERM=xterm ssh'
alias bwp='BITWARDENCLI_APPDATA_DIR=~/.config/Bitwarden\ CLI\ Personal bw'
alias bww='BITWARDENCLI_APPDATA_DIR=~/.config/Bitwarden\ CLI\ Work bw'
globalias() {
if [[ $LBUFFER =~ [A-Z0-9]+$ ]]; then
zle _expand_alias
zle expand-word
zle self-insert
else
zle zeno-auto-snippet
fi
}
# zle -N globalias
# }}}
# GENERAL {{{
setopt AUTO_CD
setopt IGNOREEOF
stty stop undef
stty start undef
# opam configuration
if [[ -r "$HOME/.opam/opam-init/init.zsh" ]]; then
source "$HOME/.opam/opam-init/init.zsh" 2>&1 /dev/null || true
fi
#}}}
#{{{ HISTORY
export HISTORY_IGNORE="q|exit|cd|pwd|dot"
zshaddhistory() {
emulate -L zsh
[[ ${1%%$'\n'} != ${~HISTORY_IGNORE} ]]
}
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_FIND_NO_DUPS
setopt HIST_SAVE_NO_DUPS
setopt HIST_BEEP
setopt HIST_REDUCE_BLANKS
#}}}
# {{{ STYLE
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || . ~/.p10k.zsh
zstyle ':completion:*:default' menu select=2
# 補完関数の表示を強化する
zstyle ':completion:*' verbose yes
zstyle ':completion:*' completer _expand _complete _match _prefix _approximate _list _history
zstyle ':completion:*:messages' format '%F{YELLOW}%d'$DEFAULT
zstyle ':completion:*:warnings' format '%F{RED}No matches for:''%F{YELLOW} %d'$DEFAULT
zstyle ':completion:*:descriptions' format '%F{YELLOW}completing %B%d%b'$DEFAULT
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:descriptions' format '%F{yellow}Completing %B%d%b%f'$DEFAULT
zstyle ':completion:*' matcher-list '' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}'
# マッチ種別を別々に表示
zstyle ':completion:*' group-name ''
zstyle ':prompt:pure:prompt:success' color green
zstyle ':prompt:pure:git:branch' color green
# Disalbe Right Prompt
RPROMPT=""
# }}}
# {{{ GPG-SSH
export GPG_TTY=$(tty)
if ! is_ssh; then
if type "gpg" > /dev/null 2>&1; then
if ! is_wsl; then
if [[ -z $SSH_AUTH_SOCK ]]; then
gpg-connect-agent updatestartuptty /bye >/dev/null
gpgconf --launch gpg-agent
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
fi
fi
fi
fi
# In WSL2
if is_wsl; then
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
if ! ss -a | grep -q $SSH_AUTH_SOCK; then
rm -f $SSH_AUTH_SOCK
wsl2_ssh_pagent_bin="$HOME/.ssh/wsl2-ssh-pagent.exe"
if test -x "$wsl2_ssh_pagent_bin"; then
(setsid nohup socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"$wsl2_ssh_pagent_bin" >/dev/null 2>&1 &)
else
echo >&2 "WARNING: $wsl2_ssh_pagent_bin"
fi
unset wsl2_ssh_pagent_bin
fi
export GPG_AGENT_SOCK=$HOME/.gnupg/S.gpg-agent
if ! ss -a | grep -q $GPG_AGENT_SOCK; then
/usr/bin/rm -rf $GPG_AGENT_SOCK
config_path="C\:/Users/$USERNAME/AppData/Local/gnupg"
wsl2_ssh_pagent_bin="$HOME/.ssh/wsl2-ssh-pagent.exe"
if test -x "$wsl2_ssh_pagent_bin"; then
(setsid nohup socat UNIX-LISTEN:$GPG_AGENT_SOCK,fork EXEC:"$wsl2_ssh_pagent_bin -gpgConfigBasepath ${config_path} -gpg S.gpg-agent" >/dev/null 2>&1 &)
else
echo >&2 "WARNING: $wsl2_ssh_pagent_bin"
fi
unset wsl2_ssh_pagent_bin
fi
fi
# }}}
# {{{ TMUX
autoload -Uz add-zsh-hook
function tmux_ssh_preexec() {
local command=$1
if [[ "$command" = *ssh* ]]; then
tmux setenv TMUX_SSH_CMD_"$(tmux display -p "#I")" "${command}"
fi
}
add-zsh-hook preexec tmux_ssh_preexec
function precmd() {
if [[ -n $TMUX ]]; then
tmux refresh-client -S
fi
}
function f() {
local project dir repository session current_session out
local ghq_command="ghq list -p | sed -e \"s|$HOME|~|\""
local fzf_options_="--expect=ctrl-space --preview='eval bat --paging=never --style=plain --color=always {}/README.md'"
local fzf_command="fzf-tmux ${fzf_options_}"
fzf_command+=" ${FZF_PREVIEW_DEFAULT_SETTING}"
fzf_command+=" --bind='${FZF_PREVIEW_DEFAULT_BIND}'"
local command="${ghq_command} | ${fzf_command}"
out=$(eval "${command}")
dir=$(tail -1 <<< "${out}")
project=${dir/$(ghq root)//}
if [[ $project == "" ]]; then
return 1
fi
if [[ -n ${TMUX} ]]; then
repository=${dir##*/}
session=${repository//./-}
current_session=$(tmux list-sessions | grep 'attached' | cut -d":" -f1)
tmux list-sessions | cut -d":" -f1 | grep -qe "^${session}\$"
local ret=$?
if [[ $ret = 0 ]]; then
local is_duplicate=true
else
local is_duplicate=false
fi
if [[ $current_session =~ ^[0-9]+$ ]]; then
if ! $is_duplicate; then
eval builtin cd "$dir"
tmux rename-session -t "$current_session" "$session"
else
tmux switch-client -t "$session"
fi
else
if ! $is_duplicate; then
eval tmux new-session -d -c "${dir}" -s "${session}"
tmux switch-client -t "$session"
else
tmux switch-client -t "$session"
fi
fi
fi
}
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 [[ -n ${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 [[ -n ${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 '^k' kill-line-or-up-pane
bindkey '^l' right-pane
bindkey '^h' backspace-or-left-pane
bindkey '^j' accept-line-or-down-pane
# }}}
# FUNCTIONS {{{
function vim-startuptime-detail() {
local time_file
time_file=$(mktemp --suffix "_vim_startuptime.txt")
echo "output: $time_file"
time nvim --startuptime "$time_file" -c q
tail -n 1 "$time_file" | cut -d " " -f1 | tr -d "\n" && printf " [ms]\n"
sort -n -k 2 < "$time_file" | tail -n 20
}
function zsh-profiler() {
ZSHRC_PROFILE=1 zsh -i -c zprof
}
function awsp() {
local profile=$(aws configure list-profiles | sort | fzf)
export AWS_PROFILE="$profile"
}
function dockercon() {
ssh -L localhost:23750:/var/run/docker.sock $@
}
function sheldonupdate() {
sheldon lock --update && sheldon source | grep -v "^$" > "${HOME}/.sheldon.lock.zsh"
}
function fk() {
local pid
local fzf_command
if [[ -z $TMUX ]]; then
fzf_command="fzf"
else
fzf_command="fzf-tmux"
fi
pid=$(ps -ef | sed 1d | $fzf_command -m | awk '{print $2}')
if [[ "x$pid" != "x" ]]; then
echo $pid | xargs kill -${1:-9}
fi
}
function envinit() {
echo "export GITHUB_API_TOKEN=$(gh auth token)\nexport HOMEBREW_GITHUB_API_TOKEN=\$GITHUB_API_TOKEN\nexport AQUA_GITHUB_TOKEN=\$GITHUB_API_TOKEN" > ~/.zshenv.local
source ~/.zshenv.local
}
# }}}
# {{{ HOOKS
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
}
# }}}
unfunction source