The 'preparation to a zsh workshop improves code quality' release. :)
[grml-etc-core.git] / etc / skel / .zshrc
1 # Filename:      .zshrc
2 # Purpose:       config file for zsh
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: Fre Nov 24 23:08:52 CET 2006 [mika]
7 ################################################################################
8
9 # source ~/.zshrc.global {{{
10 # see /etc/zsh/zshrc for some general settings
11 # If you don't have write permissions to /etc/zsh/zshrc on your own
12 # copy the file to your $HOME as /.zshrc.global and we source it:
13   if [ -r ~/.zshrc.global ] ; then
14      . ~/.zshrc.global
15   fi
16 # }}}
17
18 # completion system {{{
19 # just make sure it is loaded in this file too
20   type compinit &>/dev/null || { autoload -U compinit && compinit }
21 # }}}
22
23 # make sure isgrmlsmall is defined {{{
24   type isgrmlsmall &>/dev/null || function isgrmlsmall () { return 1 }
25 # }}}
26
27 ## variables {{{
28
29 # do you want grmlsmall-specific adjustments?
30   GRMLSMALL_SPECIFIC=1
31
32 # set terminal property (used e.g. by msgid-chooser)
33   export COLORTERM="yes"
34
35 # set default browser
36   if [ -z "$BROWSER" ] ; then
37      if [ -n "$DISPLAY" ] ; then
38         [ -x =firefox ] && export BROWSER=firefox
39      else
40         [ -x =w3m ] && export BROWSER=w3m
41      fi
42   fi
43   (( ${+PAGER} ))   || export PAGER="less"
44
45 # export qtdir
46   [ -d /usr/share/qt3 ] && export QTDIR=/usr/share/qt3
47   [ -d /usr/share/qt4 ] && export QTDIR=/usr/share/qt4
48
49 # support running 'jikes *.java && jamvm HelloWorld' OOTB:
50   [ -f /usr/share/classpath/glibj.zip ] && export JIKESPATH=/usr/share/classpath/glibj.zip
51 # }}}
52
53 ## set options {{{
54
55 # Allow comments even in interactive shells i. e.
56 # $ uname # This command prints system informations
57 # zsh: bad pattern: #
58 # $ setopt interactivecomments
59 # $ uname # This command prints system informations
60 # Linux
61 #  setopt interactivecomments
62
63 # ctrl-s will no longer freeze the terminal.
64 #  stty erase "^?"
65
66 # }}}
67
68 # {{{ global aliases
69 # These do not have to be at the beginning of the command line.
70 # Avoid typing cd ../../ for going two dirs down and so on
71 # Usage, e.g.: "$ cd ...' or just '$ ...' with 'setopt auto_cd'
72 # Notice: deactivated by 061112 by default, use another approach - see 'power completion'
73 #  alias -g '...'='../..'
74 #  alias -g '....'='../../..'
75 #  alias -g BG='& exit'
76 #  alias -g C='|wc -l'
77 #  alias -g G='|grep'
78 #  alias -g H='|head'
79 #  alias -g Hl=' --help |& less -r'
80 #  alias -g K='|keep'
81 #  alias -g L='|less'
82 #  alias -g LL='|& less -r'
83 #  alias -g M='|most'
84 #  alias -g N='&>/dev/null'
85 #  alias -g R='| tr A-z N-za-m'
86 #  alias -g SL='| sort | less'
87 #  alias -g S='| sort'
88 #  alias -g T='|tail'
89 #  alias -g V='| vim -'
90
91 # power completion, see http://zshwiki.org/home/examples/zleiab
92 # less risky than the global aliases but powerful as well
93 # just type the abbreviation key and afterwards ',.' to expand it
94   declare -A abk
95   setopt extendedglob
96   setopt interactivecomments
97   abk=(
98    # key  # value
99    'C'    '| wc -l'
100    '...' '../..'
101    '....' '../../..'
102    'BG' '& exit'
103    'C' '|wc -l'
104    'G' '|& grep --color=auto'
105    'H' '|head'
106    'Hl' ' --help |& less -r'
107    'L' '|less'
108    'LL' '|& less -r'
109    'M' '|most'
110    'N' '&>/dev/null'
111    'R' '| tr A-z N-za-m'
112    'SL' '| sort | less'
113    'S' '| sort -u'
114    'T' '|tail'
115    'V' '|& vim -'
116    'hide' "echo -en '\033]50;nil2\007'"
117    'tiny' 'echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-80-*-*-c-*-iso8859-15\007"'
118    'small' 'echo -en "\033]50;6x10\007"'
119    'medium' 'echo -en "\033]50;-misc-fixed-medium-r-normal--13-120-75-75-c-80-iso8859-15\007"'
120    'default' 'echo -e "\033]50;-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-15\007"'
121    'large' 'echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-150-*-*-c-*-iso8859-15\007"'
122    'huge' 'echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-210-*-*-c-*-iso8859-15\007"'
123    'smartfont' 'echo -en "\033]50;-artwiz-smoothansi-*-*-*-*-*-*-*-*-*-*-*-*\007"'
124    'semifont' 'echo -en "\033]50;-misc-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-15\007"'
125    'da' 'du -sch'
126    'j' 'jobs -l'
127    'u' 'translate -i'
128    'co' "./configure && make && sudo make install"
129    'CH' "./configure --help"
130    'conkeror' 'firefox -chrome chrome://conkeror/content'
131    'dir' 'ls -lSrah'
132    'lad' $'ls -d .*(/)\n# only show dot-directories'
133    'lsa' $'ls -a .*(.)\n# only show dot-files'
134    'lss' $'ls -l *(s,S,t)\n# only files with setgid/setuid/sticky flag'
135    'lsl' $'ls -l *(@[1,10])\n# only symlinks'
136    'lsx' $'ls -l *(*[1,10])\n# only executables'
137    'lsw' $'ls -ld *(R,W,X.^ND/)\n# world-{readable,writable,executable} files'
138    'lsbig' $'ls -flh *(.OL[1,10])\n# display the biggest files'
139    'lsd' $'ls -d *(/)\n# only show directories'
140    'lse' $'ls -d *(/^F)\n# only show empty directories'
141    'lsnew' $'ls -rl *(D.om[1,10])\n# display the newest files'
142    'lsold' $'ls -rtlh *(D.om[-11,-1])\n # display the oldest files'
143    'lssmall' $'ls -Srl *(.oL[1,10])\n# display the smallest files'
144    'rw-' 'chmod 600'
145    '600' 'chmod u+rw-x,g-rwx,o-rwx'
146    'rwx' 'chmod u+rwx'
147    '700' 'chmod u+rwx,g-rwx,o-rwx'
148    'r--' 'chmod u+r-wx,g-rwx,o-rwx'
149    '644' $'chmod u+rw-x,g+r-wx,o+r-wx\n # 4=r,2=w,1=x'
150    '755' 'chmod u+rwx,g+r-w+x,o+r-w+x'
151    'md' 'mkdir -p '
152    'cmplayer' 'mplayer -vo -fs -zoom fbdev'
153    'fbmplayer' 'mplayer -vo -fs -zoom fbdev'
154    'fblinks' 'links2 -driver fb'
155    'insecssh' 'ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
156    'fori' 'for i ({..}) { }'
157    'cx' 'chmod +x'
158    'e'  'print -l'
159    'se' 'setopt interactivecomments'
160    'va' 'valac --vapidir=../vapi/ --pkg=gtk+-2.0 gtktest.vala'
161    'fb2' '=mplayer -vo fbdev -fs -zoom 1>/dev/null -xy 2'
162    'fb3' '=mplayer -vo fbdev -fs  -zoom 1>/dev/null -xy 3'
163    'ci' 'centericq'
164    'D'  'export DISPLAY=:0.0'
165    'mp' 'mplayer -vo xv -fs -zoom'
166   )
167   globalias () {
168         local MATCH
169         matched_chars='[.-|_a-zA-Z0-9]#'
170         LBUFFER=${LBUFFER%%(#m)[.-|_a-zA-Z0-9]#}
171         LBUFFER+=${abk[$MATCH]:-$MATCH}
172   }
173   zle -N globalias
174   bindkey ",." globalias
175 # }}}
176
177 ## aliases {{{
178
179 # Xterm resizing-fu.
180 # Based on http://svn.kitenet.net/trunk/home-full/.zshrc?rev=11710&view=log (by Joey Hess)
181   alias hide='echo -en "\033]50;nil2\007"'
182   alias tiny='echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-80-*-*-c-*-iso8859-15\007"'
183   alias small='echo -en "\033]50;6x10\007"'
184   alias medium='echo -en "\033]50;-misc-fixed-medium-r-normal--13-120-75-75-c-80-iso8859-15\007"'
185   alias default='echo -e "\033]50;-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-15\007"'
186   alias large='echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-150-*-*-c-*-iso8859-15\007"'
187   alias huge='echo -en "\033]50;-misc-fixed-medium-r-normal-*-*-210-*-*-c-*-iso8859-15\007"'
188   alias smartfont='echo -en "\033]50;-artwiz-smoothansi-*-*-*-*-*-*-*-*-*-*-*-*\007"'
189   alias semifont='echo -en "\033]50;-misc-fixed-medium-r-semicondensed-*-*-120-*-*-*-*-iso8859-15\007"'
190 #  if [ "$TERM" = "xterm" ] && [ "$LINES" -ge 50 ] && [ "$COLUMNS" -ge 100 ] && [ -z "$SSH_CONNECTION" ]; then
191 #          large
192 #  fi
193
194 # general
195   alias da='du -sch'
196   alias j='jobs -l'
197 #  alias u='translate -i'          # translate
198
199 # compile stuff
200   alias CO="./configure"
201   alias CH="./configure --help"
202
203 # http://conkeror.mozdev.org/
204   alias conkeror='firefox -chrome chrome://conkeror/content'
205
206 # arch/tla stuff
207   if type -p tla >/dev/null 2>&1 ; then
208      alias tdi='tla what-changed --diffs | less'
209      alias tbp='tla-buildpackage'
210      alias tmi='tla archive-mirror'
211      alias tco='tla commit'
212      alias tme='tla star-merge'
213   fi
214
215 # listing stuff
216   alias dir="ls -lSrah"
217   alias lad='ls -d .*(/)'                # only show dot-directories
218   alias lsa='ls -a .*(.)'                # only show dot-files
219   alias lss='ls -l *(s,S,t)'             # only files with setgid/setuid/sticky flag
220   alias lsl='ls -l *(@[1,10])'           # only symlinks
221   alias lsx='ls -l *(*[1,10])'           # only executables
222   alias lsw='ls -ld *(R,W,X.^ND/)'       # world-{readable,writable,executable} files
223   alias lsbig="ls -flh *(.OL[1,10])"     # display the biggest files
224   alias lsd='ls -d *(/)'                 # only show directories
225   alias lse='ls -d *(/^F)'               # only show empty directories
226   alias lsnew="ls -rl *(D.om[1,10])"     # display the newest files
227   alias lsold="ls -rtlh *(D.om[1,10])"   # display the oldest files
228   alias lssmall="ls -Srl *(.oL[1,10])"   # display the smallest files
229
230 # chmod
231   alias rw-='chmod 600'
232   alias rwx='chmod 700'
233   alias r--='chmod 644'
234   alias r-x='chmod 755'
235
236 # some useful aliases
237   alias md='mkdir -p'
238
239 # console stuff
240   alias cmplayer='mplayer -vo fbdev'
241   alias fbmplayer='mplayer -vo fbdev -fs -zoom'
242   alias fblinks='links2 -driver fb'
243
244 # ignore ~/.ssh/known_hosts entries
245 #  alias insecssh='ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null" -o "PreferredAuthentications=keyboard-interactive"'
246   alias insecssh='ssh -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null"'
247
248 # use colors when browsing man pages (if not using pinfo or PAGER=most)
249   [ -d ~/.terminfo/ ] && alias man='TERMINFO=~/.terminfo/ LESS=C TERM=mostlike PAGER=less man'
250
251 # check whether Debian's package management (dpkg) is running
252   alias check_dpkg_running="$SUDO dpkg_running"
253 # }}}
254
255 ## useful functions {{{
256
257 # functions without detailed explanation:
258   agoogle() { ${=BROWSER} "http://groups.google.com/groups?as_uauthors=$*" ; }
259   bk()      { cp -b ${1} ${1}_`date --iso-8601=m` }
260   cdiff()   { diff -crd "$*" | egrep -v "^Only in |^Binary files " }
261   cl()      { cd $1 && ls -a }        # cd && ls
262   cvsa()    { cvs add $* && cvs com -m 'initial checkin' $* }
263   cvsd()    { cvs diff -N $* |& $PAGER }
264   cvsl()    { cvs log $* |& $PAGER }
265   cvsq()    { cvs -nq update }
266   cvsr()    { rcs2log $* | $PAGER }
267   cvss()    { cvs status -v $* }
268   debbug()  { ${=BROWSER} "http://bugs.debian.org/$*" }
269   debbugm() { bts show --mbox $1 } # provide bugnummer as "$1"
270   disassemble(){ gcc -pipe -S -o - -O -g $* | as -aldh -o /dev/null }
271   dmoz()    { ${=BROWSER} http://search.dmoz.org/cgi-bin/search\?search=${1// /_} }
272   dwicti()  { ${=BROWSER} http://de.wiktionary.org/wiki/${(C)1// /_} }
273   ewicti()  { ${=BROWSER} http://en.wiktionary.org/wiki/${(C)1// /_} }
274   fir()     { firefox -a firefox -remote "openURL($1)" }
275   ggogle()  { ${=BROWSER} "http://groups.google.com/groups?q=$*" }
276   google()  { ${=BROWSER} "http://www.google.com/search?&num=100&q=$*" }
277   mdiff()   { diff -udrP "$1" "$2" > diff.`date "+%Y-%m-%d"`."$1" }
278   memusage(){ ps aux | awk '{if (NR > 1) print $5; if (NR > 2) print "+"} END { print "p" }' | dc }
279   mggogle() { ${=BROWSER} "http://groups.google.com/groups?selm=$*" }
280   netcraft(){ ${=BROWSER} "http://toolbar.netcraft.com/site_report?url=$1" }
281   oleo()    { ${=BROWSER} "http://dict.leo.org/?search=$*" }
282   shtar()   { gunzip -c $1 | tar -tf - -- | $PAGER }
283   shtgz()   { tar -ztf $1 | $PAGER }
284   shzip()   { unzip -l $1 | $PAGER }
285   sig()     { agrep -d '^-- $' "$*" ~/.Signature }
286   swiki()   { ${=BROWSER} http://de.wikipedia.org/wiki/Spezial:Search/${(C)1} }
287   udiff()   { diff -urd $* | egrep -v "^Only in |^Binary files " }
288   viless()  { vim --cmd 'let no_plugin_maps = 1' -c "so \$VIMRUNTIME/macros/less.vim" "${@:--}" }
289   wikide () { ${=BROWSER} http://de.wikipedia.org/wiki/"${(C)*}" }
290   wikien()  { ${=BROWSER} http://en.wikipedia.org/wiki/"$*" }
291   wodeb ()  { ${=BROWSER} "http://packages.debian.org/cgi-bin/search_contents.pl?word=$1&version=${2:-unstable}" }
292
293   which google >/dev/null 2>&1 && gex () { google "\"[ $1]\" $*" } # exact search at google
294
295 # Function Usage: doc packagename
296   doc() { cd /usr/share/doc/$1 && ls }
297   _doc() { _files -W /usr/share/doc -/ }
298   compdef _doc doc
299
300 # make screenshot of current desktop (use 'import' from ImageMagic)
301   sshot() {
302         [[ ! -d ~/shots  ]] && mkdir ~/shots
303         #cd ~/shots ; sleep 5 ; import -window root -depth 8 -quality 80 `date "+%Y-%m-%d--%H:%M:%S"`.png
304         cd ~/shots ; sleep 5; import -window root shot_`date --iso-8601=m`.jpg
305   }
306
307 # list images only
308   limg() {
309     local -a images
310     images=( *.{jpg,gif,png}(.N) )
311     if [[ $#images -eq 0 ]] ; then
312       print "No image files found"
313     else
314       ls "$@" "$images[@]"
315     fi
316   }
317
318 # create pdf file from source code
319   makereadable() {
320      output=$1
321      shift
322      a2ps --medium A4dj -E -o $output $*
323      ps2pdf $output
324   }
325
326 # zsh with perl-regex - use it e.g. via:
327 # regcheck '\s\d\.\d{3}\.\d{3} Euro' ' 1.000.000 Euro'
328   regcheck() {
329     zmodload -i zsh/pcre
330     pcre_compile $1 && \
331     pcre_match $2 && echo "regex matches" || echo "regex does not match"
332   }
333
334 # list files which have been modified within the last x days
335   new() { print -l *(m-$1) }
336
337 # grep the history
338   greph () { history 0 | grep $1 }
339   (grep --help 2>/dev/null |grep -- --color) >/dev/null && \
340     alias grep='grep --color=auto' # use colors when GNU grep with color-support
341   alias GREP='grep -i --color=auto'
342
343 # one blank line between each line
344   if [ -r ~/.terminfo/m/mostlike ] ; then
345 #     alias man2='MANPAGER="sed -e G |less" TERMINFO=~/.terminfo TERM=mostlike /usr/bin/man'
346      man2() { PAGER='dash -c "sed G | /usr/bin/less"' TERM=mostlike /usr/bin/man "$@" ; }
347   fi
348
349 # jump between directories
350 # Copyright 2005 Nikolai Weibull <nikolai@bitwi.se>
351 # notice: option AUTO_PUSHD has to be set
352   d(){
353     emulate -L zsh
354     autoload -U colors
355     local color=$fg_bold[blue]
356     integer i=0
357     dirs -p | while read dir; do
358       local num="${$(printf "%-4d " $i)/ /.}"
359       printf " %s  $color%s$reset_color\n" $num $dir
360       (( i++ ))
361     done
362     integer dir=-1
363     read -r 'dir?Jump to directory: ' || return
364     (( dir == -1 )) && return
365     if (( dir < 0 || dir >= i )); then
366       echo d: no such directory stack entry: $dir
367       return 1
368     fi
369     cd ~$dir
370   }
371
372 # provide useful information on globbing
373   H-Glob() {
374   echo -e "
375       /      directories
376       .      plain files
377       @      symbolic links
378       =      sockets
379       p      named pipes (FIFOs)
380       *      executable plain files (0100)
381       %      device files (character or block special)
382       %b     block special files
383       %c     character special files
384       r      owner-readable files (0400)
385       w      owner-writable files (0200)
386       x      owner-executable files (0100)
387       A      group-readable files (0040)
388       I      group-writable files (0020)
389       E      group-executable files (0010)
390       R      world-readable files (0004)
391       W      world-writable files (0002)
392       X      world-executable files (0001)
393       s      setuid files (04000)
394       S      setgid files (02000)
395       t      files with the sticky bit (01000)
396    print *(m-1)          # Dateien, die vor bis zu einem Tag modifiziert wurden.
397    print *(a1)           # Dateien, auf die vor einem Tag zugegriffen wurde.
398    print *(@)            # Nur Links
399    print *(Lk+50)        # Dateien die ueber 50 Kilobytes grosz sind
400    print *(Lk-50)        # Dateien die kleiner als 50 Kilobytes sind
401    print **/*.c          # Alle *.c - Dateien unterhalb von \$PWD
402    print **/*.c~file.c   # Alle *.c - Dateien, aber nicht 'file.c'
403    print (foo|bar).*     # Alle Dateien mit 'foo' und / oder 'bar' am Anfang
404    print *~*.*           # Nur Dateien ohne '.' in Namen
405    chmod 644 *(.^x)      # make all non-executable files publically readable
406    print -l *(.c|.h)     # Nur Dateien mit dem Suffix '.c' und / oder '.h'
407    print **/*(g:users:)  # Alle Dateien/Verzeichnisse der Gruppe >users<
408    echo /proc/*/cwd(:h:t:s/self//) # Analog zu >ps ax | awk '{print $1}'<"
409   }
410
411 # find out which libs define a symbol
412 # usage example: 'lcheck strcpy'
413   lcheck() {
414      if [ -n "$1" ] ; then
415         nm -go /usr/lib/lib*.a 2>/dev/null | grep ":[[:xdigit:]]\{8\} . .*$1"
416       else
417         echo "Usage: lcheck <function>" >&2
418      fi
419   }
420
421 # clean up directory
422   purge() {
423         FILES=(*~(N) .*~(N) \#*\#(N) *.o(N) a.out(N) *.core(N) *.cmo(N) *.cmi(N) .*.swp(N))
424         NBFILES=${#FILES}
425         if [[ $NBFILES > 0 ]]; then
426                 print $FILES
427                 local ans
428                 echo -n "Remove these files? [y/n] "
429                 read -q ans
430                 if [[ $ans == "y" ]]
431                 then
432                         rm ${FILES}
433                         echo ">> $PWD purged, $NBFILES files removed"
434                 else
435                         echo "Ok. .. than not.."
436                 fi
437         fi
438    }
439
440 # Translate DE<=>EN
441 # 'translate' looks up fot a word in a file with language-to-language
442 # translations (field separator should be " : "). A typical wordlist looks
443 # like at follows:
444 #  | english-word : german-transmission
445 # It's also only possible to translate english to german but not reciprocal.
446 # Use the following oneliner to turn back the sort order:
447 #  $ awk -F ':' '{ print $2" : "$1" "$3 }' \
448 #    /usr/local/lib/words/en-de.ISO-8859-1.vok > ~/.translate/de-en.ISO-8859-1.vok
449   trans() {
450         case "$1" in
451                 -[dD]*) translate -l de-en $2
452                 ;;
453                 -[eE]*) translate -l en-de $2
454                 ;;
455                 *)
456                 echo "Usage: $0 { -D | -E }"
457                 echo "         -D == German to English"
458                 echo "         -E == English to German"
459         esac
460   }
461
462 # Some quick Perl-hacks aka /useful/ oneliner
463 #  bew() { perl -le 'print unpack "B*","'$1'"' }
464 #  web() { perl -le 'print pack "B*","'$1'"' }
465 #  hew() { perl -le 'print unpack "H*","'$1'"' }
466 #  weh() { perl -le 'print pack "H*","'$1'"' }
467 #  pversion()    { perl -M$1 -le "print $1->VERSION" } # i. e."pversion LWP -> 5.79"
468 #  getlinks ()   { perl -ne 'while ( m/"((www|ftp|http):\/\/.*?)"/gc ) { print $1, "\n"; }' $* }
469 #  gethrefs ()   { perl -ne 'while ( m/href="([^"]*)"/gc ) { print $1, "\n"; }' $* }
470 #  getanames ()  { perl -ne 'while ( m/a name="([^"]*)"/gc ) { print $1, "\n"; }' $* }
471 #  getforms ()   { perl -ne 'while ( m:(\</?(input|form|select|option).*?\>):gic ) { print $1, "\n"; }' $* }
472 #  getstrings () { perl -ne 'while ( m/"(.*?)"/gc ) { print $1, "\n"; }' $*}
473 #  getanchors () { perl -ne 'while ( m/«([^«»\n]+)»/gc ) { print $1, "\n"; }' $* }
474 #  showINC ()    { perl -e 'for (@INC) { printf "%d %s\n", $i++, $_ }' }
475 #  vimpm ()      { vim `perldoc -l $1 | sed -e 's/pod$/pm/'` }
476 #  vimhelp ()    { vim -c "help $1" -c on -c "au! VimEnter *" }
477
478 # plap foo -- list all occurrences of program in the current PATH
479   plap() {
480         if [[ $# = 0 ]]
481         then
482                 echo "Usage:    $0 program"
483                 echo "Example:  $0 zsh"
484                 echo "Lists all occurrences of program in the current PATH."
485         else
486                 ls -l ${^path}/*$1*(*N)
487         fi
488   }
489
490 # Found in the mailinglistarchive from Zsh (IIRC ~1996)
491   selhist() {
492         emulate -L zsh
493         local TAB=$'\t';
494         (( $# < 1 )) && {
495                 echo "Usage: $0 command"
496                 return 1
497         };
498         cmd=(${(f)"$(grep -w $1 $HISTFILE | sort | uniq | pr -tn)"})
499         print -l $cmd | less -F
500         echo -n "enter number of desired command [1 - $(( ${#cmd[@]} - 1 ))]: "
501         local answer
502         read answer
503         print -z "${cmd[$answer]#*$TAB}"
504   }
505
506 # mkdir && cd
507   mcd() { mkdir -p "$@"; cd "$@" }  # mkdir && cd
508
509 # cd && ls
510   cl() { cd $1 && ls -a }
511
512 # Use vim to convert plaintext to HTML
513   2html() { vim -u NONE -n -c ':syntax on' -c ':so $VIMRUNTIME/syntax/2html.vim' -c ':wqa' $1 > /dev/null 2> /dev/null }
514
515 # Usage: simple-extract <file>
516 # Description: extracts archived files (maybe)
517   simple-extract () {
518         if [[ -f $1 ]]
519         then
520                 case $1 in
521                         *.tar.bz2)  bzip2 -v -d $1      ;;
522                         *.tar.gz)   tar -xvzf $1        ;;
523                         *.rar)      unrar $1            ;;
524                         *.deb)      ar -x $1            ;;
525                         *.bz2)      bzip2 -d $1         ;;
526                         *.lzh)      lha x $1            ;;
527                         *.gz)       gunzip -d $1        ;;
528                         *.tar)      tar -xvf $1         ;;
529                         *.tgz)      gunzip -d $1        ;;
530                         *.tbz2)     tar -jxvf $1        ;;
531                         *.zip)      unzip $1            ;;
532                         *.Z)        uncompress $1       ;;
533                         *)          echo "'$1' Error. Please go away" ;;
534                 esac
535         else
536                 echo "'$1' is not a valid file"
537         fi
538   }
539
540 # Usage: smartcompress <file> (<type>)
541 # Description: compresses files or a directory.  Defaults to tar.gz
542   smartcompress() {
543         if [ $2 ]; then
544                 case $2 in
545                         tgz | tar.gz)   tar -zcvf$1.$2 $1 ;;
546                         tbz2 | tar.bz2) tar -jcvf$1.$2 $1 ;;
547                         tar.Z)          tar -Zcvf$1.$2 $1 ;;
548                         tar)            tar -cvf$1.$2  $1 ;;
549                         gz | gzip)      gzip           $1 ;;
550                         bz2 | bzip2)    bzip2          $1 ;;
551                         *)
552                         echo "Error: $2 is not a valid compression type"
553                         ;;
554                 esac
555         else
556                 smartcompress $1 tar.gz
557         fi
558   }
559
560 # Usage: show-archive <archive>
561 # Description: view archive without unpack
562   show-archive() {
563         if [[ -f $1 ]]
564         then
565                 case $1 in
566                         *.tar.gz)      gunzip -c $1 | tar -tf - -- ;;
567                         *.tar)         tar -tf $1 ;;
568                         *.tgz)         tar -ztf $1 ;;
569                         *.zip)         unzip -l $1 ;;
570                         *.bz2)         bzless $1 ;;
571                         *)             echo "'$1' Error. Please go away" ;;
572                 esac
573         else
574                 echo "'$1' is not a valid archive"
575         fi
576   }
577
578 # follow symlinks
579   folsym() {
580     if [[ -e $1 || -h $1 ]]; then
581         file=$1
582     else
583         file=`which $1`
584     fi
585     if [[ -e $file || -L $file ]]; then
586         if [[ -L $file ]]; then
587             echo `ls -ld $file | perl -ane 'print $F[7]'` '->'
588             folsym `perl -le '$file = $ARGV[0];
589                               $dest = readlink $file;
590                               if ($dest !~ m{^/}) {
591                                   $file =~ s{(/?)[^/]*$}{$1$dest};
592                               } else {
593                                   $file = $dest;
594                               }
595                               $file =~ s{/{2,}}{/}g;
596                               while ($file =~ s{[^/]+/\.\./}{}) {
597                                   ;
598                               }
599                               $file =~ s{^(/\.\.)+}{};
600                               print $file' $file`
601         else
602             ls -d $file
603         fi
604     else
605         echo $file
606     fi
607   }
608
609 # Use 'view' to read manpages, if u want colors, regex - search, ...
610 # like vi(m).
611 # It's shameless stolen from <http://www.vim.org/tips/tip.php?tip_id=167>
612   vman() { man $* | col -b | view -c 'set ft=man nomod nolist' - }
613
614 # search for various types or README file in dir and display them in $PAGER
615 # function readme() { $PAGER -- (#ia3)readme* }
616   readme() {
617         local files
618         files=(./(#i)*(read*me|lue*m(in|)ut)*(ND))
619         if (($#files))
620         then $PAGER $files
621         else
622                 print 'No README files.'
623         fi
624   }
625
626 # find all suid files in $PATH
627 # suidfind() { ls -latg $path | grep '^...s' }
628   suidfind() { ls -latg $path/*(sN) }
629
630 # See above but this is /better/ ... anywise ..
631   findsuid() {
632     print 'Output will be written to ~/suid_* ...'
633     $SUDO find / -type f \( -perm -4000 -o -perm -2000 \) -ls > ~/suid_suidfiles.`date "+%Y-%m-%d"`.out 2>&1
634     $SUDO find / -type d \( -perm -4000 -o -perm -2000 \) -ls > ~/suid_suiddirs.`date "+%Y-%m-%d"`.out 2>&1
635     $SUDO find / -type f \( -perm -2 -o -perm -20 \) -ls > ~/suid_writefiles.`date "+%Y-%m-%d"`.out 2>&1
636     $SUDO find / -type d \( -perm -2 -o -perm -20 \) -ls > ~/suid_writedirs.`date "+%Y-%m-%d"`.out 2>&1
637     print 'Finished'
638   }
639
640 # Reload functions.
641   refunc() {
642         for func in $argv
643         do
644                 unfunction $func
645                 autoload $func
646         done
647   }
648
649 # a small check to see which DIR is located on which server/partition.
650 # stolen and modified from Sven's zshrc.forall
651   dirspace() {
652         for dir in $path;
653         do
654                 (cd $dir; echo "-<$dir>"; du -shx .; echo);
655         done
656   }
657
658 # % slow_print `cat /etc/passwd`
659   slow_print() {
660         for argument in "${@}"
661         do
662                 for ((i = 1; i <= ${#1} ;i++)) {
663                         print -n "${argument[i]}"
664                         sleep 0.08
665                 }
666                 print -n " "
667         done
668         print ""
669   }
670
671 # display system state
672   status() {
673         print ""
674         print "Date..: "$(date "+%Y-%m-%d %H:%M:%S")""
675         print "Shell.: Zsh $ZSH_VERSION (PID = $$, $SHLVL nests)"
676         print "Term..: $TTY ($TERM), $BAUD bauds, $COLUMNS x $LINES cars"
677         print "Login.: $LOGNAME (UID = $EUID) on $HOST"
678         print "System: $(cat /etc/[A-Za-z]*[_-][rv]e[lr]*)"
679         print "Uptime:$(uptime)"
680         print ""
681   }
682
683 # Rip an audio CD
684   audiorip() {
685         mkdir -p ~/ripps
686         cd ~/ripps
687         cdrdao read-cd --device $DEVICE --driver generic-mmc audiocd.toc
688         cdrdao read-cddb --device $DEVICE --driver generic-mmc audiocd.toc
689         echo " * Would you like to burn the cd now? (yes/no)"
690         read input
691         if
692                 [ "$input" = "yes" ]; then
693                 echo " ! Burning Audio CD"
694                 audioburn
695                 echo " * done."
696         else
697                 echo " ! Invalid response."
698         fi
699   }
700
701 # and burn it
702   audioburn() {
703         cd ~/ripps
704         cdrdao write --device $DEVICE --driver generic-mmc audiocd.toc
705         echo " * Should I remove the temporary files? (yes/no)"
706         read input
707         if [ "$input" = "yes" ]; then
708                 echo " ! Removing Temporary Files."
709                 cd ~
710                 rm -rf ~/ripps
711                 echo " * done."
712         else
713                 echo " ! Invalid response."
714         fi
715   }
716
717 # Make an audio CD from all mp3 files
718   mkaudiocd() {
719         cd ~/ripps
720         for i in *.[Mm][Pp]3; do mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`; done
721         for i in *.mp3; do mv "$i" `echo $i | tr ' ' '_'`; done
722         for i in *.mp3; do mpg123 -w `basename $i .mp3`.wav $i; done
723         normalize -m *.wav
724         for i in *.wav; do sox $i.wav -r 44100 $i.wav resample; done
725   }
726
727 # Create an ISO image. You are prompted for volume name, filename and directory
728   mkiso() {
729         echo " * Volume name "
730         read volume
731         echo " * ISO Name (ie. tmp.iso)"
732         read iso
733         echo " * Directory or File"
734         read files
735         mkisofs -o ~/$iso -A $volume -allow-multidot -J -R -iso-level 3 -V $volume -R $files
736   }
737
738 # simple thumbnails generator
739   genthumbs () {
740     rm -rf thumb-* index.html
741     echo "
742 <html>
743   <head>
744     <title>Images</title>
745   </head>
746   <body>" > index.html
747     for f in *.(gif|jpeg|jpg|png)
748     do
749         convert -size 100x200 "$f" -resize 100x200 thumb-"$f"
750         echo "    <a href=\"$f\"><img src=\"thumb-$f\"></a>" >> index.html
751     done
752     echo "
753   </body>
754 </html>" >> index.html
755   }
756
757 # unset all limits (see zshbuiltins(1) /ulimit for details)
758   allulimit() {
759     ulimit -c unlimited
760     ulimit -d unlimited
761     ulimit -f unlimited
762     ulimit -l unlimited
763     ulimit -n unlimited
764     ulimit -s unlimited
765     ulimit -t unlimited
766   }
767
768 # ogg2mp3 with bitrate of 192
769   ogg2mp3_192() {
770     oggdec -o - ${1} | lame -b 192 - ${1:r}.mp3
771   }
772
773 # RFC 2396 URL encoding in Z-Shell
774   urlencode() {
775    setopt localoptions extendedglob
776    input=( ${(s::)1} )
777    print ${(j::)input/(#b)([^A-Za-z0-9_.!~*\'\(\)-])/%$(([##16]#match))}
778   }
779
780 # get x-lite voip software
781   getxlite() {
782     setopt local_options
783     setopt errreturn
784     [ -d ~/tmp ] || mkdir ~/tmp
785     cd ~/tmp
786     echo "Downloading http://www.counterpath.com/download/X-Lite_Install.tar.gz and storing it in ~/tmp:"
787     if wget http://www.counterpath.com/download/X-Lite_Install.tar.gz ; then
788        unp X-Lite_Install.tar.gz && echo done || echo failed
789     else
790        echo "Error while downloading." ; return 1
791     fi
792     if [ -x xten-xlite/xtensoftphone ] ; then
793        echo "Execute xten-xlite/xtensoftphone to start xlite."
794     fi
795    }
796
797 # get skype
798   getskype() {
799     setopt local_options
800     setopt errreturn
801     echo "Downloading debian package of skype."
802     echo "Notice: If you want to use a more recent skype version run 'getskypebeta'."
803     wget http://www.skype.com/go/getskype-linux-deb
804     $SUDO dpkg -i skype_debian-*.deb && echo "skype installed."
805   }
806
807 # get beta-version of skype
808   getskypebeta() {
809     setopt local_options
810     setopt errreturn
811     echo "Downloading debian package of skype (beta version)."
812     wget http://www.skype.com/go/getskype-linux-beta-deb
813     $SUDO dpkg -i skype-beta*.deb && echo "skype installed."
814   }
815
816 # get gzimo (VoIP software)
817   getgizmo() {
818     setopt local_options
819     setopt errreturn
820     echo "gconf2-common and libgconf2-4 have to be available. Installing therefor."
821     $SUDO apt-get update
822     $SUDO apt-get install gconf2-common libgconf2-4
823     wget $(lynx --dump http://www.gizmoproject.com/download-linux.html | awk '/\.deb/ {print $2" "}' | tr -d '\n')
824     $SUDO dpkg -i libsipphoneapi*.deb bonjour_*.deb gizmo-*.deb && echo "gizmo installed."
825   }
826
827 # get AIR - Automated Image and Restore
828   getair() {
829     setopt local_options
830     setopt errreturn
831     [ -w . ] || { echo 'Error: you do not have write permissions in this directory. Exiting.' ; return 1 }
832     local VER='1.2.8'
833     wget http://puzzle.dl.sourceforge.net/sourceforge/air-imager/air-$VER.tar.gz
834     tar zxf air-$VER.tar.gz
835     cd air-$VER
836     INTERACTIVE=no $SUDO ./install-air-1.2.8
837     [ -x /usr/local/bin/air ] && [ -n "$DISPLAY" ] && $SUDO air
838   }
839
840 # get specific git commitdiff
841   git-get-diff() {
842     if [ -z $GITTREE ] ; then
843       GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
844     fi
845     if ! [ -z $1 ] ; then
846      ${=BROWSER} "http://kernel.org/git/?p=$GITTREE;a=commitdiff;h=$1"
847     else
848       echo "Usage: git-get-diff <commit>"
849     fi
850   }
851
852 # get specific git commit
853   git-get-commit() {
854     if [ -z $GITTREE ] ; then
855       GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
856     fi
857     if ! [ -z $1 ] ; then
858      ${=BROWSER} "http://kernel.org/git/?p=$GITTREE;a=commit;h=$1"
859     else
860       echo "Usage: git-get-commit <commit>"
861     fi
862   }
863
864 # get specific git diff
865   git-get-plaindiff() {
866     if [ -z $GITTREE ] ; then
867       GITTREE='linux/kernel/git/torvalds/linux-2.6.git'
868     fi
869     if ! [ -z $1 ] ; then
870       wget "http://kernel.org/git/?p=$GITTREE;a=commitdiff_plain;h=$1" -O $1.diff
871     else
872       echo 'Usage: git-get-plaindiff '
873     fi
874   }
875
876 # log 'make install' output
877 # http://strcat.de/blog/index.php?/archives/335-Software-sauber-deinstallieren...html
878   mmake() {
879     [[ ! -d ~/.errorlogs ]] && mkdir ~/.errorlogs
880     =make -n install > ~/.errorlogs/${PWD##*/}-makelog
881   }
882
883 # indent source code
884   smart-indent() {
885     indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs $*
886   }
887
888 # highlight important stuff in diff output, usage example: hg diff | hidiff
889   [ -x =histring ] && \
890   alias hidiff="histring -fE '^Comparing files .*|^diff .*' | histring -c yellow -fE '^\-.*' | histring -c green -fE '^\+.*'"
891
892 # rename pictures based on information found in exif headers
893   exirename() {
894     if [ $# -lt 1 ] ; then
895        echo 'Usage: jpgrename $FILES' >& 2
896        return 1
897     else
898        echo -n 'Checking for jhead with version newer than 1.9: '
899        jhead_version=`jhead -h | grep 'used by most Digital Cameras.  v.*' | awk '{print $6}' | tr -d v`
900        if [[ $jhead_version > '1.9' ]]; then
901           echo 'success - now running jhead.'
902           jhead -n%Y-%m-%d_%Hh%M_%f $*
903        else
904           echo 'failed - exiting.'
905        fi
906     fi
907   }
908 # }}}
909
910 # mercurial related stuff {{{
911   if type -p hg >/dev/null 2>&1 ; then
912   # gnu like diff for mercurial
913   # http://www.selenic.com/mercurial/wiki/index.cgi/TipsAndTricks
914     hgdi() {
915       for i in `hg status -marn "$@"` ; diff -ubwd <(hg cat "$i") "$i"
916     }
917
918   # build debian package
919     alias hbp='hg-buildpackage'
920
921   # diffstat for specific version of a mercurial repository
922   #   hgstat      => display diffstat between last revision and tip
923   #   hgstat 1234 => display diffstat between revision 1234 and tip
924     hgstat() {
925       [ -n "$1" ] && hg diff -r $1 -r tip | diffstat || hg export tip | diffstat
926     }
927
928   # get current mercurial tip via hg itself
929     gethgclone() {
930       setopt local_options
931       setopt errreturn
932       if [ -f mercurial-tree/.hg ] ; then
933         cd mercurial-tree
934         echo "Running hg pull for retreiving latest version..."
935         hg pull
936         echo "Finished update. Building mercurial"
937         make local
938         echo "Setting \$PATH to $PWD:\$PATH..."
939         export PATH="$PWD:$PATH"
940       else
941         echo "Downloading mercurial via hg"
942         hg clone http://selenic.com/repo/hg mercurial-tree
943         cd mercurial-tree
944         echo "Building mercurial"
945         make local
946         echo "Setting \$PATH to $PWD:\$PATH..."
947         export PATH="$PWD:$PATH"
948         echo "make sure you set it permanent via ~/.zshrc if you plan to use it permanently."
949         # echo "Setting \$PYTHONPATH to PYTHONPATH=\${HOME}/lib/python,"
950         # export PYTHONPATH=${HOME}/lib/python
951       fi
952     }
953   
954   fi # end of check whether we have the 'hg'-executable
955
956   # get current mercurial snapshot
957     gethgsnap() {
958       setopt local_options
959       setopt errreturn
960       if [ -f mercurial-snapshot.tar.gz ] ; then
961          echo "mercurial-snapshot.tar.gz exists already, skipping download."
962       else
963         echo "Downloading mercurial snapshot"
964         wget http://www.selenic.com/mercurial/mercurial-snapshot.tar.gz
965       fi
966       echo "Unpacking mercurial-snapshot.tar.gz"
967       tar zxf mercurial-snapshot.tar.gz
968       cd mercurial-snapshot/
969       echo "Installing required build-dependencies"
970       $SUDO apt-get update
971       $SUDO apt-get install python2.4-dev
972       echo "Building mercurial"
973       make local
974       echo "Setting \$PATH to $PWD:\$PATH..."
975       export PATH="$PWD:$PATH"
976       echo "make sure you set it permanent via ~/.zshrc if you plan to use it permanently."
977     }
978 # }}}
979
980 # some useful commands often hard to remember - let's grep for them {{{
981
982 # Work around ion/xterm resize bug.
983 #if [ "$SHLVL" = 1 ]; then
984 #       if [ -x `which resize 2>/dev/null` ]; then
985 #               eval `resize </dev/null`
986 #       fi
987 #fi
988
989 # enable jackd:
990 #  /usr/bin/jackd -dalsa -dhw:0 -r48000 -p1024 -n2
991 # now play audio file:
992 #  alsaplayer -o jack foobar.mp3
993
994 # send files via netcat
995 # on sending side:
996 #  send() {j=$*; tar cpz ${j/%${!#}/}|nc -w 1 ${!#} 51330;}
997 #  send dir* $HOST
998 #  alias receive='nc -vlp 51330 | tar xzvp'
999
1000 # debian stuff:
1001 # dh_make -e foo@localhost -f $1
1002 # dpkg-buildpackage -rfakeroot
1003 # lintian *.deb
1004 # dpkg-scanpackages ./ /dev/null | gzip > Packages.gz
1005 # dpkg-scansources . | gzip > Sources.gz
1006 # grep-dctrl --field Maintainer $* /var/lib/apt/lists/*
1007
1008 # other stuff:
1009 # convert -geometry 200x200 -interlace LINE -verbose
1010 # ldapsearch -x -b "OU=Bedienstete,O=tug" -h ldap.tugraz.at sn=$1
1011 # ps -ao user,pcpu,start,command
1012 # gpg --keyserver blackhole.pca.dfn.de --recv-keys
1013 # xterm -bg black -fg yellow -fn -misc-fixed-medium-r-normal--14-140-75-75-c-90-iso8859-15 -ah
1014 # nc -vz $1 1-1024   # portscan via netcat
1015 # wget --mirror --no-parent --convert-links
1016 # pal -d `date +%d`
1017 # autoload -U tetris; zle -N tetris; bindkey '...' ; echo "press ... for playing tennis"
1018 #
1019 # modify console cursor
1020 # see http://www.tldp.org/HOWTO/Framebuffer-HOWTO-5.html
1021 # print $'\e[?96;0;64c'
1022 # }}}
1023
1024 # finally source a local zshrc and grmlsmall-specific configuration {{{
1025
1026 # The following file is used to remove zsh-config-items that do not work
1027 # in grml-small by default.
1028 # If you do not want these adjustments (for whatever reason),
1029 # there are three ways to accomplish that:
1030 #  a) at the beginning of this file (variables section), set
1031 #     $GRMLSMALL_SPECIFIC to 0 or comment out the variable definition.
1032 #  b) remove/rename .zshrc.grmlsmall
1033 #  c) comment out the following line
1034   (( GRMLSMALL_SPECIFIC > 0 )) && isgrmlsmall && source ~/.zshrc.grmlsmall
1035
1036 # this allows us to stay in sync with /etc/skel/.zshrc
1037 # through 'ln -s /etc/skel/.zshrc ~/.zshrc' and put own
1038 # modifications in ~/.zshrc.local
1039   if [ -r ~/.zshrc.local ] ; then
1040      . ~/.zshrc.local
1041   fi
1042 # }}}
1043
1044 ## END OF FILE #################################################################
1045 # vim:foldmethod=marker