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