-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdot.zshrc
More file actions
568 lines (457 loc) · 14.9 KB
/
dot.zshrc
File metadata and controls
568 lines (457 loc) · 14.9 KB
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
bindkey -e
typeset -a _startup_funcs=()
# Envs -------------------------------------------------------------------------
if [ -n "$TMUX" ]; then
export TERM=tmux-256color
else
export TERM=xterm-256color
fi
export LANG=en_US.UTF-8
export LC_TIME=ja_JP.UTF-8
export XDG_CONFIG_HOME=$HOME/.config
export LS_COLORS='di=01;36'
export GOPATH=$HOME/code
export CLAUDE_CONFIG_DIR=$HOME/.config/claude
if [ -x /opt/homebrew ]; then
export BREW_PREFIX='/opt/homebrew'
else
export BREW_PREFIX='/nonexistent'
fi
# Completions ------------------------------------------------------------------
# Limit completion suggestions for make to only target names
zstyle ':completion:*:*:make:*' tag-order 'targets'
# Define command lookup paths for sudo completion
zstyle ':completion:*:sudo:*' command-path $BREW_PREFIX/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
# Set color for file list completion like ls
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# If you receive "zsh compinit: insecure directories" warnings when attempting
# to load these completions, you may need to run these commands:
# chmod go-w '/opt/homebrew/share'
# chmod -R go-w '/opt/homebrew/share/zsh'
if [ -d $BREW_PREFIX/share/zsh-completions ]; then
FPATH=$BREW_PREFIX/share/zsh-completions:$FPATH
fi
autoload -Uz compinit
compinit
# PATH -------------------------------------------------------------------------
export PATH=$BREW_PREFIX/bin:$BREW_PREFIX/sbin:$GOPATH/bin:$PATH
export MANPATH=$BREW_PREFIX/share/man:$BREW_PREFIX/man:/usr/share/man
if [ -d $BREW_PREFIX/opt/coreutils ]; then
export PATH="$BREW_PREFIX/opt/coreutils/libexec/gnubin:$PATH"
export MANPATH="$BREW_PREFIX/opt/coreutils/libexec/gnuman:$MANPATH"
fi
if [ -d $BREW_PREFIX/opt/gnu-sed ]; then
export PATH="$BREW_PREFIX/opt/gnu-sed/libexec/gnubin:$PATH"
export MANPATH="$BREW_PREFIX/opt/gnu-sed/libexec/gnuman:$MANPATH"
fi
if [ -d $BREW_PREFIX/opt/gawk ]; then
export PATH="$BREW_PREFIX/opt/gawk/libexec/gnubin:$PATH"
export MANPATH="$BREW_PREFIX/opt/gawk/libexec/gnuman:$MANPATH"
fi
if [ -d ${HOME}/.local/bin ] ; then
export PATH="$HOME/.local/bin:$PATH"
fi
# mise -------------------------------------------------------------------------
if command -v mise >/dev/null 2>&1; then
eval "$(mise activate zsh)"
fi
# fzf --------------------------------------------------------------------------
if command -v fzf >/dev/null 2>&1; then
export FZF_DEFAULT_OPTS="--exact --ansi --cycle --filepath-word --exit-0 \
--preview='fzf-preview file {}' \
--layout=reverse \
--marker='X' \
--history-size=5000 \
--tiebreak=index \
--highlight-line \
--info=inline-right \
--ansi \
--layout=reverse \
--border=none \
--color=bg+:#283457 \
--color=bg:#191a22 \
--color=border:#27a1b9 \
--color=fg:#c0caf5 \
--color=gutter:#16161e \
--color=header:#ff9e64 \
--color=hl+:#2ac3de \
--color=hl:#2ac3de \
--color=info:#545c7e \
--color=marker:#ff007c \
--color=pointer:#ff007c \
--color=prompt:#2ac3de \
--color=query:#c0caf5:regular \
--color=scrollbar:#27a1b9 \
--color=separator:#ff9e64 \
--color=spinner:#ff007c \
--bind=tab:down \
--bind=shift-tab:up \
--bind=ctrl-a:select-all \
--bind=ctrl-l:toggle \
--bind=ctrl-h:toggle \
--bind=ctrl-w:backward-kill-word \
--bind=up:preview-page-up \
--bind=down:preview-page-down \
--bind=ctrl-u:half-page-up \
--bind=ctrl-d:half-page-down"
export FZF_DEFAULT_COMMAND="rg --files --hidden --follow --sort=path \
-g '!**/.DS_Store' \
-g '!**/node_modules' \
-g '!**/__pycache__' \
-g '!**/.bundle' \
-g '!**/.gradle' \
-g '!**/.ssh' \
-g '!**/.venv' \
-g '!**/{.config,dot.config}/**/logs' \
-g '!**/{.config,dot.config}/**/undo' \
-g '!**/{.config,dot.config}/claude/*/**/*' \
-g '!**/.git'"
fi
# Android Studio and Android SDK -----------------------------------------------
if [ -d '/Applications/Android Studio.app/Contents/jre/Contents/Home' ]; then
export JAVA_HOME='/Applications/Android Studio.app/Contents/jre/Contents/Home'
export PATH=$JAVA_HOME/bin:$PATH
fi
if [ -d "$HOME/Library/Android/sdk" ]; then
export ANDROID_SDK_ROOT="$HOME/Library/Android/sdk"
fi
# Neovim -----------------------------------------------------------------------
if command -v nvim >/dev/null 2>&1; then
if [ -n "${NVIM:-}" ]; then
alias vi='echo "already open nvim"'
alias vim='echo "already open nvim"'
alias nvim='echo "already open nvim"'
else
alias vi='nvim'
alias vim='nvim'
fi
alias vimdiff='nvim -d'
export EDITOR='nvim'
else
export EDITOR='vi'
fi
# ssh-key ----------------------------------------------------------------------
_startup-ssh-add-key() {
if [[ ! -o interactive ]] || [ ! -t 0 ]; then
return 0
fi
local key_paths=("$HOME/.ssh/id_ed25519" "$HOME/.ssh/id_rsa")
local keys_to_add=()
for key_path in "${key_paths[@]}"; do
if [ -f "$key_path" ]; then
keys_to_add+=("$key_path")
fi
done
if [ ${#keys_to_add[@]} -eq 0 ]; then
return 0
fi
# Fix SSH_AUTH_SOCK path so all shells share the same agent.
export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
# Check agent connectivity (exit code 2 = cannot connect).
ssh-add -l >/dev/null 2>&1
local ssh_status=$?
if [ "$ssh_status" -eq 2 ]; then
rm -f "$SSH_AUTH_SOCK"
echo "ssh-agent not running. Start ssh-agent? [y/n]"
read -r ans </dev/tty
if [[ "$ans" =~ ^[Yy]$ ]]; then
eval "$(ssh-agent -a "$SSH_AUTH_SOCK")" >/dev/null
else
return 0
fi
fi
local loaded_keys
loaded_keys="$(ssh-add -l 2>/dev/null || true)"
for key_path in "${keys_to_add[@]}"; do
local fp
fp="$(ssh-keygen -lf "$key_path" 2>/dev/null | awk '{print $2}')"
if [ -n "$fp" ] && ! echo "$loaded_keys" | grep -q "$fp"; then
ssh-add "$key_path"
fi
done
}
_startup_funcs+=(_startup-ssh-add-key)
# tmux -------------------------------------------------------------------------
_startup-tmux-main-prompt() {
if [[ ! -o interactive ]] || [ ! -t 0 ]; then
return 0
fi
if [ -n "$TMUX" ]; then
return 0
fi
if ! command -v tmux >/dev/null 2>&1; then
return 0
fi
# Skip if any session has attached clients
local attached
attached="$(tmux list-sessions -F '#{session_attached}' 2>/dev/null | grep -v '^0$' | head -1)"
if [ -n "$attached" ]; then
return 0
fi
echo "You're not in a tmux session. Start tmux-main? [y/n]"
read -r ans </dev/tty
if [[ "$ans" =~ ^[Yy]$ ]]; then
tmux-main
fi
}
_startup_funcs+=(_startup-tmux-main-prompt)
# fcitx5 -----------------------------------------------------------------------
if command -v fcitx5 >/dev/null 2>&1; then
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx
export SDL_IM_MODULE=fcitx
export GLFW_IM_MODULE=ibus
fcitx5 --disable=wayland -d --verbose '*'=0
fi
# Functions --------------------------------------------------------------------
vcs-move-to-repo() {
FZF_PROMPT='MoveTo> ' vcs-select-repo | { read -r s && [ -n "$s" ] && cd "$s"; }
}
vcs-edit-changed-files() {
local files=""
if jj root >/dev/null 2>&1; then
files="$(jj status --no-pager | grep -E '^\w ' | grep -v -E '^D ')"
else
files="$(git status -s -u --no-renames | grep -v -E '^D ')"
fi
if [ -z "$files" ]; then
util-edit-selected-files
else
_edit-files "$(FZF_PROMPT='Edit> ' vcs-select-changed-files $1)"
fi
}
util-edit-selected-files() {
_edit-files "$(FZF_PROMPT='Edit> ' util-select-files $1)"
}
util-edit-grep-results() {
_edit-files "$(FZF_PROMPT='Edit> ' util-select-grep-results $1)"
}
util-select-history() {
BUFFER=$(history -n -r 1 | fzf --no-sort +m --query "$LBUFFER" --prompt 'History> ' --preview="")
CURSOR=$#BUFFER
}
util-remove-history() {
local selected
selected=$(history -n -r 1 | fzf --no-sort +m --prompt 'Remove History> ' --preview="")
if [ -z "$selected" ]; then
return 0
fi
local HISTORY_IGNORE="${(b)selected}"
fc -W
fc -p $HISTFILE $HISTSIZE $SAVEHIST
}
_edit-files() {
if [ "$(echo "$1" | wc -l)" -eq 1 ]; then
file=$(echo "$1" | awk -F: '{print $1}')
print -s "vi $file" && fc -AI
fi
util-edit-files "$1"
}
# Aliases ----------------------------------------------------------------------
alias ls='ls --color=auto'
alias ll='ls -l --block-size=KB'
alias la='ls -A'
alias lal='ls -l -A --block-size=KB'
alias reload-shell='exec $SHELL -l'
alias s='vcs-status'
alias b='vcs-switch-branch'
alias r='vcs-restore-files'
alias t='vcs-stash-files'
alias g='vcs-move-to-repo'
alias v='vcs-edit-changed-files'
alias vv='util-edit-selected-files'
alias a='git-add-files'
alias u='git-unstage-files'
alias mt='git-mergetool-file'
alias gg='util-edit-grep-results'
if command -v bazelisk >/dev/null 2>&1; then
alias bazel='bazelisk'
fi
if command -v bat >/dev/null 2>&1; then
alias cat='bat'
fi
# Prompt -----------------------------------------------------------------------
setopt prompt_subst
ZLE_RPROMPT_INDENT=0
_prompt-pwd() {
local dir="$(print -P '%~')"
dir="${dir%/}" # Remove trailing slash
local icon_repo=$'\Uea62 '
if [[ $dir =~ '^~\/code\/src\/[^/]+\/[^/]+\/(.*)$' ]]; then
echo "$icon_repo${match[1]}"
elif [[ $dir =~ '^~\/(dotfiles\/?.*)$' ]]; then
echo "$icon_repo${match[1]}"
else
echo "$dir"
fi
}
_prompt-vcs-branch() {
if jj root >/dev/null 2>&1; then
local bookmarks="$(jj log -r @ --no-graph -T 'bookmarks' 2>/dev/null)"
if [ -n "$bookmarks" ] && [ "$bookmarks" != "" ]; then
local icon_jj_branch=$'\Ue0a0 '
echo "$icon_jj_branch$bookmarks"
else
local change_id="$(jj log -r @ --no-graph -T 'change_id.short()' 2>/dev/null)"
if [ -n "$change_id" ]; then
local icon_jj_branch=$'\Ue0a0 '
echo "$icon_jj_branch@$change_id"
else
echo ''
fi
fi
else
local branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [ -n "$branch" ]; then
local icon_git_branch=$'\Ue0a0 '
echo "$icon_git_branch$branch"
else
echo ''
fi
fi
}
() {
local icon_cat=$'\Uf011b '
local icon_key=$'\Uf805 '
local icon_folder=$'\Uf450 '
local icon_network=$'\Ufbf1 '
local icon_vim=$'\Ue62b '
local icon_clock=$'\Uf017 '
local left_sep=$'\Ue0b0 '
local primary_bg="#7AA2F7"
local primary_fg="#000000"
local secondary_bg="#3B4261"
local secondary_fg="#7AA2F7"
if [ -n "${NVIM:-}" ]; then
primary_bg="#73DACA"
secondary_fg="#73DACA"
elif [ -n "${REMOTEHOST}${SSH_CONNECTION}" ]; then
primary_bg="#FF9E64"
secondary_fg="#FF9E64"
fi
local prompt_face
if [ -n "${NVIM:-}" ]; then
prompt_face="${icon_vim}"
elif [ "${UID}" -eq 0 ]; then
prompt_face="${icon_key}"
else
prompt_face="${icon_cat}"
fi
local left_segment=""
if [ -n "${REMOTEHOST}${SSH_CONNECTION}" ]; then
left_segment+="%K{$primary_bg}%F{$primary_fg} ${icon_network}${HOST%%.*} %f%k"
else
left_segment+="%K{$primary_bg}%F{$primary_fg} %B${prompt_face}%b%f%k"
fi
left_segment+="%K{$secondary_bg}%F{$primary_bg}${left_sep}%f%k"
left_segment+="%K{$secondary_bg}%F{$secondary_fg}% "'$(_prompt-pwd)'" %f%k"
left_segment+="%F{$secondary_bg}${left_sep}%f"
local right_segment=""
right_segment+="%F{$secondary_fg} "'$(_prompt-vcs-branch)'" %f"
PROMPT="${left_segment}"
RPROMPT="${right_segment}"
PROMPT2="%F{$secondary_fg} > %f "
SPROMPT="%F{$secondary_fg}%r is correct? [n,y,a,e]: %f "
}
# History ----------------------------------------------------------------------
HISTFILE=$HOME/.zsh_history
HISTSIZE=100000
SAVEHIST=100000
autoload history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^P" history-beginning-search-backward-end
bindkey "^N" history-beginning-search-forward-end
zle -N util-select-history
bindkey '^r' util-select-history
_accept-line-with-typo-correction() {
if [[ "$BUFFER" =~ ^gti\ ]]; then
BUFFER="${BUFFER/gti /git }"
fi
zle accept-line
}
zle -N _accept-line-with-typo-correction
bindkey "^M" _accept-line-with-typo-correction
# Utilities --------------------------------------------------------------------
# シェルのプロセスごとに履歴を共有
setopt share_history
# 余分なスペースを削除してヒストリに記録する
setopt hist_reduce_blanks
# ヒストリにhistoryコマンドを記録しない
setopt hist_no_store
# ヒストリを呼び出してから実行する間に一旦編集できる状態になる
setopt hist_verify
# 行頭がスペースで始まるコマンドラインはヒストリに記録しない
setopt hist_ignore_space
# 直前と同じコマンドラインはヒストリに追加しない
setopt hist_ignore_dups
# 重複したヒストリは追加しない
setopt hist_ignore_all_dups
# 補完するかの質問は画面を超える時にのみに行う。
LISTMAX=0
# cdのタイミングで自動的にpushd
setopt auto_pushd
# 複数の zsh を同時に使う時など history ファイルに上書きせず追加
setopt append_history
# 補完候補が複数ある時に、一覧表示
setopt auto_list
# auto_list の補完候補一覧で、ls -F のようにファイルの種別をマーク表示しない
setopt no_list_types
# 補完結果をできるだけ詰める
setopt list_packed
# 補完キー(Tab, Ctrl+I) を連打するだけで順に補完候補を自動で補完
setopt auto_menu
# カッコの対応などを自動的に補完
setopt auto_param_keys
# ディレクトリ名の補完で末尾の / を自動的に付加し、次の補完に備える
setopt auto_param_slash
# ビープ音を鳴らさないようにする
setopt no_beep
# コマンドラインの引数で --prefix=/usr などの = 以降でも補完できる
setopt magic_equal_subst
# ファイル名の展開でディレクトリにマッチした場合末尾に / を付加する
setopt mark_dirs
# 8 ビット目を通すようになり、日本語のファイル名を表示可能
setopt print_eight_bit
# Ctrl+wで、直前の/までを削除する。
WORDCHARS='*?_-.[]~=&;!#$%^(){}<>'
# cd をしたときにlsを実行する
chpwd() { ls }
# ディレクトリ名だけで、ディレクトリの移動をする。
setopt auto_cd
# C-s, C-qを無効にする。
setopt no_flow_control
# Change open files limit and user processes limit.
# See: https://gist.github.com/tombigel/d503800a282fcadbee14b537735d202c
ulimit -n 200000
ulimit -u 2000
# Disable C-d to exit shell and C-z to suspend
_do_nothing() {}
zle -N _do_nothing
bindkey "^D" _do_nothing
setopt IGNORE_EOF
stty susp undef
# Integrations -----------------------------------------------------------------
if command -v jj >/dev/null 2>&1; then
source <(jj util completion zsh)
fi
if [ -n "${GHOSTTY_RESOURCES_DIR}" ]; then
. "${GHOSTTY_RESOURCES_DIR}/shell-integration/zsh/ghostty-integration"
fi
if [ -d $BREW_PREFIX/Caskroom/google-cloud-sdk ]; then
. "$BREW_PREFIX/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc"
fi
if [ -f $HOME/.zshrc.local ]; then
. $HOME/.zshrc.local
fi
typeset -U path PATH # Remove duplicated PATHs.
export PATH=$HOME/dotfiles/bin:$PATH
if command -v direnv >/dev/null 2>&1; then
eval "$(direnv hook zsh)"
fi
# Startup ----------------------------------------------------------------------
for _func in "${_startup_funcs[@]}"; do
$_func
done
unset _func _startup_funcs