-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshrc
360 lines (287 loc) · 10.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
#############################
# Exports
#############################
export FPATH=/usr/share/zsh/functions/Async:$FPATH
export PAGER=${PAGER:-less}
export SYSTEMD_EDITOR=${SYSTEMD_EDITOR:-vim}
export EDITOR=${EDITOR:-vim}
export VIRTUAL=${VIRTUAL:-vim}
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
export COLORTERM="yes"
export KEYTIMEOUT=1
export HISTSIZE=1000
export SAVEHIST=1000
export HISTFILE=$HOME/.zsh_history
export SKIM_DEFAULT_OPTIONS="--bind=alt-j:down,alt-k:up --reverse --height=40%"
#############################
# Autoloads
#############################
autoload -Uz compinit && compinit -i
autoload -U promptinit && promptinit
autoload -Uz add-zle-hook-widget
autoload -Uz edit-command-line
#############################
# ZSH Modules
#############################
zmodload zsh/parameter
zmodload zsh/complist
zmodload zsh/deltochar
zmodload zsh/mathfunc
zmodload -ap zsh/mapfile mapfile
zmodload -a zsh/stat zstat
zmodload -a zsh/zpty zpty
#############################
# Prompt
#############################
prompt dbuch
#############################
# ZSH Options
#############################
setopt vi
setopt append_history
setopt extended_history
setopt histignorealldups
setopt histignorespace
setopt auto_cd
setopt extended_glob
setopt longlistjobs
setopt nobeep
setopt notify
setopt nohup
setopt completeinword
setopt pushd_ignore_dups
setopt hash_list_all
setopt interactivecomments
setopt noglobdots
setopt noshwordsplit
setopt unset
setopt correct
setopt NO_clobber
#############################
# Types
#############################
typeset -ga ls_options
typeset -ga grep_options
ls_options+=( -vA --color=auto --group-directories-first )
grep_options+=( --color=auto )
#############################
# Alias
#############################
alias sudo='sudo '
alias ls="command ls ${ls_options:+${ls_options[*]}}"
alias la="command ls -la ${ls_options:+${ls_options[*]}}"
alias ll="command ls -l ${ls_options:+${ls_options[*]}}"
alias lh="command ls -hAl ${ls_options:+${ls_options[*]}}"
alias l="command ls -l ${ls_options:+${ls_options[*]}}"
alias grep="command grep ${grep_options:+${grep_options[*]}}"
#############################
# Functions
#############################
# Ensure precmds are run after cd
local redraw-prompt() {
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle reset-prompt
}
function skim-history() {
local selected num
setopt localoptions noglobsubst noposixbuiltins pipefail 2> /dev/null
selected=( $(fc -rl 1 | SKIM_DEFAULT_OPTIONS="--reverse --height ${SKIM_TMUX_HEIGHT:-40%} $SKIM_DEFAULT_OPTIONS -n2..,.. --tiebreak=index $SKIM_CTRL_R_OPTS --query=${(qqq)LBUFFER} -m" sk) )
local ret=$?
if [ -n "$selected" ]; then
num=$selected[1]
if [ -n "$num" ]; then
zle vi-fetch-history -n $num
fi
fi
zle redraw-prompt
return $ret
}
function skim-bookmarks() {
setopt localoptions pipefail 2> /dev/null
local location=$(cat "$BOOKMARKFILE" | sk $SKIM_DEFAULT_OPTIONS)
if [[ -z $location ]]; then
zle redisplay
return 0
fi
cd `echo "$location" | sed "s|~|$HOME/|"`
local ret=$?
zle redraw-prompt
return $ret
}
function skim-grep() {
local res=$(sk $SKIM_DEFAULT_OPTIONS --ansi -i -c 'rg --color=always --line-number "{}"')
if [[ -z $res ]]; then
zle redisplay
return 0
fi
local file=$(cut -d':' -f1 <<< "$res")
local lineN=$(cut -d':' -f2 <<< "$res")
$EDITOR $file +$lineN
local ret=$?
zle redraw-prompt
return $ret
}
# smart cd function, allows switching to /etc when running 'cd /etc/fstab'
function cd () {
if (( ${#argv} == 1 )) && [[ -f ${1} ]]; then
[[ ! -e ${1:h} ]] && return 1
print "Correcting ${1} to ${1:h}"
builtin cd ${1:h}
else
builtin cd "$@"
fi
}
function bookmark() {
local bm=`echo $PWD`
if [[ -z $(grep -x "$bm" $HOME/.bookmarks 2>/dev/null) ]]; then
echo $bm >> $HOME/.bookmarks
echo "Bookmark '$bm' saved"
else
echo "Bookmark '$bm' existed"
return 1
fi
}
#############################
# Zsh Widgets
#############################
zle -N redraw-prompt
zle -N edit-command-line
zle -N skim-bookmarks
zle -N skim-grep
zle -N skim-history
#############################
# Keybindings
#############################
bindkey -v
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
bindkey -M vicmd -r "^j"
bindkey -M vicmd 'J' vi-down-line-or-history
bindkey -M viins '^[J' vi-down-line-or-history
bindkey -M vicmd 'k' vi-up-line-or-history
bindkey -M viins '^[K' vi-up-line-or-history
bindkey -M vicmd 'v' edit-command-line
bindkey -M viins '^[v' edit-command-line
bindkey -M vicmd '^[g' skim-grep
bindkey -M viins '^[g' skim-grep
bindkey -M vicmd '^[b' skim-bookmarks
bindkey -M viins '^[b' skim-bookmarks
bindkey -M vicmd '^[r' skim-history
bindkey -M viins '^[r' skim-history
#############################
# Completion system
#############################
# allow one error for every three characters typed in approximate completer
zstyle ':completion:*:approximate:' max-errors 'reply=( $((($#PREFIX+$#SUFFIX)/3 )) numeric )'
# don't complete backup files as executables
zstyle ':completion:*:complete:-command-::commands' ignored-patterns '(aptitude-*|*\~)'
# start menu completion only if it could find no unambiguous initial string
zstyle ':completion:*:correct:*' insert-unambiguous true
zstyle ':completion:*:corrections' format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}'
zstyle ':completion:*:correct:*' original true
# activate color-completion
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# format on completion
zstyle ':completion:*:descriptions' format $'%{\e[0;31m%}completing %B%d%b%{\e[0m%}'
# automatically complete 'cd -<tab>' and 'cd -<ctrl-d>' with menu
# zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
# insert all expansions for expand completer
zstyle ':completion:*:expand:*' tag-order all-expansions
zstyle ':completion:*:history-words' list false
# activate menu
zstyle ':completion:*:history-words' menu yes
# ignore duplicate entries
zstyle ':completion:*:history-words' remove-all-dups yes
zstyle ':completion:*:history-words' stop yes
# match uppercase from lowercase
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
# separate matches into groups
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=5
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:options' auto-description '%d'
# describe options in full
zstyle ':completion:*:options' description 'yes'
# on processes completion complete all user processes
zstyle ':completion:*:processes' command 'ps -au$USER'
# offer indexes before parameters in subscripts
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
# provide verbose completion information
zstyle ':completion:*' verbose true
zstyle ':completion:*:-command-:*:' verbose false
# set format for warnings
zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches for:%{\e[0m%} %d'
# define files to ignore for zcompile
zstyle ':completion:*:*:zcompile:*' ignored-patterns '(*~|*.zwc)'
zstyle ':completion:correct:' prompt 'correct to: %e'
# Ignore completion functions for commands you don't have:
zstyle ':completion::(^approximate*):*:functions' ignored-patterns '_*'
# Provide more processes in completion of programs like killall:
zstyle ':completion:*:processes-names' command 'ps c -u ${USER} -o command | uniq'
# complete manual by their section
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.*' insert-sections true
zstyle ':completion:*:man:*' menu yes select
# Search path for sudo completion
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin \
/usr/local/bin \
/usr/sbin \
/usr/bin \
/sbin \
/bin
# provide .. as a completion
zstyle ':completion:*' special-dirs ..
# run rehash on completion so new installed program are found automatically:
function _force_rehash () {
(( CURRENT == 1 )) && rehash
return 1
}
zstyle -e ':completion:*' completer '
if [[ $_last_try != "$HISTNO$BUFFER$CURSOR" ]] ; then
_last_try="$HISTNO$BUFFER$CURSOR"
reply=(_complete _match _ignored _prefix _files)
else
if [[ $words[1] == (rm|mv) ]] ; then
reply=(_complete _files)
else
reply=(_oldlist _expand _force_rehash _complete _ignored _correct _approximate _files)
fi
fi'
# command for process lists, the local web server details and host completion
zstyle ':completion:*:urls' local 'www' '/var/www/' 'public_html'
COMP_CACHE_DIR=${COMP_CACHE_DIR:-${ZDOTDIR:-$HOME}/.cache}
if [[ ! -d ${COMP_CACHE_DIR} ]]; then
command mkdir -p "${COMP_CACHE_DIR}"
fi
zstyle ':completion:*' use-cache yes
zstyle ':completion:*:complete:*' cache-path "${COMP_CACHE_DIR}"
# host completion
[[ -r ~/.ssh/config ]] && _ssh_config_hosts=(${${(s: :)${(ps:\t:)${${(@M)${(f)"$(<$HOME/.ssh/config)"}:#Host *}#Host }}}:#*[*?]*}) || _ssh_config_hosts=()
[[ -r ~/.ssh/known_hosts ]] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=()
[[ -r /etc/hosts ]] && : ${(A)_etc_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}##[:blank:]#[^[:blank:]]#}}} || _etc_hosts=()
hosts=(
$(hostname)
"$_ssh_config_hosts[@]"
"$_ssh_hosts[@]"
"$_etc_hosts[@]"
localhost
)
zstyle ':completion:*:hosts' hosts $hosts
# use generic completion system for programs not yet defined; (_gnu_generic works
# with commands that provide a --help option with "standard" gnu-like output.)
for compcom in cp df gpasswd head mv \
pal stow uname ; do
[[ -z ${_comps[$compcom]} ]] && compdef _gnu_generic ${compcom}
done; unset compcom