99642aa59dc2525bd179ecd7447f634904ffad82
[grml-etc-core.git] / etc / zsh / zshrc
1 # Filename:      zshrc
2 # Purpose:       config file for zsh (z shell)
3 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
4 # Bug-Reports:   see http://grml.org/bugs/
5 # License:       This file is licensed under the GPL v2.
6 # Latest change: Mit Okt 25 23:44:27 CEST 2006 [mika]
7 ################################################################################
8 # This file is sourced only for interactive shells. It
9 # should contain commands to set up aliases, functions,
10 # options, key bindings, etc.
11 #
12 # Global Order: zshenv, zprofile, zshrc, zlogin
13 ################################################################################
14
15 # {{{ check for version/system
16 # check for versions (compatibility reasons)
17   if autoload is-at-least && is-at-least ; then
18      is4() { is-at-least 4 }
19      is42() { is-at-least 4.2 }
20   else
21     is4(){
22       [[ $ZSH_VERSION == 4.* ]] && return 0
23       return 1
24     }
25     is42(){
26       [[ $ZSH_VERSION == 4.<2->* ]] && return 0
27       return 1
28     }
29   fi
30
31 # grml specific stuff
32   isgrml(){
33     [ -f /etc/grml_version ] && return 0
34     return 1
35   }
36
37   isgrmlcd(){
38     [ -f /etc/grml_cd ] && return 0
39     return 1
40   }
41
42 # check for user, if not running as root set $SUDO to sudo
43  (( EUID != 0 )) && SUDO='sudo' || SUDO=''
44
45 # change directory to home on first invocation of zsh
46 # important for rungetty -> autologin
47 # Thanks go to Bart Schaefer!
48   isgrml && checkhome() {
49   if [[ -z "$ALREADY_DID_CD_HOME" ]]; then
50      export ALREADY_DID_CD_HOME=$HOME
51      cd
52   fi
53   }
54 # }}}
55
56 # {{{ set some variables
57   export EDITOR=${EDITOR:-vim}
58   export SHELL=${SHELL:-/bin/zsh}
59   export MAIL=${MAIL:-/var/mail/$USER}
60   [[ -f ~/.terminfo/m/mostlike ]] && MYLESS='LESS=C TERMINFO=~/.terminfo TERM=mostlike less' || MYLESS='less'
61   [[ -x /usr/bin/dircolors ]] && eval `dircolors -b`
62
63 # Search path for the cd comman
64 #  cdpath=(.. ~)
65
66 # Support our own site-functions
67   [ -d /etc/zsh/site-functions ] && export FPATH=/etc/zsh/site-functions:$FPATH
68
69 # automatically remove duplicates from these arrays
70   typeset -U path cdpath fpath manpath
71 # }}}
72
73 # {{{ keybindings
74  if [[ "$TERM" != emacs ]]; then
75   [[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char
76   [[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line
77   [[ -z "$terminfo[kend]"  ]] || bindkey -M emacs "$terminfo[kend]"  end-of-line
78   [[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]" vi-delete-char
79   [[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]" vi-beginning-of-line
80   [[ -z "$terminfo[kend]"  ]] || bindkey -M vicmd "$terminfo[kend]"  vi-end-of-line
81   [[ -z "$terminfo[cuu1]"  ]] || bindkey -M viins "$terminfo[cuu1]"  vi-up-line-or-history
82   [[ -z "$terminfo[cuf1]"  ]] || bindkey -M viins "$terminfo[cuf1]"  vi-forward-char
83   [[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]" vi-up-line-or-history
84   [[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]" vi-down-line-or-history
85   [[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]" vi-forward-char
86   [[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]" vi-backward-char
87   # ncurses stuff:
88   [[ "$terminfo[kcuu1]" == "\eO"* ]] && bindkey -M viins "${terminfo[kcuu1]/O/[}" vi-up-line-or-history
89   [[ "$terminfo[kcud1]" == "\eO"* ]] && bindkey -M viins "${terminfo[kcud1]/O/[}" vi-down-line-or-history
90   [[ "$terminfo[kcuf1]" == "\eO"* ]] && bindkey -M viins "${terminfo[kcuf1]/O/[}" vi-forward-char
91   [[ "$terminfo[kcub1]" == "\eO"* ]] && bindkey -M viins "${terminfo[kcub1]/O/[}" vi-backward-char
92   [[ "$terminfo[khome]" == "\eO"* ]] && bindkey -M viins "${terminfo[khome]/O/[}" beginning-of-line
93   [[ "$terminfo[kend]"  == "\eO"* ]] && bindkey -M viins "${terminfo[kend]/O/[}"  end-of-line
94   [[ "$terminfo[khome]" == "\eO"* ]] && bindkey -M emacs "${terminfo[khome]/O/[}" beginning-of-line
95   [[ "$terminfo[kend]"  == "\eO"* ]] && bindkey -M emacs "${terminfo[kend]/O/[}"  end-of-line
96 fi
97
98 ## keybindings (run 'bindkeys' for details, more details via man zshzle)
99 # use emacs style per default
100   bindkey -e
101 # use vi style:
102 # bindkey -v
103
104 #if [[ "$TERM" == screen ]]; then
105   bindkey '\e[1~' beginning-of-line       # home
106   bindkey '\e[4~' end-of-line             # end
107   bindkey "^[[A"  up-line-or-search       # cursor up
108   bindkey "^[[B"  down-line-or-search     # <ESC>-
109   bindkey '^x'    history-beginning-search-backward # alternative ways of searching the shell history
110 # bindkey -s '^L' "|less\n"             # ctrl-L pipes to less
111 # bindkey -s '^B' " &\n"                # ctrl-B runs it in the background
112 # if terminal type is set to 'rxvt':
113   bindkey '\e[7~' beginning-of-line       # home
114   bindkey '\e[8~' end-of-line             # end
115 #fi
116
117 #  bindkey '\eq' push-line-or-edit
118 # }}}
119
120 # {{{ autoloading
121   autoload -U zmv    # who needs mmv or rename?
122   autoload history-search-end
123
124   # we don't want to quote/espace URLs on our own...
125   # avoid 'url-quote-magic: function definition file not found' on some older boxes
126   if [ -f "/usr/share/zsh/$ZSH_VERSION/functions/Zle/url-quote-magic" ] ; then
127      autoload -U url-quote-magic && zle -N self-insert url-quote-magic
128   else
129      print 'Notice: no url-quote-magic available :('
130   fi
131
132   alias run-help >&/dev/null && unalias run-help
133   autoload run-help # use via 'esc-h'
134
135 # completion system
136   is42 && if autoload -U compinit && [ -f "/usr/share/zsh/$ZSH_VERSION/functions/Completion/compinit" ] ; then
137     compinit
138    else
139      print 'Notice: no compinit available :('
140      function zstyle { }
141      function compdef { }
142   fi
143   is4 && autoload -U zed                  # use ZLE editor to edit a file or function
144
145   is4 && \
146   for mod in complist deltochar mathfunc ; do
147     zmodload -i zsh/${mod}
148   done
149 # autoload zsh modules when they are referenced
150   is4 && \
151   for opt mod in a  stat    \
152                  a  zpty    \
153                  a  zprof   \
154                  ap mapfile ; do
155     zmodload -${opt} zsh/${mod} ${mod}
156   done
157   unset opt mod
158
159   is4 && autoload -U insert-files && \
160   zle -N insert-files && \
161   bindkey "^Xf" insert-files # C-x-f
162
163   bindkey ' '   magic-space    # also do history expansion on space
164   bindkey '\ei' menu-complete  # menu completion via esc-i
165
166 # press esc-e for editing command line in $EDITOR or $VISUAL
167   is4 && autoload -U edit-command-line && \
168   zle -N edit-command-line && \
169   bindkey '\ee' edit-command-line
170
171 ## menu selection: pick item but stay in the menu (press esc-return)
172   is4 && bindkey -M menuselect '\e^M' accept-and-menu-complete
173
174 # press "ctrl-e d" to insert the actual date in the form yyyy-mm-dd
175   _bkdate() { BUFFER="$BUFFER$(date '+%F')"; CURSOR=$#BUFFER; }
176   bindkey '\C-ed' _bkdate
177   zle -N _bkdate
178
179 # press esc-m for inserting last typed word again (thanks to caphuso!)
180   insert-last-typed-word() { zle insert-last-word -- 0 -1 }; \
181   zle -N insert-last-typed-word; bindkey "\em" insert-last-typed-word
182
183 # set command prediction from history, see 'man 1 zshcontrib'
184 #  is4 && autoload -U predict-on && \
185 #  zle -N predict-on         && \
186 #  zle -N predict-off        && \
187 #  bindkey "^X^Z" predict-on && \
188 #  bindkey "^Z" predict-off
189
190 # put job into foreground via ctrl-z:
191   bindkey -s '^z' "fg\n"
192
193 # press ctrl-q to quote line:
194 #  mquote () {
195 #        zle beginning-of-line
196 #        zle forward-word
197 #        # RBUFFER="'$RBUFFER'"
198 #        RBUFFER=${(q)RBUFFER}
199 #        zle end-of-line
200 #  }
201 #  zle -N mquote && bindkey '^q' mquote
202
203 # run command line as user root via sudo:
204   _sudo-command-line() {
205     [[ $BUFFER != sudo\ * ]] && LBUFFER="sudo $LBUFFER"
206   }
207   zle -N sudo-command-line _sudo-command-line
208   bindkey "^Os" sudo-command-line
209 # }}}
210
211 # {{{ set some important options
212   umask 022
213
214 # history:
215   setopt append_history       # append history list to the history file (important for multiple parallel zsh sessions!)
216   is4 && setopt SHARE_HISTORY # import new commands from the history file also in other zsh-session
217   setopt extended_history     # save each command's beginning timestamp and the duration to the history file
218   is4 && setopt histignorealldups # If  a  new  command  line being added to the history
219                               # list duplicates an older one, the older command is removed from the list
220   setopt histignorespace      # remove command lines from the history list when
221                               # the first character on the line is a space
222 #  setopt histallowclobber    # add `|' to output redirections in the history
223 #  setopt NO_clobber          # warning if file exists ('cat /dev/null > ~/.zshrc')
224   setopt auto_cd              # if a command is issued that can't be executed as a normal command,
225                               # and the command is the name of a directory, perform the cd command to that directory
226   setopt extended_glob        # in order to use #, ~ and ^ for filename generation
227                               # grep word *~(*.gz|*.bz|*.bz2|*.zip|*.Z) ->
228                               # -> searches for word not in compressed files
229                               # don't forget to quote '^', '~' and '#'!
230   setopt notify               # report the status of backgrounds jobs immediately
231   setopt hash_list_all        # Whenever a command completion is attempted, make sure \
232                               # the entire command path is hashed first.
233   setopt completeinword       # not just at the end
234 # setopt nocheckjobs          # don't warn me about bg processes when exiting
235   setopt nohup                # and don't kill them, either
236 # setopt printexitvalue       # alert me if something failed
237 # setopt dvorak               # with spelling correction, assume dvorak kb
238   setopt auto_pushd           # make cd push the old directory onto the directory stack.
239   setopt nonomatch            # try to avoid the 'zsh: no matches found...'
240   setopt nobeep               # avoid "beep"ing
241
242   MAILCHECK=30       # mailchecks
243   REPORTTIME=5       # report about cpu-/system-/user-time of command if running longer than 5 secondes
244   watch=(notme root) # watch for everyone but me and root
245
246 # define word separators (for stuff like backward-word, forward-word, backward-kill-word,..)
247 #  WORDCHARS='*?_-.[]~=/&;!#$%^(){}<>' # the default
248 #  WORDCHARS=.
249 #  WORDCHARS='*?_[]~=&;!#$%^(){}'
250 #  WORDCHARS='${WORDCHARS:s@/@}'
251
252 # only slash should be considered as a word separator:
253   slash-backward-kill-word() {
254     local WORDCHARS="${WORDCHARS:s@/@}"
255     # zle backward-word
256     zle backward-kill-word
257   }
258   zle -N slash-backward-kill-word
259   bindkey '\ev' slash-backward-kill-word # press esc-v to delete a word until its last '/' (not the same as ctrl-w!)
260 # }}}
261
262 # {{{ history
263   export ZSHDIR=$HOME/.zsh
264   HISTFILE=$HOME/.zsh_history
265   isgrmlcd && HISTSIZE=500  || HISTSIZE=5000
266   isgrmlcd && SAVEHIST=1000 || SAVEHIST=10000 # useful for setopt append_history
267 # }}}
268
269 # {{{ display battery status on right side of prompt via running 'BATTERY=1 zsh'
270   if [ -n "$BATTERY" ] ; then
271      if [ -x =acpi ] ; then
272         PERCENT="${(C)${(s| |)$(acpi 2>/dev/null)}[4]}"
273         [ -z "$PERCENT" ] && PERCENT='acpi not present'
274         if [ "${PERCENT%%%}" -lt 20 ] ; then
275            PERCENT="warning: ${PERCENT}%"
276         fi
277      fi
278   fi
279 # }}}
280
281 # {{{ set prompt
282   if [ -f "/usr/share/zsh/$ZSH_VERSION/functions/Prompts/promptinit" ] ; then
283      autoload promptinit && promptinit # people should be able to use their favourite prompt
284   else
285      print 'Notice: no prompiinit available :('
286   fi
287   # precmd() => a function which is executed just before each prompt
288   # precmd () { setopt promptsubst; [[ -o interactive ]] && jobs -l;
289   # run 'NOPRECMD=1 zsh' to disable the precmd + preexec commands
290   is4 && ! [[ -n "$NOPRECMD" ]] && precmd () {
291       if [ -n "$BATTERY" ] ; then
292         RPROMPT="%(?..:()% ${PERCENT}${SCREENTITLE}"
293       else
294         RPROMPT="%(?..:()% ${SCREENTITLE}"
295       fi
296       # adjust title of xterm
297       # see http://www.faqs.org/docs/Linux-mini/Xterm-Title.html
298       case $TERM in (xterm*|rxvt)
299         print -Pn "\e]0;%n@%m: %~\a"
300         ;;
301       esac
302   }
303
304   # chpwd () => a function which is executed whenever the directory is changed
305
306   # preexec() => a function running before every command
307   is4 && ! [[ -n "$NOPRECMD" ]] && preexec () {
308   # set screen window title if running in a screen
309   # get the name of the program currently running and hostname of local machine
310       local HOSTNAME=$(hostname)
311       if [[ "$HOSTNAME" != grml ]] ; then
312          NAME="@$HOSTNAME"
313       fi
314       if [[ "$TERM" == screen* ]]; then
315 #          local CMD=${1[(wr)^(*=*|sudo|ssh|-*)]}       # dont't use hostname
316           local CMD="${1[(wr)^(*=*|sudo|ssh|-*)]}$NAME" # use hostname
317           echo -ne "\ek$CMD\e\\"
318       fi
319   # set the screen title to "zsh" when sitting at the command prompt:
320       if [[ "$TERM" == screen* ]]; then
321            SCREENTITLE=$'%{\ekzsh\e\\%}'
322       else
323            SCREENTITLE=''
324       fi
325   # adjust title of xterm
326       case $TERM in (xterm*|rxvt)
327         print -Pn "\e]0;%n@%m: $1\a"
328         ;;
329       esac
330   }
331
332 # set colors
333   if autoload colors && colors ; then
334      BLUE="%{${fg[blue]}%}"
335      RED="%{${fg_bold[red]}%}"
336      GREEN="%{${fg[green]}%}"
337      CYAN="%{${fg[cyan]}%}"
338      WHITE="%{${fg[white]}%}"
339      NO_COLOUR="%{${reset_color}%}"
340   else
341      BLUE="%{\e[1;34m%}"
342      RED="%{\e[1;31m%}"
343      GREEN="%{\e[1;32m%}"
344      CYAN="%{\e[1;36m%}"
345      WHITE="%{\e[1;37m%}"
346      NO_COLOUR="%{\e[0m%}"
347   fi
348
349   EXITCODE="%(?..%?%1v )"
350   PS2='`%_> '       # secondary prompt, printed when the shell needs more information to complete a command.
351   PS3='?# '         # selection prompt used within a select loop.
352   PS4='+%N:%i:%_> ' # the execution trace prompt (setopt xtrace). default: '+%N:%i>'
353
354 # set variable debian_chroot if running in a chroot with /etc/debian_chroot
355   if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
356     debian_chroot=$(cat /etc/debian_chroot)
357   fi
358
359 # don't use colors on dumb terminals (like emacs):
360   if [[ "$TERM" == dumb ]] ; then
361     PROMPT="${EXITCODE}${debian_chroot:+($debian_chroot)}%n@%m %40<...<%B%~%b%<< %# "
362   else
363     # only if $GRMLPROMPT is set (e.g. via 'GRMLPROMPT=1 zsh') use the extended prompt
364     # set variable identifying the chroot you work in (used in the prompt below)
365     if [[ -n "$GRMLPROMPT" ]]; then
366       PROMPT="${RED}${EXITCODE}${CYAN}[%j running job(s)] ${GREEN}{history#%!} ${RED}%(3L.+.) ${BLUE}%* %D
367 ${BLUE}%n${NO_COLOUR}@%m %40<...<%B%~%b%<< %# "
368     else
369       if (( EUID != 0 )); then
370         PROMPT="${RED}${EXITCODE}${WHITE}${debian_chroot:+($debian_chroot)}${BLUE}%n${NO_COLOUR}@%m %40<...<%B%~%b%<< %# " # primary prompt string
371       else
372         PROMPT="${BLUE}${EXITCODE}${WHITE}${debian_chroot:+($debian_chroot)}${RED}%n${NO_COLOUR}@%m %40<...<%B%~%b%<< %# " # primary prompt string
373       fi
374     fi
375   fi
376
377 # if we are inside a grml-chroot set a specific prompt theme
378   if [ -n "$GRML_CHROOT" ] ; then
379      PROMPT="%{$fg[red]%}(CHROOT) %{$fg_bold[red]%}%n%{$fg_no_bold[white]%}@%m %40<...<%B%~%b%<< %\# "
380   fi
381 # }}}
382
383 # {{{ 'hash' some often used directories
384   hash -d deb=/var/cache/apt/archives
385   hash -d doc=/usr/share/doc
386   hash -d linux=/lib/modules/$(command uname -r)/build/
387   hash -d log=/var/log
388   hash -d slog=/var/log/syslog
389   hash -d src=/usr/src
390   hash -d templ=/usr/share/doc/grml-templates
391   hash -d tt=/usr/share/doc/texttools-doc
392   hash -d www=/var/www
393 # }}}
394
395 # {{{ some aliases
396   if [ $UID = 0 ] ; then
397      [ -r /etc/grml/screenrc ] && alias screen='/usr/bin/screen -c /etc/grml/screenrc'
398   elif [ -r $HOME/.screenrc ] ; then
399      alias screen="/usr/bin/screen -c $HOME/.screenrc"
400   else
401      [ -r /etc/grml/screenrc_grml ] && alias screen='/usr/bin/screen -c /etc/grml/screenrc_grml'
402   fi
403
404   if ls --help 2>/dev/null |grep -- --color= >/dev/null && [ "$TERM" != dumb ] ; then
405      alias ls='ls -b -CF --color=auto' # do we have GNU ls with color-support?
406      alias la='ls -la --color=auto'
407      alias ll='ls -l --color=auto'
408      alias lh='ls -hAl --color=auto'
409      alias l='ls -lF --color=auto'
410   else
411      alias ls='ls -b -CF'
412      alias la='ls -la'
413      alias ll='ls -l'
414      alias lh='ls -hAl'
415      alias l='ls -lF'
416   fi
417
418   alias cp='nocorrect cp'         # no spelling correction on cp
419   alias mkdir='nocorrect mkdir'   # no spelling correction on mkdir
420   alias mv='nocorrect mv'         # no spelling correction on mv
421   alias rm='nocorrect rm'         # no spelling correction on rm
422
423   alias rd='rmdir'
424   alias md='mkdir'
425
426   alias swspeak="setopt singlelinezle ; unsetopt prompt_cr ; export PS1='%m%# ' ; speechd-up" # set up software synth.
427
428 # truecrypt; use e.g. via 'truec /dev/ice' /mnt/ice' or 'truec -i'
429   if [ -x /usr/sbin/truecrypt ] ; then
430      alias truec='truecrypt --mount-options "rw,sync,dirsync,users,uid=1000,gid=users,umask=077" '
431   fi
432
433   zsh-help(){print "$bg[white]$fg[black]
434 zsh-help - hints for use of zsh on grml
435 =======================================$reset_color
436
437 Main configuration of zsh happens in /etc/zsh/zshrc (global)
438 and /etc/skel/.zshrc which is copied to \$HOME/.zshrc once.
439 The files are part of the package grml-etc, if you want to
440 use them on a non-grml-system just get the tar.gz from
441 http://grml.org/repos/
442
443 If you want to stay in sync with zsh configuration of grml
444 run 'ln -sf /etc/skel/.zshrc \$HOME/.zshrc' and configure
445 your own stuff in \$HOME/.zshrc.local. System wide configuration
446 without touching configuration files of grml can take place
447 in /etc/zsh/zshrc.local.
448
449 If you want to use the configuration of user grml also when
450 running as user root just run 'zshskel' which will source
451 the file /etc/skel/.zshrc.
452
453 For information regarding zsh start at http://grml.org/zsh/
454
455 Take a look at grml's zsh refcard:
456 % xpdf =(zcat /usr/share/doc/grml-docs/zsh/grml-zsh-refcard.pdf.gz)
457
458 Check out the main zsh refcard:
459 % $BROWSER http://www.bash2zsh.com/zsh_refcard/refcard.pdf
460
461 And of course visit the zsh-lovers:
462 % man zsh-lovers
463
464 You can adjust some options through environment variables when
465 invoking zsh without having to edit configuration files.
466 Basically meant for bash users who are not used to the power of
467 the zsh yet. :)
468
469   \"NOCOR=1    zsh\" => deactivate automatic correction
470   \"NOMENU=1   zsh\" => do not use menu completion (note: use strg-d for completion instead!)
471   \"NOPRECMD=1 zsh\" => disable the precmd + preexec commands (set GNU screen title)
472   \"BATTERY=1  zsh\" => activate battery status (via acpi) on right side of prompt
473 $bg[white]$fg[black]
474 Please report wishes + bugs to the grml-team: http://grml.org/bugs/
475 Enjoy your grml system with the zsh!$reset_color"
476 }
477
478 # debian stuff
479   if [ -r /etc/debian_version ] ; then
480     alias acs="apt-cache search"
481     alias acsh="apt-cache show"
482     alias adg="$SUDO apt-get dist-upgrade"
483     alias agi="$SUDO apt-get install"
484     alias ag="$SUDO apt-get upgrade"
485     alias au="$SUDO apt-get update"
486     alias dbp="dpkg-buildpackage"
487     alias ge="grep-excuses"
488
489     isgrmlcd && alias su="sudo su"          # change to user root
490     alias tlog="tail -f /var/log/syslog"    # take a look at the syslog
491     alias zshskel="source /etc/skel/.zshrc" # source skeleton zshrc
492   fi
493
494 # if cdrecord is a symlink (to wodim) or isn't present at all warn:
495   if [ -L /usr/bin/cdrecord -o ! -x =cdrecord ] ; then
496      if [ -x =wodim ] ; then
497         alias cdrecord="echo 'cdrecord is not provided under its original name by Debian anymore.
498 See #377109 in the BTS of Debian for more details.
499
500 Please use the wodim binary instead' ; return 1"
501      fi
502   fi
503
504 # }}}
505
506 # {{{ Use hard limits, except for a smaller stack and no core dumps
507   unlimit
508   limit stack 8192
509   limit core 0      # important for a live-cd-system
510   limit -s
511 # }}}
512
513 # {{{ completion stuff
514
515 # Where to look for autoloaded function definitions
516   if [ -d /etc/zsh/completion.d ] ; then
517     local comp=/etc/zsh/completion.d
518     for func in $comp/*(N-.:t); . ${comp}/${func}
519   fi
520
521 # called later (via is4 && grmlcomp)
522 # notice: use 'zstyle' for getting current settings
523 #         press ^Xh (control-x h) for getting tags in context; ^X? (control-x ?) to run complete_debug with trace output
524 grmlcomp() {
525 ## completion system
526   zstyle ':completion:*:approximate:'    max-errors 'reply=( $((($#PREFIX+$#SUFFIX)/3 )) numeric )' # allow one error for every three characters typed in approximate completer
527   zstyle ':completion:*:complete:-command-::commands' ignored-patterns '*\~' # don't complete backup files as executables
528   zstyle ':completion:*:correct:*'       insert-unambiguous true             # start menu completion only if it could find no unambiguous initial string
529   zstyle ':completion:*:corrections'     format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}' #
530   zstyle ':completion:*:correct:*'       original true                       #
531   zstyle ':completion:*:default'         list-colors ${(s.:.)LS_COLORS}      # activate color-completion(!)
532   zstyle ':completion:*:descriptions'    format $'%{\e[0;31m%}completing %B%d%b%{\e[0m%}'  # format on completion
533   zstyle ':completion:*:*:cd:*:directory-stack' menu yes select              # complete 'cd -<tab>' with menu
534   zstyle ':completion:*:expand:*'        tag-order all-expansions            # insert all expansions for expand completer
535   zstyle ':completion:*:history-words'   list false                          #
536   zstyle ':completion:*:history-words'   menu yes                            # activate menu
537   zstyle ':completion:*:history-words'   remove-all-dups yes                 # ignore duplicate entries
538   zstyle ':completion:*:history-words'   stop yes                            #
539   zstyle ':completion:*:*:linda:*'       file-patterns '*.deb'               # complete debian packages for command 'linda'
540   zstyle ':completion:*:*:lintian:*'     file-patterns '*.deb'               # complete debian packages for command 'lintian'
541   zstyle ':completion:*'                 matcher-list 'm:{a-z}={A-Z}'        # match uppercase from lowercase
542   zstyle ':completion:*:matches'         group 'yes'                         # separate matches into groups
543   zstyle ':completion:*'                 group-name ''
544   if [[ -z "$NOMENU" ]] ; then
545     zstyle ':completion:*'               menu select=5                       # if there are more than 5 options allow selecting from a menu
546   else
547     setopt no_auto_menu # don't use any menus at all
548   fi
549   zstyle ':completion:*:messages'        format '%d'                         #
550   zstyle ':completion:*:options'         auto-description '%d'               #
551   zstyle ':completion:*:options'         description 'yes'                   # describe options in full
552   zstyle ':completion:*:processes'       command 'ps -au$USER'               # on processes completion complete all user processes
553   zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters        # offer indexes before parameters in subscripts
554   zstyle ':completion:*'                 verbose true                        # provide verbose completion information
555   zstyle ':completion:*:warnings'        format $'%{\e[0;31m%}No matches for:%{\e[0m%} %d' # set format for warnings
556   zstyle ':completion:*:*:zcompile:*'    ignored-patterns '(*~|*.zwc)'       # define files to ignore for zcompile
557   zstyle ':completion:correct:'          prompt 'correct to: %e'             #
558   zstyle ':completion::(^approximate*):*:functions' ignored-patterns '_*'    # Ignore completion functions for commands you don't have:
559
560 # complete manual by their section
561   zstyle ':completion:*:manuals'    separate-sections true
562   zstyle ':completion:*:manuals.*'  insert-sections   true
563   zstyle ':completion:*:man:*'      menu yes select
564
565 ## correction
566 # run rehash on completion so new installed program are found automatically:
567   _force_rehash() {
568       (( CURRENT == 1 )) && rehash
569          return 1 # Because we didn't really complete anything
570     }
571 # some people don't like the automatic correction - so run 'NOCOR=1 zsh' to deactivate it
572   if [[ -n "$NOCOR" ]] ; then
573     zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete
574     setopt nocorrect # do not try to correct the spelling if possible
575   else
576 #    zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete _correct _approximate
577     setopt correct  # try to correct the spelling if possible
578     zstyle -e ':completion:*' completer '
579         if [[ $_last_try != "$HISTNO$BUFFER$CURSOR" ]]; then
580           _last_try="$HISTNO$BUFFER$CURSOR"
581           reply=(_complete _match _prefix)
582         else
583           if [[ $words[1] = (rm|mv) ]]; then
584             reply=(_complete)
585           else
586             reply=(_oldlist _expand _force_rehash _complete _correct _approximate)
587           fi
588         fi'
589   fi
590 # zstyle ':completion:*' completer _complete _correct _approximate
591 # zstyle ':completion:*' expand prefix suffix
592
593 # automatic rehash? Credits go to Frank Terbeck
594 # function my_accept () {
595 #   local buf
596 #   [[ -z ${BUFFER} ]] && zle accept-line && return
597 #   buf=( ${(z)BUFFER}  )
598 #   [[ -z ${commands[${buf[1]}]} ]] && rehash
599 #   zle accept-line
600 # }
601 # zle -N my_accept
602 # bindkey "^M" my_accept
603
604 # command for process lists, the local web server details and host completion
605   hosts=(`hostname` grml.org)
606   zstyle '*' hosts $hosts
607   zstyle ':completion:*:urls' local 'www' '/var/www/' 'public_html'
608
609 # caching
610   [ -d $ZSHDIR/cache ] && zstyle ':completion:*' use-cache yes && \
611                           zstyle ':completion::complete:*' cache-path $ZSHDIR/cache/
612
613 # use ~/.ssh/known_hosts for completion [does not work with hashing of new ssh versions anymore]
614   if [ -f "$HOME/.ssh/known_hosts" ] ; then
615     hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*})
616     zstyle ':completion:*:hosts' hosts $hosts
617   fi
618
619 # use generic completion system for programs not yet defined:
620   compdef _gnu_generic tail head feh cp mv gpg df stow uname ipacsum fetchipac
621
622 # see upgrade function in this file
623   compdef _hosts upgrade
624 }
625 # }}}
626
627 # {{{ grmlstuff
628 grmlstuff() {
629 # people should use 'grml-x'!
630   function startx() {
631     if [ -e /etc/X11/xorg.conf ] ; then
632        [ -x /usr/bin/startx ] && /usr/bin/startx || /usr/X11R6/bin/startx
633     else
634       echo "Please use the script \"grml-x\" for starting the X Window System
635 because there does not exist /etc/X11/xorg.conf yet.
636 If you want to use startx anyway please call \"/usr/bin/startx\"."
637       return -1
638     fi
639   }
640
641   function xinit() {
642     if [ -e /etc/X11/xorg.conf ] ; then
643        [ -x /usr/bin/xinit ] && /usr/bin/xinit || /usr/X11R6/bin/xinit
644     else
645       echo "Please use the script \"grml-x\" for starting the X Window System.
646 because there does not exist /etc/X11/xorg.conf yet.
647 If you want to use xinit anyway please call \"/usr/bin/xinit\"."
648       return -1
649     fi
650   }
651
652   if [ -x /usr/sbin/915resolution ] ; then
653      alias 855resolution='echo -e "Please use 915resolution as resolution modify tool for Intel graphic chipset."; return -1'
654   fi
655
656   alias grml-version='cat /etc/grml_version'
657
658   if [ -x /usr/sbin/rebuildfstab ] ; then
659      local fstabuser=$(getent passwd 1000 | cut -d: -f1)
660      alias grml-rebuildfstab="rebuildfstab -v -r -u $fstabuser -g $fstabuser"
661   fi
662 }
663 # }}}
664
665 # {{{ now run the functions
666   isgrml && checkhome
667   is4    && isgrml    && grmlstuff
668   is4    && grmlcomp
669 # }}}
670
671 # {{{ keephack
672   [ -r /etc/zsh/keephack ] && is4 && source /etc/zsh/keephack
673 # }}}
674
675 # {{{ wonderful idea of using "e" glob qualifier by Peter Stephenson
676 # You use it as follows:
677 # $ NTREF=/reference/file
678 # $ ls -l *(e:nt:)
679 # This lists all the files in the current directory newer than the reference file.
680 # You can also specify the reference file inline; note quotes:
681 # $ ls -l *(e:'nt ~/.zshenv':)
682   is4 && nt() {
683     if [[ -n $1 ]]; then
684       local NTREF=${~1}
685     fi
686     [[ $REPLY -nt $NTREF ]]
687   }
688 # }}}
689
690 # shell functions {{{
691   setenv()  { typeset -x "${1}${1:+=}${(@)argv[2,$#]}" }  # csh compatibility
692   freload() { while (( $# )); do; unfunction $1; autoload -U $1; shift; done }
693   manzsh()  {  /usr/bin/man zshall |  vim -c "se ft=man| se hlsearch" +/"$1" - ; }
694 #  manzsh()  { man zshall | $MYLESS -p $1 ; }
695 #  manzsh() {  /usr/bin/man zshall |  most +/"$1" ; }
696
697 # use "dchange <package-name>" to view Debian's changelog of the package:
698   dchange() {
699     if [ -r /usr/share/doc/${1}/changelog.Debian.gz ] ; then
700        most /usr/share/doc/${1}/changelog.Debian.gz
701     else
702        if [ -r /usr/share/doc/${1}/changelog.gz ] ; then
703           most /usr/share/doc/${1}/changelog.gz
704        else
705           echo "No changelog for package $1 found, sorry."
706           return 1
707        fi
708     fi
709   }
710   _dchange() { _files -W /usr/share/doc -/ }
711   compdef _dchange dchange
712
713 # use "uchange <package-name>" to view upstream's changelog of the package:
714   uchange() {
715     if [ -r /usr/share/doc/${1}/changelog.gz ] ; then
716        most /usr/share/doc/${1}/changelog.gz
717     else
718        echo "No changelog for package $1 found, sorry."
719        return 1
720     fi
721   }
722   _uchange() { _files -W /usr/share/doc -/ }
723   compdef _uchange uchange
724
725 # edit alias via zle:
726   edalias() {
727     [ -z "$1" ] && { echo "Usage: edalias <alias_to_edit>" ; return 1 } || vared aliases'[$1]' ;
728   }
729   compdef _aliases edalias
730
731 # edit function via zle:
732   edfun() {
733     [ -z "$1" ] && { echo "Usage: edfun <function_to_edit>" ; return 1 } || zed -f "$1" ;
734   }
735   compdef _functions edfun
736
737 # use it e.g. via 'Restart apache2'
738  if [ -d /etc/init.d ] ; then
739   for i in Start Restart Stop Reload ; do
740     eval "$i() { $SUDO /etc/init.d/\$1 ${i:l} \$2 ; }"
741   done
742 # now the completion for this:
743   compctl -g "$(echo /etc/init.d/*(:t))" Start Restart Stop Reload
744  fi
745 # }}}
746
747 # make sure our environment is clean regarding colors
748   for color in BLUE RED GREEN CYAN WHITE ; unset $color
749
750 # source another config file if present {{{
751   if [ -r /etc/zsh/zshrc.local ]; then
752    source /etc/zsh/zshrc.local
753   fi
754 # }}}
755
756 # "persistent history" {{{
757 # just write important commands you always need to ~/.important_commands
758   if [ -r ~/.important_commands ] ; then
759      fc -R ~/.important_commands
760   fi
761 # }}}
762
763 ## END OF FILE #################################################################
764 # vim:foldmethod=marker