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