-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc
427 lines (364 loc) · 14.4 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
#
# Global
#
# Create a hash table for globally stashing variables without polluting main
# scope with a bunch of identifiers.
typeset -A __ital
__ital[ITALIC_ON]=$'\e[3m'
__ital[ITALIC_OFF]=$'\e[23m'
#
# Completion
#
fpath=($HOME/.zsh/completions $fpath)
autoload -U compinit
compinit -u
# Make completion:
# - Try exact (case-sensitive) match first.
# - Then fall back to case-insensitive.
# - Accept abbreviations after . or _ or - (ie. f.b -> foo.bar).
# - Substring complete (ie. bar -> foobar).
zstyle ':completion:*' matcher-list '' '+m:{[:lower:]}={[:upper:]}' '+m:{[:upper:]}={[:lower:]}' '+m:{_-}={-_}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
# Colorize completions using default `ls` colors.
zstyle ':completion:*' list-colors ''
# Allow completion of ..<Tab> to ../ and beyond.
zstyle -e ':completion:*' special-dirs '[[ $PREFIX = (../)#(..) ]] && reply=(..)'
# $CDPATH is overpowered (can allow us to jump to 100s of directories) so tends
# to dominate completion; exclude path-directories from the tag-order so that
# they will only be used as a fallback if no completions are found.
zstyle ':completion:*:complete:(cd|pushd):*' tag-order 'local-directories named-directories'
# Categorize completion suggestions with headings:
zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format %F{default}%B%{$__ital[ITALIC_ON]%}--- %d ---%{$__ital[ITALIC_OFF]%}%b%f
# Enable keyboard navigation of completions in menu
# (not just tab/shift-tab but cursor keys as well):
zstyle ':completion:*' menu select
#
# Correction
#
# exceptions to auto-correction
alias man='nocorrect man'
alias mkdir='nocorrect mkdir'
alias mv='nocorrect mv'
alias sudo='nocorrect sudo'
#
# Prompt
#
autoload -U colors
colors
# http://zsh.sourceforge.net/Doc/Release/User-Contributions.html
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git hg
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' disable-patterns "${(b)HOME}/code/(commerce|portal|portal-ee|portal-master)(|/*)"
zstyle ':vcs_info:*' stagedstr "%F{green}●%f" # default 'S'
zstyle ':vcs_info:*' unstagedstr "%F{red}●%f" # default 'U'
zstyle ':vcs_info:*' use-simple true
zstyle ':vcs_info:git+set-message:*' hooks git-untracked
zstyle ':vcs_info:git*:*' formats '[%b%m%c%u] ' # default ' (%s)-[%b]%c%u-'
zstyle ':vcs_info:git*:*' actionformats '[%b|%a%m%c%u] ' # default ' (%s)-[%b|%a]%c%u-'
zstyle ':vcs_info:hg*:*' formats '[%m%b] '
zstyle ':vcs_info:hg*:*' actionformats '[%b|%a%m] '
zstyle ':vcs_info:hg*:*' branchformat '%b'
zstyle ':vcs_info:hg*:*' get-bookmarks true
zstyle ':vcs_info:hg*:*' get-revision true
zstyle ':vcs_info:hg*:*' get-mq false
zstyle ':vcs_info:hg*+gen-hg-bookmark-string:*' hooks hg-bookmarks
zstyle ':vcs_info:hg*+se-message:*' hooks hg-message
function +vi-git-untracked() {
emulate -L zsh
if [[ -n $(git ls-files --exclude-standard --others 2> /dev/null) ]]; then
hook_com[unstaged]+="%F{blue}●%f"
fi
}
RPROMPT_BASE="\${vcs_info_msg_0_}%F{blue}%~%f"
setopt PROMPT_SUBST
# Anonymous function to avoid leaking variables.
function () {
# Check for tmux by looking at $TERM, because $TMUX won't be propagated to any
# nested sudo shells but $TERM will.
local TMUXING=$([[ "$TERM" =~ "tmux" ]] && echo tmux)
if [ -n "$TMUXING" -a -n "$TMUX" ]; then
# In a a tmux session created in a non-root or root shell.
local LVL=$(($SHLVL - 1))
else
# Either in a root shell created inside a non-root tmux session,
# or not in a tmux session.
local LVL=$SHLVL
fi
if [[ $EUID -eq 0 ]]; then
local SUFFIX='%F{yellow}%n%f'$(printf '%%F{yellow}\u276f%.0s%%f' {1..$LVL})
else
local SUFFIX=$(printf '%%F{red}\u276f%.0s%%f' {1..$LVL})
fi
export PS1="%F{green}${SSH_TTY:+%n@%m}%f%B${SSH_TTY:+:}%b%F{blue}%B%1~%b%F{yellow}%B%(1j.*.)%(?..!)%b%f %B${SUFFIX}%b "
if [[ -n "$TMUXING" ]]; then
# Outside tmux, ZLE_RPROMPT_INDENT ends up eating the space after PS1, and
# prompt still gets corrupted even if we add an extra space to compensate.
export ZLE_RPROMPT_INDENT=0
fi
}
export RPROMPT=$RPROMPT_BASE
export SPROMPT="zsh: correct %F{red}'%R'%f to %F{red}'%r'%f [%B%Uy%u%bes, %B%Un%u%bo, %B%Ue%u%bdit, %B%Ua%u%bbort]? "
#
# History
#
export HISTSIZE=100000
export HISTFILE="$HOME/.zsh_history"
export SAVEHIST=$HISTSIZE
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS
#
# Options
#
setopt AUTO_CD # [default] .. is shortcut for cd .. (etc)
setopt AUTO_PARAM_SLASH # tab completing directory appends a slash
setopt AUTO_PUSHD # [default] cd automatically pushes old dir onto dir stack
setopt CORRECT # [default] command auto-correction
setopt CORRECT_ALL # [default] argument auto-correction
setopt NO_HIST_IGNORE_ALL_DUPS # don't filter non-contiguous duplicates from history
setopt HIST_FIND_NO_DUPS # don't show dupes when searching
setopt HIST_IGNORE_DUPS # do filter contiguous duplicates from history
setopt HIST_IGNORE_SPACE # [default] don't record commands starting with a space
setopt HIST_VERIFY # confirm history expansion (!$, !!, !foo)
setopt IGNORE_EOF # [default] prevent accidental C-d from exiting shell
setopt INTERACTIVE_COMMENTS # [default] allow comments, even in interactive shells
setopt LIST_PACKED # make completion lists more densely packed
setopt MENU_COMPLETE # auto-insert first possible ambiguous completion
setopt NO_NOMATCH # [default] unmatched patterns are left unchanged
setopt PRINT_EXIT_VALUE # [default] for non-zero exit status
setopt PUSHD_IGNORE_DUPS # don't push multiple copies of same dir onto stack
setopt PUSHD_SILENT # [default] don't print dir stack after pushing/popping
setopt SHARE_HISTORY # share history across shells
#
# Plug-ins
#
# NOTE: must come before zsh-history-substring-search & zsh-syntax-highlighting.
autoload -U select-word-style
select-word-style bash # only alphanumeric chars are considered WORDCHARS
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=59'
# NOTE: must come after select-word-style.
source ~/.zsh/zsh-history-substring-search/zsh-history-substring-search.zsh
# Note that this will only ensure unique history if we supply a prefix
# before hitting "up" (ie. we perform a "search"). HIST_FIND_NO_DUPS
# won't prevent dupes from appearing when just hitting "up" without a
# prefix (ie. that's "zle up-line-or-history" and not classified as a
# "search"). So, we have HIST_IGNORE_DUPS to make life bearable for that
# case.
#
# https://superuser.com/a/1494647/322531
HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=1
# Uncomment this to get syntax highlighting:
# NOTE: must come after select-word-style.
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Enables bracketed paste
source ~/.zsh/paste
#
# Bindings
#
# Use "cbt" capability ("back_tab", as per `man terminfo`), if we have it:
if tput cbt &> /dev/null; then
bindkey "$(tput cbt)" reverse-menu-complete # make Shift-tab go to previous completion
fi
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
autoload -U edit-command-line
zle -N edit-command-line
bindkey '^x^x' edit-command-line
bindkey -v # Vim kyebinds
bindkey jj vi-cmd-mode
export KEYTIMEOUT=20 # Make Vi mode transitions faster (KEYTIMEOUT is in hundredths of a second)
bindkey ' ' magic-space # do history expansion on space
# Replace standard history-incremental-search-{backward,forward} bindings.
# These are the same but permit patterns (eg. a*b) to be used.
bindkey "^r" history-incremental-pattern-search-backward
bindkey "^s" history-incremental-pattern-search-forward
# Make CTRL-Z background things and unbackground them.
function fg-bg() {
if [[ $#BUFFER -eq 0 ]]; then
fg
else
zle push-input
fi
}
zle -N fg-bg
bindkey '^Z' fg-bg
#
# Other
#
source $HOME/.zsh/aliases
source $HOME/.zsh/functions
source $HOME/.zsh/exports
#
# Third-party
#
# Skim
# test -e "$HOME/.zsh/skim/shell/key-bindings.zsh" && source "$HOME/.zsh/skim/shell/key-bindings.zsh"
test -e "$HOME/.zsh/skim/shell/completion.zsh" && source "$HOME/.zsh/skim/shell/completion.zsh"
#
# Hooks
#
autoload -U add-zsh-hook
function -set-tab-and-window-title() {
emulate -L zsh
local CMD="${1:gs/$/\\$}"
print -Pn "\e]0;$CMD:q\a"
}
# $HISTCMD (the current history event number) is shared across all shells
# (due to SHARE_HISTORY). Maintain this local variable to count the number of
# commands run in this specific shell.
HISTCMD_LOCAL=0
# Executed before displaying prompt.
function -update-window-title-precmd() {
emulate -L zsh
if [[ HISTCMD_LOCAL -eq 0 ]]; then
# About to display prompt for the first time; nothing interesting to show in
# the history. Show $PWD.
-set-tab-and-window-title "$(basename $PWD)"
else
local LAST=$(history | tail -1 | awk '{print $2}')
if [ -n "$TMUX" ]; then
# Inside tmux, just show the last command: tmux will prefix it with the
# session name (for context).
-set-tab-and-window-title "$LAST"
else
# Outside tmux, show $PWD (for context) followed by the last command.
-set-tab-and-window-title "$(basename $PWD) > $LAST"
fi
fi
}
add-zsh-hook precmd -update-window-title-precmd
# Executed before executing a command: $2 is one-line (truncated) version of
# the command.
function -update-window-title-preexec() {
emulate -L zsh
setopt EXTENDED_GLOB
HISTCMD_LOCAL=$((++HISTCMD_LOCAL))
# Skip ENV=settings, sudo, ssh; show first distinctive word of command;
# mostly stolen from:
# https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/termsupport.zsh
local TRIMMED="${2[(wr)^(*=*|mosh|ssh|sudo)]}"
if [ -n "$TMUX" ]; then
# Inside tmux, show the running command: tmux will prefix it with the
# session name (for context).
-set-tab-and-window-title "$TRIMMED"
else
# Outside tmux, show $PWD (for context) followed by the running command.
-set-tab-and-window-title "$(basename $PWD) > $TRIMMED"
fi
}
add-zsh-hook preexec -update-window-title-preexec
typeset -F SECONDS
function -record-start-time() {
emulate -L zsh
ZSH_START_TIME=${ZSH_START_TIME:-$SECONDS}
}
add-zsh-hook preexec -record-start-time
function -report-start-time() {
emulate -L zsh
if [ $ZSH_START_TIME ]; then
local DELTA=$(($SECONDS - $ZSH_START_TIME))
local DAYS=$((~~($DELTA / 86400)))
local HOURS=$((~~(($DELTA - $DAYS * 86400) / 3600)))
local MINUTES=$((~~(($DELTA - $DAYS * 86400 - $HOURS * 3600) / 60)))
local SECS=$(($DELTA - $DAYS * 86400 - $HOURS * 3600 - $MINUTES * 60))
local ELAPSED=''
test "$DAYS" != '0' && ELAPSED="${DAYS}d"
test "$HOURS" != '0' && ELAPSED="${ELAPSED}${HOURS}h"
test "$MINUTES" != '0' && ELAPSED="${ELAPSED}${MINUTES}m"
if [ "$ELAPSED" = '' ]; then
SECS="$(print -f "%.2f" $SECS)s"
elif [ "$DAYS" != '0' ]; then
SECS=''
else
SECS="$((~~$SECS))s"
fi
ELAPSED="${ELAPSED}${SECS}"
export RPROMPT="%F{cyan}%{$__ital[ITALIC_ON]%}${ELAPSED}%{$__ital[ITALIC_OFF]%}%f $RPROMPT_BASE"
unset ZSH_START_TIME
else
export RPROMPT="$RPROMPT_BASE"
fi
}
add-zsh-hook precmd -report-start-time
function -auto-ls-after-cd() {
emulate -L zsh
# Only in response to a user-initiated `cd`, not indirectly (eg. via another
# function).
if [ "$ZSH_EVAL_CONTEXT" = "toplevel:shfunc" ]; then
ls -a
fi
}
add-zsh-hook chpwd -auto-ls-after-cd
# Remember each command we run.
function -record-command() {
__ital[LAST_COMMAND]="$2"
}
add-zsh-hook preexec -record-command
# Update vcs_info (slow) after any command that probably changed it.
function -maybe-show-vcs-info() {
local LAST="$__ital[LAST_COMMAND]"
# In case user just hit enter, overwrite LAST_COMMAND, because preexec
# won't run and it will otherwise linger.
__ital[LAST_COMMAND]="<unset>"
# Check first word; via:
# http://tim.vanwerkhoven.org/post/2012/10/28/ZSH/Bash-string-manipulation
case "$LAST[(w)1]" in
cd|cp|git|rm|touch|mv)
vcs_info
;;
*)
;;
esac
}
add-zsh-hook precmd -maybe-show-vcs-info
# adds `cdr` command for navigating to recent directories
autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs
# enable menu-style completion for cdr
zstyle ':completion:*:*:cdr:*:*' menu selection
# fall through to cd if cdr is passed a non-recent dir as an argument
zstyle ':chpwd:*' recent-dirs-default true
# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
typeset -g -A key
key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
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]}"
key[Shift-Tab]="${terminfo[kcbt]}"
# 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[Backspace]}" ]] && bindkey -- "${key[Backspace]}" backward-delete-char
[[ -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
[[ -n "${key[Shift-Tab]}" ]] && bindkey -- "${key[Shift-Tab]}" reverse-menu-complete
# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
autoload -Uz add-zle-hook-widget
function zle_application_mode_start { echoti smkx }
function zle_application_mode_stop { echoti rmkx }
add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi