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