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