-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path.zshrc
More file actions
387 lines (326 loc) · 11 KB
/
.zshrc
File metadata and controls
387 lines (326 loc) · 11 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
## Environment
export LANG=ja_JP.UTF-8
export EDITOR=vim
## Shell options
setopt auto_cd # cd by typing directory name
setopt auto_pushd # push directories to stack on cd
setopt pushd_ignore_dups # no duplicate entries in directory stack
setopt nocorrect # disable command auto-correction
setopt nonomatch # pass unmatched globs as literal strings
setopt list_packed # compact completion list
setopt noautoremoveslash # keep trailing slash on completions
setopt nolistbeep no_beep # disable all beep sounds
setopt ignoreeof # disable Ctrl+D shell exit
setopt interactivecomments # allow # comments in interactive shell
setopt extended_glob # enable extended glob patterns
unsetopt promptcr # allow output without trailing newline
case ${OSTYPE} in
linux*) export GLFW_IM_MODULE=ibus ;;
esac
umask 002
## History
HISTFILE=~/.zsh_history
HISTSIZE=1000000
SAVEHIST=$HISTSIZE
setopt hist_ignore_dups # ignore consecutive duplicate commands
setopt hist_ignore_all_dups # remove older duplicate entries
setopt hist_reduce_blanks # strip extra whitespace from commands
setopt hist_verify # confirm history expansion before executing
setopt share_history # share history across sessions
## Keybindings
stty -ixon # disable XON/XOFF to allow ^S for history search
bindkey -e # emacs key bindings
# ^P/^N: history search matching current line prefix
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
export WORDCHARS="" # treat symbols as word delimiters
bindkey '^I' complete-word # Tab: complete word
case ${OSTYPE} in
darwin*)
bindkey "^[[1;3C" forward-word # Option+Right: forward word
bindkey "^[[1;3D" backward-word # Option+Left: backward word
;;
esac
## Completion
setopt complete_aliases # enable completion for aliased commands
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*' group-name ''
## Aliases
# ls color flag differs by OS (-G for macOS, --color=auto for Linux)
case ${OSTYPE} in
darwin*) LS_COLOR=-G ;;
linux*) LS_COLOR=--color=auto ;;
esac
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ls="ls -F $LS_COLOR"
alias ll="ls -aFhl $LS_COLOR"
alias la="ls -aF $LS_COLOR"
alias lt="ls -aFhlt $LS_COLOR | head -10"
alias less='less -M'
alias grep='grep -E --color'
alias g='git'
alias h='history -16'
alias l='less'
## PATH
typeset -U PATH
case ${OSTYPE} in
darwin*)
export PATH=/opt/homebrew/bin:$PATH
export PATH=$HOME/.local/bin:$PATH
;;
linux*)
export PATH=/usr/local/bin:$PATH
;;
esac
# Ruby / Perl / Python version managers
[ -d ~/.rbenv ] && export PATH=~/.rbenv/bin:$PATH
[ -d ~/.plenv ] && export PATH=~/.plenv/bin:$PATH
[ -d ~/.pyenv ] && export PATH=~/.pyenv/bin:$PATH
command -v rbenv >/dev/null && eval "$(rbenv init -)"
command -v plenv >/dev/null && eval "$(plenv init -)"
command -v pyenv >/dev/null && eval "$(pyenv init -)"
## Oh My Zsh
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="robbyrussell"
plugins=(git zsh-completions zsh-autosuggestions fast-syntax-highlighting fzf-tab)
typeset -U fpath
case ${OSTYPE} in
darwin*) fpath=(/opt/homebrew/share/zsh/site-functions $fpath) ;;
esac
fpath=(${ZSH_CUSTOM:-$ZSH/custom}/plugins/zsh-completions/src(N) $fpath)
[ -f "$ZSH/oh-my-zsh.sh" ] && source "$ZSH/oh-my-zsh.sh"
## Tmux auto-start
is_screen_running() {
[ ! -z "$WINDOW" ]
}
is_tmux_running() {
[ ! -z "$TMUX" ]
}
is_screen_or_tmux_running() {
is_screen_running || is_tmux_running
}
shell_has_started_interactively() {
[ ! -z "$PS1" ]
}
resolve_alias() {
cmd="$1"
while \
whence "$cmd" >/dev/null 2>/dev/null \
&& [ "$(whence "$cmd")" != "$cmd" ]
do
cmd=$(whence "$cmd")
done
echo "$cmd"
}
if ! is_screen_or_tmux_running && shell_has_started_interactively; then
for cmd in tmux tscreen screen; do
if whence $cmd >/dev/null 2>/dev/null; then
$(resolve_alias "$cmd")
break
fi
done
fi
## Additional configs
[ -f ~/.zshrc.mine ] && source ~/.zshrc.mine
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
command -v fd >/dev/null && export FZF_DEFAULT_COMMAND='fd --type file --hidden --no-ignore'
export MCFLY_RESULTS=2000
if command -v mcfly >/dev/null; then
eval "$(mcfly init zsh)"
# --- スコア係数 ---
_w_frequency=1.0
_w_selected=5.0
_w_recency=10.0
_w_dir=3.0
# --- 初期オプション ---
_filter_exit_code=true
_use_dir_weight=true
function _fzf_mcfly_history() {
local db_path
if [[ -f "$HOME/.mcfly/history.db" ]]; then
db_path="$HOME/.mcfly/history.db"
elif [[ -f "$HOME/Library/Application Support/McFly/history.db" ]]; then
db_path="$HOME/Library/Application Support/McFly/history.db"
elif [[ -f "${XDG_DATA_HOME:-$HOME/.local/share}/mcfly/history.db" ]]; then
db_path="${XDG_DATA_HOME:-$HOME/.local/share}/mcfly/history.db"
else
zle -M "mcfly DB not found"
return 1
fi
local state_file query_script toggle_fe toggle_ud now cur_dir safe_dir selected
state_file=$(mktemp)
query_script=$(mktemp)
toggle_fe=$(mktemp)
toggle_ud=$(mktemp)
chmod +x "$query_script" "$toggle_fe" "$toggle_ud"
now=$(date +%s)
cur_dir=$(pwd)
safe_dir="${cur_dir//\'/\'\'}"
local init_fe=$([[ "$_filter_exit_code" == true ]] && echo 1 || echo 0)
local init_ud=$([[ "$_use_dir_weight" == true ]] && echo 1 || echo 0)
echo "$init_fe $init_ud" > "$state_file"
# クエリスクリプト(状態ファイルを読んでSQLを組み立てる)
cat > "$query_script" << SCRIPT
#!/bin/zsh
read fe ud < "${state_file}"
where=\$([[ "\$fe" == 1 ]] && echo "WHERE exit_code = 0" || echo "")
dir_sql=\$([[ "\$ud" == 1 ]] && echo "+ SUM(CASE WHEN dir = '${safe_dir}' THEN 1.0 ELSE 0.0 END) * ${_w_dir}" || echo "")
sqlite3 "${db_path}" "
SELECT cmd FROM (
SELECT cmd,
COUNT(*) * ${_w_frequency}
+ SUM(selected) * ${_w_selected}
+ ${_w_recency} / ((${now} - MAX(when_run)) / 86400.0 + 1.0)
\${dir_sql}
AS score
FROM commands
\${where}
GROUP BY cmd
)
ORDER BY score DESC
" 2>/dev/null
SCRIPT
# 「成功のみ」トグルスクリプト(fzfアクションを出力する)
cat > "$toggle_fe" << SCRIPT
#!/bin/zsh
read fe ud < "${state_file}"
[[ "\$fe" == 1 ]] && fe=0 || fe=1
echo "\$fe \$ud" > "${state_file}"
fe_s=\$([[ "\$fe" == 1 ]] && echo ON || echo OFF)
ud_s=\$([[ "\$ud" == 1 ]] && echo ON || echo OFF)
echo "reload(${query_script})+change-header(ctrl-e: 成功のみ[\${fe_s}] ctrl-d: 現在dir優先[\${ud_s}])"
SCRIPT
# 「現在dir優先」トグルスクリプト
cat > "$toggle_ud" << SCRIPT
#!/bin/zsh
read fe ud < "${state_file}"
[[ "\$ud" == 1 ]] && ud=0 || ud=1
echo "\$fe \$ud" > "${state_file}"
fe_s=\$([[ "\$fe" == 1 ]] && echo ON || echo OFF)
ud_s=\$([[ "\$ud" == 1 ]] && echo ON || echo OFF)
echo "reload(${query_script})+change-header(ctrl-e: 成功のみ[\${fe_s}] ctrl-d: 現在dir優先[\${ud_s}])"
SCRIPT
local init_fe_s=$([[ $init_fe == 1 ]] && echo ON || echo OFF)
local init_ud_s=$([[ $init_ud == 1 ]] && echo ON || echo OFF)
selected=$("$query_script" \
| fzf --no-sort --height 40% --reverse \
--query "${LBUFFER}" \
--header "ctrl-e: 成功のみ[${init_fe_s}] ctrl-d: 現在dir優先[${init_ud_s}]" \
--bind "ctrl-e:transform(${toggle_fe})" \
--bind "ctrl-d:transform(${toggle_ud})" \
2>/dev/null)
rm -f "$state_file" "$query_script" "$toggle_fe" "$toggle_ud"
if [[ -n "$selected" ]]; then
LBUFFER="$selected"
RBUFFER=""
fi
zle reset-prompt
}
zle -N _fzf_mcfly_history
bindkey '^R' _fzf_mcfly_history
fi
function _fzf_recent_files() {
local selected
selected=$(ls -tp | fzf --height 40% --reverse)
[[ -n "$selected" ]] && LBUFFER+="$selected"
zle reset-prompt
}
zle -N _fzf_recent_files
bindkey '^X^F' _fzf_recent_files # insert recent file/dir name from cwd
_fzf_history_fc_edit() {
local num cmd tmpfile
num=$(fc -l 1 | fzf --no-sort --tac --height 40% --reverse | awk '{print $1}')
[[ -z "$num" ]] && return
cmd=$(fc -ln $num $num)
tmpfile=$(mktemp /tmp/zsh-fc-edit.XXXXXX)
print -r -- "$cmd" > "$tmpfile"
${EDITOR:-vim} "$tmpfile" </dev/tty >/dev/tty
if [[ $? -eq 0 ]]; then
cmd=$(< "$tmpfile")
rm -f "$tmpfile"
print -s -- "$cmd" # add edited command to history
LBUFFER="$cmd"
RBUFFER=""
zle accept-line
else
rm -f "$tmpfile"
zle reset-prompt
fi
}
zle -N _fzf_history_fc_edit
bindkey '^X^H' _fzf_history_fc_edit # select history via fzf, then edit in $EDITOR
FAVORITES_FILE="${HOME}/.zsh_favorites"
_favorites_add() {
[[ -z "$LBUFFER" ]] && return
[[ -s "$FAVORITES_FILE" ]] && echo >> "$FAVORITES_FILE"
echo "$LBUFFER" >> "$FAVORITES_FILE"
zle -M "Saved to favorites: $LBUFFER"
}
zle -N _favorites_add
bindkey '^X^A' _favorites_add # add current line to favorites
_favorites_run() {
[[ ! -f "$FAVORITES_FILE" ]] && return
local fav="$FAVORITES_FILE"
local parse="awk '/^[[:space:]]*\$/{b=0;next} !b{printf \"%d\\t%s\\n\",NR,\$0;b=1}' \"$fav\""
local del_reload="awk -v n={1} 'NR==n{s=1;next} s{if(/^[[:space:]]*\$/)s=0;next} {print}' \"$fav\" > \"$fav.tmp\" && mv \"$fav.tmp\" \"$fav\" && $parse"
local result key lineno block tmpfile
result=$(eval "$parse" | fzf \
--height 40% --reverse \
--delimiter=$'\t' --with-nth=2 \
--expect=ctrl-e \
--header='Enter: run | Ctrl-E: edit | Ctrl-D: delete' \
--bind "ctrl-d:reload($del_reload)" \
--preview="awk -v n={1} 'NR==n{f=1} f&&/^[[:space:]]*\$/{exit} f{print}' \"$fav\"" \
--preview-window=right:50%)
[[ -z "$result" ]] && { zle reset-prompt; return; }
key=$(head -1 <<< "$result")
lineno=$(tail -1 <<< "$result" | cut -f1)
[[ -z "$lineno" ]] && { zle reset-prompt; return; }
block=$(awk -v n="$lineno" 'NR==n{f=1} f&&/^[[:space:]]*$/{exit} f{print}' "$fav")
if [[ "$key" == "ctrl-e" ]]; then
tmpfile=$(mktemp /tmp/zsh-fav.XXXXXX)
print -r -- "$block" > "$tmpfile"
${EDITOR:-vim} "$tmpfile" </dev/tty >/dev/tty
if [[ $? -eq 0 ]]; then
block=$(< "$tmpfile")
rm -f "$tmpfile"
else
rm -f "$tmpfile"
zle reset-prompt
return
fi
fi
BUFFER="$block"
CURSOR=${#BUFFER}
zle accept-line
}
zle -N _favorites_run
bindkey '^X^X' _favorites_run # select and run from favorites
if command -v zoxide >/dev/null; then
eval "$(zoxide init zsh)"
# zoxide wrapper: run pwd after z/zi
function z() {
__zoxide_z "$@" && pwd
}
function zi() {
__zoxide_zi "$@" && pwd
}
fi
_backward-kill-to-whitespace() {
local i=$CURSOR
while (( i > 0 )) && [[ "${BUFFER[i]}" != [[:space:]] ]]; do
(( i-- ))
done
BUFFER="${BUFFER:0:$i}${BUFFER:$CURSOR}"
CURSOR=$i
}
zle -N _backward-kill-to-whitespace
bindkey '^[w' _backward-kill-to-whitespace