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