-
Notifications
You must be signed in to change notification settings - Fork 0
/
zshrc
2789 lines (2430 loc) · 92.5 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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Filename: /etc/zsh/zshrc
# Purpose: config file for zsh (z shell)
# Authors: grml-team (grml.org), (c) Michael Prokop <[email protected]>
# Bug-Reports: see http://grml.org/bugs/
# License: This file is licensed under the GPL v2.
################################################################################
# This file is sourced only for interactive shells. It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#
# Global Order: zshenv, zprofile, zshrc, zlogin
################################################################################
# USAGE
# If you are using this file as your ~/.zshrc file, please use ~/.zshrc.pre
# and ~/.zshrc.local for your own customisations. The former file is read
# before ~/.zshrc, the latter is read after it. Also, consider reading the
# refcard and the reference manual for this setup, both available from:
# <http://grml.org/zsh/>
# Contributing:
# If you want to help to improve grml's zsh setup, clone the grml-etc-core
# repository from git.grml.org:
# git clone git://git.grml.org/grml-etc-core.git
#
# Make your changes, commit them; use 'git format-patch' to create a series
# of patches and send those to the following address via 'git send-email':
#
# Doing so makes sure the right people get your patches for review and
# possibly inclusion.
# zsh-refcard-tag documentation:
# You may notice strange looking comments in this file.
# These are there for a purpose. grml's zsh-refcard can now be
# automatically generated from the contents of the actual configuration
# file. However, we need a little extra information on which comments
# and what lines of code to take into account (and for what purpose).
#
# Here is what they mean:
#
# List of tags (comment types) used:
# #a# Next line contains an important alias, that should
# be included in the grml-zsh-refcard.
# (placement tag: @@INSERT-aliases@@)
# #f# Next line contains the beginning of an important function.
# (placement tag: @@INSERT-functions@@)
# #v# Next line contains an important variable.
# (placement tag: @@INSERT-variables@@)
# #k# Next line contains an important keybinding.
# (placement tag: @@INSERT-keybindings@@)
# #d# Hashed directories list generation:
# start denotes the start of a list of 'hash -d'
# definitions.
# end denotes its end.
# (placement tag: @@INSERT-hasheddirs@@)
# #A# Abbreviation expansion list generation:
# start denotes the beginning of abbreviations.
# end denotes their end.
# Lines within this section that end in '#d .*' provide
# extra documentation to be included in the refcard.
# (placement tag: @@INSERT-abbrev@@)
# #m# This tag allows you to manually generate refcard entries
# for code lines that are hard/impossible to parse.
# Example:
# #m# k ESC-h Call the run-help function
# That would add a refcard entry in the keybindings table
# for 'ESC-h' with the given comment.
# So the syntax is: #m# <section> <argument> <comment>
# #o# This tag lets you insert entries to the 'other' hash.
# Generally, this should not be used. It is there for
# things that cannot be done easily in another way.
# (placement tag: @@INSERT-other-foobar@@)
#
# All of these tags (except for m and o) take two arguments, the first
# within the tag, the other after the tag:
#
# #<tag><section># <comment>
#
# Where <section> is really just a number, which are defined by the
# @secmap array on top of 'genrefcard.pl'. The reason for numbers
# instead of names is, that for the reader, the tag should not differ
# much from a regular comment. For zsh, it is a regular comment indeed.
# The numbers have got the following meanings:
# 0 -> "default"
# 1 -> "system"
# 2 -> "user"
# 3 -> "debian"
# 4 -> "search"
# 5 -> "shortcuts"
# 6 -> "services"
#
# So, the following will add an entry to the 'functions' table in the
# 'system' section, with a (hopefully) descriptive comment:
# #f1# Edit an alias via zle
# edalias() {
#
# It will then show up in the @@INSERT-aliases-system@@ replacement tag
# that can be found in 'grml-zsh-refcard.tex.in'.
# If the section number is omitted, the 'default' section is assumed.
# Furthermore, in 'grml-zsh-refcard.tex.in' @@INSERT-aliases@@ is
# exactly the same as @@INSERT-aliases-default@@. If you want a list of
# *all* aliases, for example, use @@INSERT-aliases-all@@.
# zsh profiling
# just execute 'ZSH_PROFILE_RC=1 zsh' and run 'zprof' to get the details
if [[ $ZSH_PROFILE_RC -gt 0 ]] ; then
zmodload zsh/zprof
fi
# load .zshrc.pre to give the user the chance to overwrite the defaults
[[ -r ${HOME}/.zshrc.pre ]] && source ${HOME}/.zshrc.pre
# check for version/system
# check for versions (compatibility reasons)
is4(){
[[ $ZSH_VERSION == <4->* ]] && return 0
return 1
}
is41(){
[[ $ZSH_VERSION == 4.<1->* || $ZSH_VERSION == <5->* ]] && return 0
return 1
}
is42(){
[[ $ZSH_VERSION == 4.<2->* || $ZSH_VERSION == <5->* ]] && return 0
return 1
}
is425(){
[[ $ZSH_VERSION == 4.2.<5->* || $ZSH_VERSION == 4.<3->* || $ZSH_VERSION == <5->* ]] && return 0
return 1
}
is43(){
[[ $ZSH_VERSION == 4.<3->* || $ZSH_VERSION == <5->* ]] && return 0
return 1
}
is433(){
[[ $ZSH_VERSION == 4.3.<3->* || $ZSH_VERSION == 4.<4->* \
|| $ZSH_VERSION == <5->* ]] && return 0
return 1
}
is439(){
[[ $ZSH_VERSION == 4.3.<9->* || $ZSH_VERSION == 4.<4->* \
|| $ZSH_VERSION == <5->* ]] && return 0
return 1
}
#f1# Checks whether or not you're running grml
isgrml(){
[[ -f /etc/grml_version ]] && return 0
return 1
}
#f1# Checks whether or not you're running a grml cd
isgrmlcd(){
[[ -f /etc/grml_cd ]] && return 0
return 1
}
if isgrml ; then
#f1# Checks whether or not you're running grml-small
isgrmlsmall() {
if [[ ${${${(f)"$(</etc/grml_version)"}%% *}##*-} == 'small' ]]; then
return 0
fi
return 1
}
else
isgrmlsmall() { return 1 }
fi
isdarwin(){
[[ $OSTYPE == darwin* ]] && return 0
return 1
}
#f1# are we running within an utf environment?
isutfenv() {
case "$LANG $CHARSET $LANGUAGE" in
*utf*) return 0 ;;
*UTF*) return 0 ;;
*) return 1 ;;
esac
}
# check for user, if not running as root set $SUDO to sudo
(( EUID != 0 )) && SUDO='sudo' || SUDO=''
# change directory to home on first invocation of zsh
# important for rungetty -> autologin
# Thanks go to Bart Schaefer!
isgrml && checkhome() {
if [[ -z "$ALREADY_DID_CD_HOME" ]] ; then
export ALREADY_DID_CD_HOME=$HOME
cd
fi
}
# check for zsh v3.1.7+
if ! [[ ${ZSH_VERSION} == 3.1.<7->* \
|| ${ZSH_VERSION} == 3.<2->.<->* \
|| ${ZSH_VERSION} == <4->.<->* ]] ; then
printf '-!-\n'
printf '-!- In this configuration we try to make use of features, that only\n'
printf '-!- require version 3.1.7 of the shell; That way this setup can be\n'
printf '-!- used with a wide range of zsh versions, while using fairly\n'
printf '-!- advanced features in all supported versions.\n'
printf '-!-\n'
printf '-!- However, you are running zsh version %s.\n' "$ZSH_VERSION"
printf '-!-\n'
printf '-!- While this *may* work, it might as well fail.\n'
printf '-!- Please consider updating to at least version 3.1.7 of zsh.\n'
printf '-!-\n'
printf '-!- DO NOT EXPECT THIS TO WORK FLAWLESSLY!\n'
printf '-!- If it does today, you'\''ve been lucky.\n'
printf '-!-\n'
printf '-!- Ye been warned!\n'
printf '-!-\n'
function zstyle() { : }
fi
# 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
(( found = 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
}
# Load is-at-least() for more precise version checks Note that this test will
# *always* fail, if the is-at-least function could not be marked for
# autoloading.
zrcautoload is-at-least || is-at-least() { return 1 }
# set some important options (as early as possible)
# 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
is4 && 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
is4 && 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
# setting some default values
NOCOR=${NOCOR:-0}
NOMENU=${NOMENU:-0}
NOPRECMD=${NOPRECMD:-0}
COMMAND_NOT_FOUND=${COMMAND_NOT_FOUND:-0}
GRML_ZSH_CNF_HANDLER=${GRML_ZSH_CNF_HANDLER:-/usr/share/command-not-found/command-not-found}
BATTERY=${BATTERY:-0}
GRMLSMALL_SPECIFIC=${GRMLSMALL_SPECIFIC:-1}
ZSH_NO_DEFAULT_LOCALE=${ZSH_NO_DEFAULT_LOCALE:-0}
typeset -ga ls_options
typeset -ga grep_options
if ls --help 2> /dev/null | grep -q GNU; then
ls_options=( --color=auto )
elif [[ $OSTYPE == freebsd* ]]; then
ls_options=( -G )
fi
if grep --help 2> /dev/null | grep -q GNU || \
[[ $OSTYPE == freebsd* ]]; then
grep_options=( --color=auto )
fi
# utility functions
# this function checks if a command exists and returns either true
# or false. This avoids using 'which' and 'whence', which will
# avoid problems with aliases for which on certain weird systems. :-)
# Usage: check_com [-c|-g] word
# -c only checks for external commands
# -g does the usual tests and also checks for global aliases
check_com() {
emulate -L zsh
local -i comonly gatoo
if [[ $1 == '-c' ]] ; then
(( comonly = 1 ))
shift
elif [[ $1 == '-g' ]] ; then
(( gatoo = 1 ))
else
(( comonly = 0 ))
(( gatoo = 0 ))
fi
if (( ${#argv} != 1 )) ; then
printf 'usage: check_com [-c] <command>\n' >&2
return 1
fi
if (( comonly > 0 )) ; then
[[ -n ${commands[$1]} ]] && return 0
return 1
fi
if [[ -n ${commands[$1]} ]] \
|| [[ -n ${functions[$1]} ]] \
|| [[ -n ${aliases[$1]} ]] \
|| [[ -n ${reswords[(r)$1]} ]] ; then
return 0
fi
if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then
return 0
fi
return 1
}
# creates an alias and precedes the command with
# sudo if $EUID is not zero.
salias() {
emulate -L zsh
local only=0 ; local multi=0
while [[ $1 == -* ]] ; do
case $1 in
(-o) only=1 ;;
(-a) multi=1 ;;
(--) shift ; break ;;
(-h)
printf 'usage: salias [-h|-o|-a] <alias-expression>\n'
printf ' -h shows this help text.\n'
printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n'
printf ' be careful using this option.\n'
printf ' -o only sets an alias if a preceding sudo would be needed.\n'
return 0
;;
(*) printf "unkown option: '%s'\n" "$1" ; return 1 ;;
esac
shift
done
if (( ${#argv} > 1 )) ; then
printf 'Too many arguments %s\n' "${#argv}"
return 1
fi
key="${1%%\=*}" ; val="${1#*\=}"
if (( EUID == 0 )) && (( only == 0 )); then
alias -- "${key}=${val}"
elif (( EUID > 0 )) ; then
(( multi > 0 )) && val="${val// ; / ; sudo }"
alias -- "${key}=sudo ${val}"
fi
return 0
}
# a "print -l ${(u)foo}"-workaround for pre-4.2.0 shells
# usage: uprint foo
# Where foo is the *name* of the parameter you want printed.
# Note that foo is no typo; $foo would be wrong here!
if ! is42 ; then
uprint () {
emulate -L zsh
local -a u
local w
local parameter=$1
if [[ -z ${parameter} ]] ; then
printf 'usage: uprint <parameter>\n'
return 1
fi
for w in ${(P)parameter} ; do
[[ -z ${(M)u:#$w} ]] && u=( $u $w )
done
builtin print -l $u
}
fi
# Check if we can read given files and source those we can.
xsource() {
if (( ${#argv} < 1 )) ; then
printf 'usage: xsource FILE(s)...\n' >&2
return 1
fi
while (( ${#argv} > 0 )) ; do
[[ -r "$1" ]] && source "$1"
shift
done
return 0
}
# Check if we can read a given file and 'cat(1)' it.
xcat() {
emulate -L zsh
if (( ${#argv} != 1 )) ; then
printf 'usage: xcat FILE\n' >&2
return 1
fi
[[ -r $1 ]] && cat $1
return 0
}
# Remove these functions again, they are of use only in these
# setup files. This should be called at the end of .zshrc.
xunfunction() {
emulate -L zsh
local -a funcs
funcs=(salias xcat xsource xunfunction zrcautoload)
for func in $funcs ; do
[[ -n ${functions[$func]} ]] \
&& unfunction $func
done
return 0
}
# this allows us to stay in sync with grml's zshrc and put own
# modifications in ~/.zshrc.local
zrclocal() {
xsource "/etc/zsh/zshrc.local"
xsource "${HOME}/.zshrc.local"
return 0
}
# locale setup
if (( ZSH_NO_DEFAULT_LOCALE == 0 )); then
xsource "/etc/default/locale"
fi
for var in LANG LC_ALL LC_MESSAGES ; do
[[ -n ${(P)var} ]] && export $var
done
xsource "/etc/sysconfig/keyboard"
TZ=$(xcat /etc/timezone)
# set some variables
if check_com -c vim ; then
#v#
export EDITOR=${EDITOR:-vim}
else
export EDITOR=${EDITOR:-vi}
fi
#v#
export PAGER=${PAGER:-less}
#v#
export MAIL=${MAIL:-/var/mail/$USER}
# if we don't set $SHELL then aterm, rxvt,.. will use /bin/sh or /bin/bash :-/
export SHELL='/bin/zsh'
# color setup for ls:
check_com -c dircolors && eval $(dircolors -b)
# color setup for ls on OS X:
isdarwin && export CLICOLOR=1
# do MacPorts setup on darwin
if isdarwin && [[ -d /opt/local ]]; then
# Note: PATH gets set in /etc/zprofile on Darwin, so this can't go into
# zshenv.
PATH="/opt/local/bin:/opt/local/sbin:$PATH"
MANPATH="/opt/local/share/man:$MANPATH"
fi
# do Fink setup on darwin
isdarwin && xsource /sw/bin/init.sh
# load our function and completion directories
for fdir in /usr/share/grml/zsh/completion /usr/share/grml/zsh/functions; do
fpath=( ${fdir} ${fdir}/**/*(/N) ${fpath} )
if [[ ${fpath} == '/usr/share/grml/zsh/functions' ]] ; then
for func in ${fdir}/**/[^_]*[^~](N.) ; do
zrcautoload ${func:t}
done
fi
done
unset fdir func
# support colors in less
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'
# mailchecks
MAILCHECK=30
# 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
# keybindings
if [[ "$TERM" != emacs ]] ; then
[[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line
[[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]" vi-delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]" vi-beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M vicmd "$terminfo[kend]" vi-end-of-line
[[ -z "$terminfo[cuu1]" ]] || bindkey -M viins "$terminfo[cuu1]" vi-up-line-or-history
[[ -z "$terminfo[cuf1]" ]] || bindkey -M viins "$terminfo[cuf1]" vi-forward-char
[[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]" vi-up-line-or-history
[[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]" vi-down-line-or-history
[[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]" vi-forward-char
[[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]" vi-backward-char
# ncurses stuff:
[[ "$terminfo[kcuu1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcuu1]/O/[}" vi-up-line-or-history
[[ "$terminfo[kcud1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcud1]/O/[}" vi-down-line-or-history
[[ "$terminfo[kcuf1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcuf1]/O/[}" vi-forward-char
[[ "$terminfo[kcub1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcub1]/O/[}" vi-backward-char
[[ "$terminfo[khome]" == $'\eO'* ]] && bindkey -M viins "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kend]/O/[}" end-of-line
[[ "$terminfo[khome]" == $'\eO'* ]] && bindkey -M emacs "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == $'\eO'* ]] && bindkey -M emacs "${terminfo[kend]/O/[}" end-of-line
fi
## keybindings (run 'bindkeys' for details, more details via man zshzle)
# use emacs style per default:
bindkey -e
# use vi style:
# bindkey -v
## 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
#if [[ "$TERM" == screen ]] ; then
## with HOME/END, move to beginning/end of line (on multiline) on first keypress
## to beginning/end of buffer on second keypress
## and to beginning/end of history on (at most) the third keypress
# terminator & non-debian xterm
bindkey '\eOH' beginning-of-somewhere # home
bindkey '\eOF' end-of-somewhere # end
# freebsd console
bindkey '\e[H' beginning-of-somewhere # home
bindkey '\e[F' end-of-somewhere # end
# xterm,gnome-terminal,quake,etc
bindkey '^[[1~' beginning-of-somewhere # home
bindkey '^[[4~' end-of-somewhere # end
# if terminal type is set to 'rxvt':
bindkey '\e[7~' beginning-of-somewhere # home
bindkey '\e[8~' end-of-somewhere # end
#fi
bindkey '\e[A' up-line-or-search # cursor up
bindkey '\e[B' down-line-or-search # <ESC>-
## use Ctrl-left-arrow and Ctrl-right-arrow for jumping to word-beginnings on the CL
bindkey "\e[5C" forward-word
bindkey "\e[5D" backward-word
bindkey "\e[1;5C" forward-word
bindkey "\e[1;5D" backward-word
## the same for alt-left-arrow and alt-right-arrow
bindkey '^[[1;3C' forward-word
bindkey '^[[1;3D' backward-word
# Search backward in the history for a line beginning with the current
# line up to the cursor and move the cursor to the end of the line then
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
#k# search history backward for entry beginning with typed text
bindkey '^xp' history-beginning-search-backward-end
#k# search history forward for entry beginning with typed text
bindkey '^xP' history-beginning-search-forward-end
#k# search history backward for entry beginning with typed text
bindkey "\e[5~" history-beginning-search-backward-end # PageUp
#k# search history forward for entry beginning with typed text
bindkey "\e[6~" history-beginning-search-forward-end # PageDown
# bindkey -s '^l' "|less\n" # ctrl-L pipes to less
# bindkey -s '^b' " &\n" # ctrl-B runs it in the background
# insert unicode character
# usage example: 'ctrl-x i' 00A7 'ctrl-x i' will give you an §
# See for example http://unicode.org/charts/ for unicode characters code
zrcautoload insert-unicode-char
zle -N insert-unicode-char
#k# Insert Unicode character
bindkey '^xi' insert-unicode-char
#m# k Shift-tab Perform backwards menu completion
if [[ -n "$terminfo[kcbt]" ]]; then
bindkey "$terminfo[kcbt]" reverse-menu-complete
elif [[ -n "$terminfo[cbt]" ]]; then # required for GNU screen
bindkey "$terminfo[cbt]" reverse-menu-complete
fi
## 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
}
#k# Toggle abbreviation expansion on/off
zle -N grml_toggle_abbrev
bindkey '^xA' 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
bindkey "^x^h" 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
#k# Kill left-side word or everything up to next slash
bindkey '\ev' slash-backward-kill-word
#k# Kill left-side word or everything up to next slash
bindkey '\e^h' slash-backward-kill-word
#k# Kill left-side word or everything up to next slash
bindkey '\e^?' slash-backward-kill-word
# use the new *-pattern-* widgets for incremental history search
if is439 ; then
bindkey '^r' history-incremental-pattern-search-backward
bindkey '^s' history-incremental-pattern-search-forward
fi
# a generic accept-line wrapper
# 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
# 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 ',.' 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
matched_chars='[.-|_a-zA-Z0-9]#'
LBUFFER=${LBUFFER%%(#m)[.-|_a-zA-Z0-9]#}
LBUFFER+=${abk[$MATCH]:-$MATCH}
}
zle -N zleiab && bindkey ",." zleiab
#f# display contents of assoc array $abk
help-show-abk()
{
zle -M "$(print "Type ,. after these abbreviations to expand them:"; print -a -C 2 ${(kv)abk})"
}
#k# Display list of abbreviations that expand when followed by ,.
zle -N help-show-abk && bindkey '^xb' help-show-abk
# autoloading
zrcautoload zmv # who needs mmv or rename?
zrcautoload history-search-end
# we don't want to quote/espace URLs on our own...
# if autoload -U url-quote-magic ; then
# zle -N self-insert url-quote-magic
# zstyle ':url-quote-magic:*' url-metas '*?[]^()~#{}='
# else
# print 'Notice: no url-quote-magic available :('
# fi
alias url-quote='autoload -U url-quote-magic ; zle -N self-insert url-quote-magic'
#m# k ESC-h Call \kbd{run-help} for the 1st word on the command line
alias run-help >&/dev/null && unalias run-help
for rh in run-help{,-git,-svk,-svn}; do
zrcautoload $rh
done; unset rh
# completion system
if zrcautoload compinit ; then
compinit || print 'Notice: no compinit available :('
else
print 'Notice: no compinit available :('
function zstyle { }
function compdef { }
fi
is4 && zrcautoload zed # use ZLE editor to edit a file or function
is4 && \
for mod in complist deltochar mathfunc ; do
zmodload -i zsh/${mod} 2>/dev/null || print "Notice: no ${mod} available :("
done
# autoload zsh modules when they are referenced
if is4 ; then
zmodload -a zsh/stat zstat
zmodload -a zsh/zpty zpty
zmodload -ap zsh/mapfile mapfile
fi
if is4 && zrcautoload insert-files && zle -N insert-files ; then
#k# Insert files and test globbing
bindkey "^xf" insert-files # C-x-f
fi
bindkey ' ' magic-space # also do history expansion on space
#k# Trigger menu-complete
bindkey '\ei' menu-complete # menu completion via esc-i
# press esc-e for editing command line in $EDITOR or $VISUAL
if is4 && zrcautoload edit-command-line && zle -N edit-command-line ; then
#k# Edit the current line in \kbd{\$EDITOR}
bindkey '\ee' edit-command-line
fi
if is4 && [[ -n ${(k)modules[zsh/complist]} ]] ; then
#k# menu selection: pick item but stay in the menu