-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshrc
607 lines (473 loc) · 18.3 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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# a lot of this has been taken from the kind folks at grml.org
# append history list to the history file; this is the default but we make sure
# because it's required for share_history.
setopt append_history
# import new commands from the history file also in other zsh-session
setopt share_history
# save each command's beginning timestamp and the duration to the history file
setopt extended_history
# If a new command line being added to the history list duplicates an older
# one, the older command is removed from the list
setopt histignorealldups
# remove command lines from the history list when the first character on the
# line is a space
setopt histignorespace
# if a command is issued that can't be executed as a normal command, and the
# command is the name of a directory, perform the cd command to that directory.
setopt auto_cd
# in order to use #, ~ and ^ for filename generation grep word
# *~(*.gz|*.bz|*.bz2|*.zip|*.Z) -> searches for word not in compressed files
# don't forget to quote '^', '~' and '#'!
setopt extended_glob
# display PID when suspending processes as well
setopt longlistjobs
# try to avoid the 'zsh: no matches found...'
setopt nonomatch
# report the status of backgrounds jobs immediately
setopt notify
# whenever a command completion is attempted, make sure the entire command path
# is hashed first.
setopt hash_list_all
# not just at the end
setopt completeinword
# Don't send SIGHUP to background processes when the shell exits.
setopt nohup
# make cd push the old directory onto the directory stack.
setopt auto_pushd
# avoid "beep"ing
setopt nobeep
# don't push the same dir twice.
setopt pushd_ignore_dups
# * shouldn't match dotfiles. ever.
setopt noglobdots
# use zsh style word splitting
setopt noshwordsplit
# don't error out when unset parameters are used
setopt unset
# "persistent history"
# just write important commands you always need to ~/.important_commands
if [[ -r ~/.important_commands ]] ; then
fc -R ~/.important_commands
fi
# report about cpu-/system-/user-time of command if running longer than
# 5 seconds
REPORTTIME=5
# watch for everyone but me and root
watch=(notme root)
# automatically remove duplicates from these arrays
typeset -U path cdpath fpath manpath
# Load a few modules
for mod in parameter complist deltochar mathfunc ; do
zmodload -i zsh/${mod} 2>/dev/null || print "Notice: no ${mod} available :("
done
# autoload zsh modules when they are referenced
zmodload -a zsh/stat zstat
zmodload -a zsh/zpty zpty
zmodload -ap zsh/mapfile mapfile
# autoload wrapper - use this one instead of autoload directly
# We need to define this function as early as this, because autoloading
# 'is-at-least()' needs it.
function zrcautoload() {
emulate -L zsh
setopt extended_glob
local fdir ffile
local -i ffound
ffile=$1
(( ffound = 0 ))
for fdir in ${fpath} ; do
[[ -e ${fdir}/${ffile} ]] && (( ffound = 1 ))
done
(( ffound == 0 )) && return 1
if [[ $ZSH_VERSION == 3.1.<6-> || $ZSH_VERSION == <4->* ]] ; then
autoload -U ${ffile} || return 1
else
autoload ${ffile} || return 1
fi
return 0
}
# we want color
zrcautoload colors && colors
# load the lookup subsystem if it's available on the system
zrcautoload lookupinit && lookupinit
# completion system
COMPDUMPFILE=${COMPDUMPFILE:-${ZDOTDIR:-${HOME}}/.zcompdump}
if zrcautoload compinit ; then
compinit -d ${COMPDUMPFILE} || print 'Notice: no compinit available :('
else
print 'Notice: no compinit available :('
function compdef { }
fi
# 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 ''
if [[ "$NOMENU" -eq 0 ]] ; then
# if there are more than 5 options allow selecting from a menu
zstyle ':completion:*' menu select=5
else
# don't use any menus at all
setopt no_auto_menu
fi
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
# recent (as of Dec 2007) zsh versions are able to provide descriptions
# for commands (read: 1st word in the line) that it will list for the user
# to choose from. The following disables that, because it's not exactly fast.
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 \
/usr/X11R6/bin
# provide .. as a completion
zstyle ':completion:*' special-dirs ..
# run rehash on completion so new installed program are found automatically:
_force_rehash() {
(( CURRENT == 1 )) && rehash
return 1
}
# correction
# some people don't like the automatic correction - so run 'NOCOR=1 zsh' to deactivate it
if [[ "$NOCOR" -gt 0 ]] ; then
zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete _files _ignored
setopt nocorrect
else
# try to be smart about when to use what completer...
setopt correct
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'
fi
# command for process lists, the local web server details and host completion
zstyle ':completion:*:urls' local 'www' '/var/www/' 'public_html'
# caching
[[ -d $ZSHDIR/cache ]] && zstyle ':completion:*' use-cache yes && \
zstyle ':completion::complete:*' cache-path $ZSHDIR/cache/
# host completion
[[ -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_hosts[@]"
"$_etc_hosts[@]"
localhost
)
zstyle ':completion:*:hosts' hosts $hosts
# TODO: so, why is this here?
# zstyle '*' 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 deborphan df feh fetchipac gpasswd head hnb ipacsum mv \
pal stow tail uname ; do
[[ -z ${_comps[$compcom]} ]] && compdef _gnu_generic ${compcom}
done; unset compcom
# see upgrade function in this file
compdef _hosts upgrade
# Custom widgets:
## beginning-of-line OR beginning-of-buffer OR beginning of history
## by: Bart Schaefer <[email protected]>, Bernhard Tittelbach
beginning-or-end-of-somewhere() {
local hno=$HISTNO
if [[ ( "${LBUFFER[-1]}" == $'\n' && "${WIDGET}" == beginning-of* ) || \
( "${RBUFFER[1]}" == $'\n' && "${WIDGET}" == end-of* ) ]]; then
zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
else
zle .${WIDGET:s/somewhere/line-hist/} "$@"
if (( HISTNO != hno )); then
zle .${WIDGET:s/somewhere/buffer-or-history/} "$@"
fi
fi
}
zle -N beginning-of-somewhere beginning-or-end-of-somewhere
zle -N end-of-somewhere beginning-or-end-of-somewhere
## toggle the ,. abbreviation feature on/off
# NOABBREVIATION: default abbreviation-state
# 0 - enabled (default)
# 1 - disabled
NOABBREVIATION=${NOABBREVIATION:-0}
grml_toggle_abbrev() {
if (( ${NOABBREVIATION} > 0 )) ; then
NOABBREVIATION=0
else
NOABBREVIATION=1
fi
}
zle -N grml_toggle_abbrev
# add a command line to the shells history without executing it
commit-to-history() {
print -s ${(z)BUFFER}
zle send-break
}
zle -N commit-to-history
# only slash should be considered as a word separator:
slash-backward-kill-word() {
local WORDCHARS="${WORDCHARS:s@/@}"
# zle backward-word
zle backward-kill-word
}
zle -N slash-backward-kill-word
# This widget can prevent unwanted autocorrections from command-name
# to _command-name, rehash automatically on enter and call any number
# of builtin and user-defined widgets in different contexts.
#
# For a broader description, see:
# <http://bewatermyfriend.org/posts/2007/12-26.11-50-38-tooltime.html>
#
# The code is imported from the file 'zsh/functions/accept-line' from
# <http://ft.bewatermyfriend.org/comp/zsh/zsh-dotfiles.tar.bz2>, which
# distributed under the same terms as zsh itself.
# A newly added command will may not be found or will cause false
# correction attempts, if you got auto-correction set. By setting the
# following style, we force accept-line() to rehash, if it cannot
# find the first word on the command line in the $command[] hash.
zstyle ':acceptline:*' rehash true
function Accept-Line() {
setopt localoptions noksharrays
local -a subs
local -xi aldone
local sub
local alcontext=${1:-$alcontext}
zstyle -a ":acceptline:${alcontext}" actions subs
(( ${#subs} < 1 )) && return 0
(( aldone = 0 ))
for sub in ${subs} ; do
[[ ${sub} == 'accept-line' ]] && sub='.accept-line'
zle ${sub}
(( aldone > 0 )) && break
done
}
function Accept-Line-getdefault() {
emulate -L zsh
local default_action
zstyle -s ":acceptline:${alcontext}" default_action default_action
case ${default_action} in
((accept-line|))
printf ".accept-line"
;;
(*)
printf ${default_action}
;;
esac
}
function Accept-Line-HandleContext() {
zle Accept-Line
default_action=$(Accept-Line-getdefault)
zstyle -T ":acceptline:${alcontext}" call_default \
&& zle ${default_action}
}
function accept-line() {
setopt localoptions noksharrays
local -ax cmdline
local -x alcontext
local buf com fname format msg default_action
alcontext='default'
buf="${BUFFER}"
cmdline=(${(z)BUFFER})
com="${cmdline[1]}"
fname="_${com}"
Accept-Line 'preprocess'
zstyle -t ":acceptline:${alcontext}" rehash \
&& [[ -z ${commands[$com]} ]] \
&& rehash
if [[ -n ${com} ]] \
&& [[ -n ${reswords[(r)$com]} ]] \
|| [[ -n ${aliases[$com]} ]] \
|| [[ -n ${functions[$com]} ]] \
|| [[ -n ${builtins[$com]} ]] \
|| [[ -n ${commands[$com]} ]] ; then
# there is something sensible to execute, just do it.
alcontext='normal'
Accept-Line-HandleContext
return
fi
if [[ -o correct ]] \
|| [[ -o correctall ]] \
&& [[ -n ${functions[$fname]} ]] ; then
# nothing there to execute but there is a function called
# _command_name; a completion widget. Makes no sense to
# call it on the commandline, but the correct{,all} options
# will ask for it nevertheless, so warn the user.
if [[ ${LASTWIDGET} == 'accept-line' ]] ; then
# Okay, we warned the user before, he called us again,
# so have it his way.
alcontext='force'
Accept-Line-HandleContext
return
fi
if zstyle -t ":acceptline:${alcontext}" nocompwarn ; then
alcontext='normal'
Accept-Line-HandleContext
else
# prepare warning message for the user, configurable via zstyle.
zstyle -s ":acceptline:${alcontext}" compwarnfmt msg
if [[ -z ${msg} ]] ; then
msg="%c will not execute and completion %f exists."
fi
zformat -f msg "${msg}" "c:${com}" "f:${fname}"
zle -M -- "${msg}"
fi
return
elif [[ -n ${buf//[$' \t\n']##/} ]] ; then
# If we are here, the commandline contains something that is not
# executable, which is neither subject to _command_name correction
# and is not empty. might be a variable assignment
alcontext='misc'
Accept-Line-HandleContext
return
fi
# If we got this far, the commandline only contains whitespace, or is empty.
alcontext='empty'
Accept-Line-HandleContext
}
zle -N accept-line
zle -N Accept-Line
zle -N Accept-Line-HandleContext
# power completion / abbreviation expansion / buffer expansion
# see http://zshwiki.org/home/examples/zleiab for details
# less risky than the global aliases but powerful as well
# just type the abbreviation key and afterwards 'ctrl-x .' to expand it
declare -A abk
setopt extendedglob
setopt interactivecomments
abk=(
# key # value (#d additional doc string)
#A# start
'...' '../..'
'....' '../../..'
'BG' '& exit'
'C' '| wc -l'
'G' '|& grep '${grep_options:+"${grep_options[*]}"}
'H' '| head'
'Hl' ' --help |& less -r' #d (Display help in pager)
'L' '| less'
'LL' '|& less -r'
'M' '| most'
'N' '&>/dev/null' #d (No Output)
'R' '| tr A-z N-za-m' #d (ROT13)
'SL' '| sort | less'
'S' '| sort -u'
'T' '| tail'
'V' '|& vim -'
#A# end
'co' './configure && make && sudo make install'
)
zleiab() {
emulate -L zsh
setopt extendedglob
local MATCH
if (( NOABBREVIATION > 0 )) ; then
LBUFFER="${LBUFFER},."
return 0
fi
LBUFFER=${LBUFFER%%(#m)[.\-+:|_a-zA-Z0-9]#}
LBUFFER+=${abk[$MATCH]:-$MATCH}
}
zle -N zleiab
help-show-abk()
{
zle -M "$(print "Type ,. after these abbreviations to expand them:"; print -a -C 2 ${(kv)abk})"
}
zle -N help-show-abk
if zrcautoload history-search-end; then
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
fi
zle -C hist-complete complete-word _generic
zstyle ':completion:hist-complete:*' completer _history
H-Glob() {
echo -e "
/ directories
. plain files
@ symbolic links
= sockets
p named pipes (FIFOs)
* executable plain files (0100)
% device files (character or block special)
%b block special files
%c character special files
r owner-readable files (0400)
w owner-writable files (0200)
x owner-executable files (0100)
A group-readable files (0040)
I group-writable files (0020)
E group-executable files (0010)
R world-readable files (0004)
W world-writable files (0002)
X world-executable files (0001)
s setuid files (04000)
S setgid files (02000)
t files with the sticky bit (01000)
print *(m-1) # Files modified up to a day ago
print *(a1) # Files accessed a day ago
print *(@) # Just symlinks
print *(Lk+50) # Files bigger than 50 kilobytes
print *(Lk-50) # Files smaller than 50 kilobytes
print **/*.c # All *.c files recursively starting in \$PWD
print **/*.c~file.c # Same as above, but excluding 'file.c'
print (foo|bar).* # Files starting with 'foo' or 'bar'
print *~*.* # All Files that do not contain a dot
chmod 644 *(.^x) # make all plain non-executable files publically readable
print -l *(.c|.h) # Lists *.c and *.h
print **/*(g:users:) # Recursively match all files that are owned by group 'users'
echo /proc/*/cwd(:h:t:s/self//) # Analogous to >ps ax | awk '{print $1}'<"
}
alias help-zshglob=H-Glob
PROMPT=$'\n'"%n@%m"$'\n'"%1~ $ "
RPROMPT="[%{$fg_no_bold[yellow]%}%?%{$reset_color%}]"
# Use emacs-like key bindings by default:
bindkey -e