# {{{ setup # load .zshrc.pre to give the user the chance to overwrite the defaults [[ -r ${HOME}/.zshrc.pre ]] && source ${HOME}/.zshrc.pre # check for user, if not running as root set $SUDO to sudo (( EUID != 0 )) && SUDO='sudo' || SUDO='' # autoload wrapper - use this one instead of autoload directly # We need to define this function as early as this, because autoloading # 'is-at-least()' needs it. function zrcautoload() { emulate -L zsh setopt extended_glob local fdir ffile local -i ffound ffile=$1 (( found = 0 )) for fdir in ${fpath} ; do [[ -e ${fdir}/${ffile} ]] && (( ffound = 1 )) done (( ffound == 0 )) && return 1 if [[ $ZSH_VERSION == 3.1.<6-> || $ZSH_VERSION == <4->* ]] ; then autoload -U ${ffile} || return 1 else autoload ${ffile} || return 1 fi return 0 } # Load is-at-least() for more precise version checks # Note that this test will *always* fail, if the is-at-least # function could not be marked for autoloading. zrcautoload is-at-least || is-at-least() { return 1 } # }}} # {{{ set some important options (as early as possible) # Please update these tags, if you change the umask settings below. setopt append_history # append history list to the history file (important for multiple parallel zsh sessions!) setopt SHARE_HISTORY # import new commands from the history file also in other zsh-session setopt extended_history # save each command's beginning timestamp and the duration to the history file setopt histignorealldups # If a new command line being added to the history # list duplicates an older one, the older command is removed from the list setopt histignorespace # remove command lines from the history list when # the first character on the line is a space setopt auto_cd # if a command is issued that can't be executed as a normal command, # and the command is the name of a directory, perform the cd command to that directory setopt extended_glob # in order to use #, ~ and ^ for filename generation # grep word *~(*.gz|*.bz|*.bz2|*.zip|*.Z) -> # -> searches for word not in compressed files # don't forget to quote '^', '~' and '#'! setopt longlistjobs # display PID when suspending processes as well setopt notify # report the status of backgrounds jobs immediately setopt hash_list_all # Whenever a command completion is attempted, make sure \ # the entire command path is hashed first. setopt completeinword # not just at the end setopt nohup # and don't kill them, either setopt auto_pushd # make cd push the old directory onto the directory stack. setopt nonomatch # try to avoid the 'zsh: no matches found...' setopt nobeep # avoid "beep"ing setopt pushd_ignore_dups # don't push the same dir twice. setopt noglobdots # * shouldn't match dotfiles. ever. setopt noshwordsplit # use zsh style word splitting # }}} # utility functions {{{ # this function checks if a command exists and returns either true # or false. This avoids using 'which' and 'whence', which will # avoid problems with aliases for which on certain weird systems. :-) # Usage: check_com [-c|-g] word # -c only checks for external commands # -g does the usual tests and also checks for global aliases check_com() { emulate -L zsh local -i comonly gatoo if [[ $1 == '-c' ]] ; then (( comonly = 1 )) shift elif [[ $1 == '-g' ]] ; then (( gatoo = 1 )) else (( comonly = 0 )) (( gatoo = 0 )) fi if (( ${#argv} != 1 )) ; then printf 'usage: check_com [-c] \n' >&2 return 1 fi if (( comonly > 0 )) ; then [[ -n ${commands[$1]} ]] && return 0 return 1 fi if [[ -n ${commands[$1]} ]] \ || [[ -n ${functions[$1]} ]] \ || [[ -n ${aliases[$1]} ]] \ || [[ -n ${reswords[(r)$1]} ]] ; then return 0 fi if (( gatoo > 0 )) && [[ -n ${galiases[$1]} ]] ; then return 0 fi return 1 } # creates an alias and precedes the command with # sudo if $EUID is not zero. salias() { emulate -L zsh local only=0 ; local multi=0 while [[ $1 == -* ]] ; do case $1 in (-o) only=1 ;; (-a) multi=1 ;; (--) shift ; break ;; (-h) printf 'usage: salias [-h|-o|-a] \n' printf ' -h shows this help text.\n' printf ' -a replace '\'' ; '\'' sequences with '\'' ; sudo '\''.\n' printf ' be careful using this option.\n' printf ' -o only sets an alias if a preceding sudo would be needed.\n' return 0 ;; (*) printf "unkown option: '%s'\n" "$1" ; return 1 ;; esac shift done if (( ${#argv} > 1 )) ; then printf 'Too many arguments %s\n' "${#argv}" return 1 fi key="${1%%\=*}" ; val="${1#*\=}" if (( EUID == 0 )) && (( only == 0 )); then alias -- "${key}=${val}" elif (( EUID > 0 )) ; then (( multi > 0 )) && val="${val// ; / ; sudo }" alias -- "${key}=sudo ${val}" fi return 0 } # Check if we can read given files and source those we can. xsource() { emulate -L zsh if (( ${#argv} < 1 )) ; then printf 'usage: xsource FILE(s)...\n' >&2 return 1 fi while (( ${#argv} > 0 )) ; do [[ -r $1 ]] && source $1 shift done return 0 } # Check if we can read a given file and 'cat(1)' it. xcat() { emulate -L zsh if (( ${#argv} != 1 )) ; then printf 'usage: xcat FILE\n' >&2 return 1 fi [[ -r $1 ]] && cat $1 return 0 } # Remove these functions again, they are of use only in these # setup files. This should be called at the end of .zshrc. xunfunction() { emulate -L zsh local -a funcs funcs=(salias xcat xsource xunfunction zrcautoload) for func in $funcs ; do [[ -n ${functions[$func]} ]] \ && unfunction $func done return 0 } # this allows us to stay in sync with grml's zshrc and put own # modifications in ~/.zshrc.local zrclocal() { xsource "/etc/zsh/zshrc.local" xsource "${HOME}/.zshrc.local" return 0 } #}}} # {{{ set some variables export SHELL='/bin/zsh' # color setup for ls: eval $(dircolors -b) # support colors in less export LESS_TERMCAP_mb=$'\E[01;31m' export LESS_TERMCAP_md=$'\E[01;31m' export LESS_TERMCAP_me=$'\E[0m' export LESS_TERMCAP_se=$'\E[0m' export LESS_TERMCAP_so=$'\E[01;44;33m' export LESS_TERMCAP_ue=$'\E[0m' export LESS_TERMCAP_us=$'\E[01;32m' typeset -U path cdpath fpath manpath # }}} # {{{ keybindings if [[ "$TERM" != emacs ]] ; then [[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char [[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line [[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line [[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]" vi-delete-char [[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]" vi-beginning-of-line [[ -z "$terminfo[kend]" ]] || bindkey -M vicmd "$terminfo[kend]" vi-end-of-line [[ -z "$terminfo[cuu1]" ]] || bindkey -M viins "$terminfo[cuu1]" vi-up-line-or-history [[ -z "$terminfo[cuf1]" ]] || bindkey -M viins "$terminfo[cuf1]" vi-forward-char [[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]" vi-up-line-or-history [[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]" vi-down-line-or-history [[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]" vi-forward-char [[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]" vi-backward-char # ncurses stuff: [[ "$terminfo[kcuu1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcuu1]/O/[}" vi-up-line-or-history [[ "$terminfo[kcud1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcud1]/O/[}" vi-down-line-or-history [[ "$terminfo[kcuf1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcuf1]/O/[}" vi-forward-char [[ "$terminfo[kcub1]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kcub1]/O/[}" vi-backward-char [[ "$terminfo[khome]" == $'\eO'* ]] && bindkey -M viins "${terminfo[khome]/O/[}" beginning-of-line [[ "$terminfo[kend]" == $'\eO'* ]] && bindkey -M viins "${terminfo[kend]/O/[}" end-of-line [[ "$terminfo[khome]" == $'\eO'* ]] && bindkey -M emacs "${terminfo[khome]/O/[}" beginning-of-line [[ "$terminfo[kend]" == $'\eO'* ]] && bindkey -M emacs "${terminfo[kend]/O/[}" end-of-line fi ## keybindings (run 'bindkeys' for details, more details via man zshzle) # use emacs style per default: bindkey -e # use vi style: # bindkey -v #if [[ "$TERM" == screen ]] ; then bindkey '\e[1~' beginning-of-line # home bindkey '\e[4~' end-of-line # end bindkey '\e[A' up-line-or-search # cursor up bindkey '\e[B' down-line-or-search # - bindkey '^xp' history-beginning-search-backward bindkey '^xP' history-beginning-search-forward # bindkey -s '^L' "|less\n" # ctrl-L pipes to less # bindkey -s '^B' " &\n" # ctrl-B runs it in the background # if terminal type is set to 'rxvt': bindkey '\e[7~' beginning-of-line # home bindkey '\e[8~' end-of-line # end #fi # insert unicode character # usage example: 'ctrl-x i' 00A7 'ctrl-x i' will give you an � # See for example http://unicode.org/charts/ for unicode characters code zrcautoload insert-unicode-char zle -N insert-unicode-char #k# Insert Unicode character bindkey '^Xi' insert-unicode-char ## toggle the ,. abbreviation feature on/off # NOABBREVIATION: default abbreviation-state # 0 - enabled (default) # 1 - disabled NOABBREVIATION=${NOABBREVIATION:-0} # add a command line to the shells history without executing it commit-to-history() { print -s ${(z)BUFFER} zle send-break } zle -N commit-to-history bindkey "^x^h" commit-to-history # only slash should be considered as a word separator: slash-backward-kill-word() { local WORDCHARS="${WORDCHARS:s@/@}" # zle backward-word zle backward-kill-word } zle -N slash-backward-kill-word #k# Kill everything in a word up to its last \kbd{/} bindkey '\ev' slash-backward-kill-word # }}} # a generic accept-line wrapper {{{ # This widget can prevent unwanted autocorrections from command-name # to _command-name, rehash automatically on enter and call any number # of builtin and user-defined widgets in different contexts. # # For a broader description, see: # # # The code is imported from the file 'zsh/functions/accept-line' from # , which # distributed under the same terms as zsh itself. # A newly added command will may not be found or will cause false # correction attempts, if you got auto-correction set. By setting the # following style, we force accept-line() to rehash, if it cannot # find the first word on the command line in the $command[] hash. zstyle ':acceptline:*' rehash true function Accept-Line() { setopt localoptions noksharrays local -a subs local -xi aldone local sub zstyle -a ":acceptline:${alcontext}" actions subs (( ${#subs} < 1 )) && return 0 (( aldone = 0 )) for sub in ${subs} ; do [[ ${sub} == 'accept-line' ]] && sub='.accept-line' zle ${sub} (( aldone > 0 )) && break done } function Accept-Line-getdefault() { local default_action zstyle -s ":acceptline:${alcontext}" default_action default_action case ${default_action} in ((accept-line|)) printf ".accept-line" ;; (*) printf ${default_action} ;; esac } function accept-line() { setopt localoptions noksharrays local -a cmdline local -x alcontext local buf com fname format msg default_action alcontext='default' buf="${BUFFER}" cmdline=(${(z)BUFFER}) com="${cmdline[1]}" fname="_${com}" zstyle -t ":acceptline:${alcontext}" rehash \ && [[ -z ${commands[$com]} ]] \ && rehash if [[ -n ${reswords[(r)$com]} ]] \ || [[ -n ${aliases[$com]} ]] \ || [[ -n ${functions[$com]} ]] \ || [[ -n ${builtins[$com]} ]] \ || [[ -n ${commands[$com]} ]] ; then # there is something sensible to execute, just do it. alcontext='normal' zle Accept-Line default_action=$(Accept-Line-getdefault) zstyle -T ":acceptline:${alcontext}" call_default \ && zle ${default_action} return fi if [[ -o correct ]] \ || [[ -o correctall ]] \ && [[ -n ${functions[$fname]} ]] ; then # nothing there to execute but there is a function called # _command_name; a completion widget. Makes no sense to # call it on the commandline, but the correct{,all} options # will ask for it nevertheless, so warn the user. if [[ ${LASTWIDGET} == 'accept-line' ]] ; then # Okay, we warned the user before, he called us again, # so have it his way. alcontext='force' zle Accept-Line default_action=$(Accept-Line-getdefault) zstyle -T ":acceptline:${alcontext}" call_default \ && zle ${default_action} return fi # prepare warning message for the user, configurable via zstyle. zstyle -s ":acceptline:${alcontext}" compwarnfmt msg if [[ -z ${msg} ]] ; then msg="%c will not execute and completion %f exists." fi zformat -f msg "${msg}" "c:${com}" "f:${fname}" zle -M -- "${msg}" return elif [[ -n ${buf//[$' \t\n']##/} ]] ; then # If we are here, the commandline contains something that is not # executable, which is neither subject to _command_name correction # and is not empty. might be a variable assignment alcontext='misc' zle Accept-Line default_action=$(Accept-Line-getdefault) zstyle -T ":acceptline:${alcontext}" call_default \ && zle ${default_action} return fi # If we got this far, the commandline only contains whitespace, or is empty. alcontext='empty' zle Accept-Line default_action=$(Accept-Line-getdefault) zstyle -T ":acceptline:${alcontext}" call_default \ && zle ${default_action} } zle -N accept-line zle -N Accept-Line # }}} # power completion - abbreviation expansion {{{ # power completion / abbreviation expansion / buffer expansion # see http://zshwiki.org/home/examples/zleiab for details # less risky than the global aliases but powerful as well # just type the abbreviation key and afterwards ',.' to expand it declare -A abk setopt extendedglob setopt interactivecomments abk=( # key # value (#d additional doc string) #A# start '...' '../..' '....' '../../..' 'BG' '& exit' 'C' '| wc -l' 'G' '|& grep --color=auto ' 'H' '| head' 'Hl' ' --help |& less -r' #d (Display help in pager) 'L' '| less' 'LL' '|& less -r' 'M' '| most' 'N' '&>/dev/null' #d (No Output) 'R' '| tr A-z N-za-m' #d (ROT13) 'SL' '| sort | less' 'S' '| sort -u' 'T' '| tail' 'V' '|& vim -' ) globalias() { emulate -L zsh setopt extendedglob local MATCH if (( NOABBREVIATION > 0 )) ; then LBUFFER="${LBUFFER},." return 0 fi matched_chars='[.-|_a-zA-Z0-9]#' LBUFFER=${LBUFFER%%(#m)[.-|_a-zA-Z0-9]#} LBUFFER+=${abk[$MATCH]:-$MATCH} } zle -N globalias bindkey ",." globalias # }}} # {{{ autoloading zrcautoload zmv # who needs mmv or rename? zrcautoload history-search-end # we don't want to quote/espace URLs on our own... # if autoload -U url-quote-magic ; then # zle -N self-insert url-quote-magic # zstyle ':url-quote-magic:*' url-metas '*?[]^()~#{}=' # else # print 'Notice: no url-quote-magic available :(' # fi alias url-quote='autoload -U url-quote-magic ; zle -N self-insert url-quote-magic' #m# k ESC-h Call \kbd{run-help} for the 1st word on the command line alias run-help >&/dev/null && unalias run-help for rh in run-help{,-git,-svk,-svn}; do zrcautoload $rh done; unset rh # completion system if zrcautoload compinit ; then compinit || print 'Notice: no compinit available :(' else print 'Notice: no compinit available :(' function zstyle { } function compdef { } fi zrcautoload zed # use ZLE editor to edit a file or function for mod in complist deltochar mathfunc ; do zmodload -i zsh/${mod} 2>/dev/null || print "Notice: no ${mod} available :(" done # autoload zsh modules when they are referenced tmpargs=( a stat a zpty ap mapfile ) while (( ${#tmpargs} > 0 )) ; do zmodload -${tmpargs[1]} zsh/${tmpargs[2]} ${tmpargs[2]} shift 2 tmpargs done unset tmpargs if zrcautoload insert-files && zle -N insert-files ; then #k# Insert files bindkey "^Xf" insert-files # C-x-f fi bindkey ' ' magic-space # also do history expansion on space #k# Trigger menu-complete bindkey '\ei' menu-complete # menu completion via esc-i # press esc-e for editing command line in $EDITOR or $VISUAL if zrcautoload edit-command-line && zle -N edit-command-line ; then #k# Edit the current line in \kbd{\$EDITOR} bindkey '\ee' edit-command-line fi if [[ -n ${(k)modules[zsh/complist]} ]] ; then #k# menu selection: pick item but stay in the menu bindkey -M menuselect '\e^M' accept-and-menu-complete # accept a completion and try to complete again by using menu # completion; very useful with completing directories # by using 'undo' one's got a simple file browser bindkey -M menuselect '^o' accept-and-infer-next-history fi # press "ctrl-e d" to insert the actual date in the form yyyy-mm-dd _bkdate() { BUFFER="$BUFFER$(date '+%F')"; CURSOR=$#BUFFER; } zle -N _bkdate #k# Insert a timestamp on the command line (yyyy-mm-dd) bindkey '^Ed' _bkdate # press esc-m for inserting last typed word again (thanks to caphuso!) insert-last-typed-word() { zle insert-last-word -- 0 -1 }; zle -N insert-last-typed-word; #k# Insert last typed word bindkey "\em" insert-last-typed-word #k# Shortcut for \kbd{fg} bindkey -s '^z' "fg\n" # run command line as user root via sudo: sudo-command-line() { [[ -z $BUFFER ]] && zle up-history [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER" } zle -N sudo-command-line #k# Put the current command line into a \kbd{sudo} call bindkey "^Os" sudo-command-line ### jump behind the first word on the cmdline. ### useful to add options. function jump_after_first_word() { local words words=(${(z)BUFFER}) if (( ${#words} <= 1 )) ; then CURSOR=${#BUFFER} else CURSOR=${#${words[1]}} fi } zle -N jump_after_first_word bindkey '^x1' jump_after_first_word # }}} # {{{ history ZSHDIR=$HOME/.zsh #v# HISTFILE=$HOME/.zsh/histfile HISTSIZE=1000000 SAVEHIST=1000000 # useful for setopt append_history # }}} # dirstack handling {{{ DIRSTACKSIZE=${DIRSTACKSIZE:-20} DIRSTACKFILE=${DIRSTACKFILE:-${HOME}/.zdirs} if [[ -f ${DIRSTACKFILE} ]] && [[ ${#dirstack[*]} -eq 0 ]] ; then dirstack=( ${(f)"$(< $DIRSTACKFILE)"} ) # "cd -" won't work after login by just setting $OLDPWD, so [[ -d $dirstack[0] ]] && cd $dirstack[0] && cd $OLDPWD fi chpwd() { local -ax my_stack my_stack=( ${PWD} ${dirstack} ) builtin print -l ${(u)my_stack} >! ${DIRSTACKFILE} } # }}} # directory based profiles {{{ CHPWD_PROFILE='default' function chpwd_profiles() { # Say you want certain settings to be active in certain directories. # This is what you want. # # zstyle ':chpwd:profiles:/usr/src/grml(|/|/*)' profile grml # zstyle ':chpwd:profiles:/usr/src/debian(|/|/*)' profile debian # # When that's done and you enter a directory that matches the pattern # in the third part of the context, a function called chpwd_profile_grml, # for example, is called (if it exists). # # If no pattern matches (read: no profile is detected) the profile is # set to 'default', which means chpwd_profile_default is attempted to # be called. # # A word about the context (the ':chpwd:profiles:*' stuff in the zstyle # command) which is used: The third part in the context is matched against # ${PWD}. That's why using a pattern such as /foo/bar(|/|/*) makes sense. # Because that way the profile is detected for all these values of ${PWD}: # /foo/bar # /foo/bar/ # /foo/bar/baz # So, if you want to make double damn sure a profile works in /foo/bar # and everywhere deeper in that tree, just use (|/|/*) and be happy. # # The name of the detected profile will be available in a variable called # 'profile' in your functions. You don't need to do anything, it'll just # be there. # # Then there is the parameter $CHPWD_PROFILE is set to the profile, that # was is currently active. That way you can avoid running code for a # profile that is already active, by running code such as the following # at the start of your function: # # function chpwd_profile_grml() { # [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1 # ... # } # # The initial value for $CHPWD_PROFILE is 'default'. # # Version requirement: # This feature requires zsh 4.3.3 or newer. # If you use this feature and need to know whether it is active in your # current shell, there are several ways to do that. Here are two simple # ways: # # a) If knowing if the profiles feature is active when zsh starts is # good enough for you, you can put the following snippet into your # .zshrc.local: # # (( ${+functions[chpwd_profiles]} )) && print "directory profiles active" # # b) If that is not good enough, and you would prefer to be notified # whenever a profile changes, you can solve that by making sure you # start *every* profile function you create like this: # # function chpwd_profile_myprofilename() { # [[ ${profile} == ${CHPWD_PROFILE} ]] && return 1 # print "chpwd(): Switching to profile: $profile" # ... # } # # That makes sure you only get notified if a profile is *changed*, # not everytime you change directory, which would probably piss # you off fairly quickly. :-) # # There you go. Now have fun with that. local -x profile zstyle -s ":chpwd:profiles:${PWD}" profile profile || profile='default' if (( ${+functions[chpwd_profile_$profile]} )) ; then chpwd_profile_${profile} fi CHPWD_PROFILE="${profile}" return 0 } chpwd_functions=( ${chpwd_functions} chpwd_profiles ) # }}} # set colors for use in prompts {{{ if zrcautoload colors && colors 2>/dev/null ; then BLUE="%{${fg[blue]}%}" RED="%{${fg_bold[red]}%}" GREEN="%{${fg[green]}%}" CYAN="%{${fg[cyan]}%}" MAGENTA="%{${fg[magenta]}%}" YELLOW="%{${fg[yellow]}%}" WHITE="%{${fg[white]}%}" NO_COLOUR="%{${reset_color}%}" else BLUE=$'%{\e[1;34m%}' RED=$'%{\e[1;31m%}' GREEN=$'%{\e[1;32m%}' CYAN=$'%{\e[1;36m%}' WHITE=$'%{\e[1;37m%}' MAGENTA=$'%{\e[1;35m%}' YELLOW=$'%{\e[1;33m%}' NO_COLOUR=$'%{\e[0m%}' fi # }}} # {{{ set prompt if zrcautoload promptinit && promptinit 2>/dev/null ; then promptinit # people should be able to use their favourite prompt else print 'Notice: no promptinit available :(' fi setopt prompt_subst # make sure to use right prompt only when not running a command setopt transient_rprompt precmd () { if [[ $TERM == screen* ]] ; then if [[ -n ${VCS_INFO_message_1_} ]] ; then print -nP "\ek${VCS_INFO_message_1_}\e\\" else print -nP "\ekzsh\e\\" fi fi #if [[ $DONTSETRPROMPT -eq 0 ]] ; then # RPROMPT="%(?..:() " #fi # adjust title of xterm # see http://www.faqs.org/docs/Linux-mini/Xterm-Title.html [[ ${NOTITLE} -gt 0 ]] && return 0 case $TERM in (xterm*|rxvt*) print -Pn "\e]0;%n@%m: %~\a" ;; esac } # preexec() => a function running before every command preexec () { # set hostname if not running on host with name 'grml' if [[ -n "$HOSTNAME" ]] && [[ "$HOSTNAME" != $(hostname) ]] ; then NAME="@$HOSTNAME" fi # get the name of the program currently running and hostname of local machine # set screen window title if running in a screen if [[ "$TERM" == screen* ]] ; then # local CMD=${1[(wr)^(*=*|sudo|ssh|-*)]} # don't use hostname local CMD="${1[(wr)^(*=*|sudo|ssh|-*)]}$NAME" # use hostname echo -ne "\ek$CMD\e\\" fi # adjust title of xterm case $TERM in (xterm*|rxvt*) print -Pn "\e]0;%n@%m: $1\a" ;; esac } EXITCODE="%(?..%?%1v )" PS2='\`%_> ' # secondary prompt, printed when the shell needs more information to complete a command. PS3='?# ' # selection prompt used within a select loop. PS4='+%N:%i:%_> ' # the execution trace prompt (setopt xtrace). default: '+%N:%i>' PROMPT="[%n] [%~\$]%# " # }}} # {{{ 'hash' some often used directories hash -d pkg=/var/cache/pacman/pkg hash -d log=/var/log hash -d www=/home/www # }}} # {{{ some aliases # ls always colored alias ls='ls --color=auto' alias ll='ls -l --time-style="+%F, %H:%M " --group-directories-first --color=auto' alias la='ls -A --color=auto' alias l='ls -CF --color=auto' # long colored list, human readable sizes alias lh='ls -hAl --color=auto' alias cp='nocorrect cp' # no spelling correction on cp alias mkdir='nocorrect mkdir' # no spelling correction on mkdir alias mv='nocorrect mv' # no spelling correction on mv alias rm='nocorrect rm -I' # no spelling correction on rm #a1# Execute \kbd{rmdir} alias rd='rmdir' #a1# Execute \kbd{rmdir} alias md='mkdir' # I like clean prompt, so provide simple way to get that alias 0='return 0' # for really lazy people like mika: check_com S &>/dev/null || alias S='screen' check_com s &>/dev/null || alias s='ssh' # get top 10 shell commands: alias top10='print -l ? ${(o)history%% *} | uniq -c | sort -nr | head -n 10' # }}} # {{{ Use hard limits, except for a smaller stack and no core dumps unlimit limit stack 8192 limit -s # }}} # {{{ completion system # called later (via grmlcomp) # note: use 'zstyle' for getting current settings # press ^Xh (control-x h) for getting tags in context; ^X? (control-x ?) to run complete_debug with trace output grmlcomp() { # TODO: This could use some additional information # allow one error for every three characters typed in approximate completer zstyle ':completion:*:approximate:' max-errors 'reply=( $((($#PREFIX+$#SUFFIX)/3 )) numeric )' # don't complete backup files as executables zstyle ':completion:*:complete:-command-::commands' ignored-patterns '(aptitude-*|*\~)' # start menu completion only if it could find no unambiguous initial string zstyle ':completion:*:correct:*' insert-unambiguous true zstyle ':completion:*:corrections' format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}' zstyle ':completion:*:correct:*' original true # activate color-completion zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} # format on completion zstyle ':completion:*:descriptions' format $'%{\e[0;31m%}completing %B%d%b%{\e[0m%}' # complete 'cd -' with menu zstyle ':completion:*:*:cd:*:directory-stack' menu yes select # insert all expansions for expand completer zstyle ':completion:*:expand:*' tag-order all-expansions zstyle ':completion:*:history-words' list false # activate menu zstyle ':completion:*:history-words' menu yes # ignore duplicate entries zstyle ':completion:*:history-words' remove-all-dups yes zstyle ':completion:*:history-words' stop yes # match uppercase from lowercase zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' # separate matches into groups zstyle ':completion:*:matches' group 'yes' zstyle ':completion:*' group-name '' #zstyle ':completion:*' menu select=5 zstyle ':completion:*' menu yes select zstyle ':completion:*:messages' format '%d' zstyle ':completion:*:options' auto-description '%d' # describe options in full zstyle ':completion:*:options' description 'yes' # on processes completion complete all user processes zstyle ':completion:*:processes' command 'ps -au$USER' # offer indexes before parameters in subscripts zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters # provide verbose completion information zstyle ':completion:*' verbose true # recent (as of Dec 2007) zsh versions are able to provide descriptions # for commands (read: 1st word in the line) that it will list for the user # to choose from. The following disables that, because it's not exactly fast. zstyle ':completion:*:-command-:*:' verbose false # set format for warnings zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches for:%{\e[0m%} %d' # define files to ignore for zcompile zstyle ':completion:*:*:zcompile:*' ignored-patterns '(*~|*.zwc)' zstyle ':completion:correct:' prompt 'correct to: %e' # Ignore completion functions for commands you don't have: zstyle ':completion::(^approximate*):*:functions' ignored-patterns '_*' # Provide more processes in completion of programs like killall: zstyle ':completion:*:processes-names' command 'ps c -u ${USER} -o command | uniq' # complete manual by their section zstyle ':completion:*:manuals' separate-sections true zstyle ':completion:*:manuals.*' insert-sections true zstyle ':completion:*:man:*' menu yes select # provide .. as a completion zstyle ':completion:*' special-dirs .. # run rehash on completion so new installed program are found automatically: _force_rehash() { (( CURRENT == 1 )) && rehash return 1 } ## correction # some people don't like the automatic correction - so run 'NOCOR=1 zsh' to deactivate it if [[ "$NOCOR" -gt 0 ]] ; then zstyle ':completion:*' completer _oldlist _expand _force_rehash _complete _files _ignored setopt nocorrect else # try to be smart about when to use what completer... setopt correct zstyle -e ':completion:*' completer ' if [[ $_last_try != "$HISTNO$BUFFER$CURSOR" ]] ; then _last_try="$HISTNO$BUFFER$CURSOR" reply=(_complete _match _ignored _prefix _files) else if [[ $words[1] == (rm|mv) ]] ; then reply=(_complete _files) else reply=(_oldlist _expand _force_rehash _complete _ignored _correct _approximate _files) fi fi' fi # command for process lists, the local web server details and host completion zstyle ':completion:*:urls' local 'www' '/var/www/' 'public_html' # caching [[ -d $ZSHDIR/cache ]] && zstyle ':completion:*' use-cache yes && \ zstyle ':completion::complete:*' cache-path $ZSHDIR/cache/ # host completion /* add brackets as vim can't parse zsh's complex cmdlines 8-) {{{ */ [[ -r ~/.ssh/known_hosts ]] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=() [[ -r /etc/hosts ]] && : ${(A)_etc_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$(\n' "$0" && return 1 for file in "$@" ; do while [[ -h "$file" ]] ; do ls -l $file file=$(readlink "$file") done done } #f1# Edit an alias via zle edalias() { [[ -z "$1" ]] && { echo "Usage: edalias " ; return 1 } || vared aliases'[$1]' ; } compdef _aliases edalias #f1# Edit a function via zle edfunc() { [[ -z "$1" ]] && { echo "Usage: edfun " ; return 1 } || zed -f "$1" ; } compdef _functions edfunc # use it e.g. via 'Restart apache2' #m# f6 Start() \kbd{/etc/init.d/\em{process}}\quad\kbd{start} #m# f6 Restart() \kbd{/etc/init.d/\em{process}}\quad\kbd{restart} #m# f6 Stop() \kbd{/etc/init.d/\em{process}}\quad\kbd{stop} #m# f6 Reload() \kbd{/etc/init.d/\em{process}}\quad\kbd{reload} #m# f6 Force-Reload() \kbd{/etc/init.d/\em{process}}\quad\kbd{force-reload} #f1# Provides useful information on globbing H-Glob() { echo -e " / directories . plain files @ symbolic links = sockets p named pipes (FIFOs) * executable plain files (0100) % device files (character or block special) %b block special files %c character special files r owner-readable files (0400) w owner-writable files (0200) x owner-executable files (0100) A group-readable files (0040) I group-writable files (0020) E group-executable files (0010) R world-readable files (0004) W world-writable files (0002) X world-executable files (0001) s setuid files (04000) S setgid files (02000) t files with the sticky bit (01000) print *(m-1) # Files modified up to a day ago print *(a1) # Files accessed a day ago print *(@) # Just symlinks print *(Lk+50) # Files bigger than 50 kilobytes print *(Lk-50) # Files smaller than 50 kilobytes print **/*.c # All *.c files recursively starting in \$PWD print **/*.c~file.c # Same as above, but excluding 'file.c' print (foo|bar).* # Files starting with 'foo' or 'bar' print *~*.* # All Files that do not contain a dot chmod 644 *(.^x) # make all plain non-executable files publically readable print -l *(.c|.h) # Lists *.c and *.h print **/*(g:users:) # Recursively match all files that are owned by group 'users' echo /proc/*/cwd(:h:t:s/self//) # Analogous to >ps ax | awk '{print $1}'<" } alias help-zshglob=H-Glob check_com -c qma && alias ?='qma zshall' # grep for running process, like: 'any vim' any() { emulate -L zsh if [[ -z "$1" ]] ; then echo "any - grep for process(es) by keyword" >&2 echo "Usage: any " >&2 ; return 1 else local STRING=$1 local LENGTH=$(expr length $STRING) local FIRSCHAR=$(echo $(expr substr $STRING 1 1)) local REST=$(echo $(expr substr $STRING 2 $LENGTH)) ps ax| grep "[$FIRSCHAR]$REST" #ps xauwww| grep "[$FIRSCHAR]$REST" fi } # a wrapper for vim, that deals with title setting # VIM_OPTIONS # set this array to a set of options to vim you always want # to have set when calling vim (in .zshrc.local), like: # VIM_OPTIONS=( -p ) # This will cause vim to send every file given on the # commandline to be send to it's own tab (needs vim7). #vim() { # VIM_PLEASE_SET_TITLE='yes' command vim ${VIM_OPTIONS} "$@" #} # make a backup of a file bk() { cp -b "${1}" "$(echo $1 | sed "s/\./_`date +%Y-%m-%d_%T`./")" } # }}} # {{{ make sure our environment is clean regarding colors for color in BLUE RED GREEN CYAN YELLOW MAGENTA WHITE ; unset $color # }}} # "persistent history" {{{ # just write important commands you always need to ~/.important_commands if [[ -r ~/.important_commands ]] ; then fc -R ~/.important_commands fi # }}} # variables {{{ # set terminal property (used e.g. by msgid-chooser) export COLORTERM="yes" [[ -d /usr/share/qt4 ]] && export QTDIR=/usr/share/qt4 # }}} # aliases {{{ # general alias da='du -sch' alias j='jobs -l' # listing stuff alias dir="ls -lSrah" #a2# Only show dot-directories alias lad='ls -d .*(/)' # only show dot-directories #a2# Only show dot-files alias lsa='ls -a .*(.)' # only show dot-files #a2# Only files with setgid/setuid/sticky flag alias lss='ls -l *(s,S,t)' # only files with setgid/setuid/sticky flag #a2# Only show 1st ten symlinks alias lsl='ls -l *(@)' # only symlinks #a2# Display only executables alias lsx='ls -l *(*)' # only executables #a2# Display world-{readable,writable,executable} files alias lsw='ls -ld *(R,W,X.^ND/)' # world-{readable,writable,executable} files #a2# Display the ten biggest files alias lsbig="ls -flh *(.OL[1,10])" # display the biggest files #a2# Only show directories alias lsd='ls -d *(/)' # only show directories #a2# Only show empty directories alias lse='ls -d *(/^F)' # only show empty directories #a2# Display the ten newest files alias lsnew="ls -rl *(D.om[1,10])" # display the newest files #a2# Display the ten oldest files alias lsold="ls -rtlh *(D.om[1,10])" # display the oldest files #a2# Display the ten smallest files alias lssmall="ls -Srl *(.oL[1,10])" # display the smallest files alias sl='ls -la' alias tree='ls -Csu' alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias .....='cd ../../../..' # global aliases alias -g G='| grep' alias -g H='| head' alias -g L='| less' alias -g N='> /dev/null' alias -g S='&> /dev/null' alias -g M='| more' alias -g T='| tail' alias -g W='| wc' alias -g X='| xargs' alias -g Y='2>&1' function filegrep() { if [ -z $2 ] then grep -Hrin --color=auto "$1" * else grep -Hrin --color=auto "$1" * --exclude "*$2*" fi } # type a directory's name to cd to it. compctl -/ cd # some useful aliases #a2# Execute \kbd{mkdir -o} alias md='mkdir -p' # simple webserver alias http="python -m SimpleHTTPServer" # Use 'g' instead of 'git': alias g='git' # }}} # useful functions {{{ #' #f5# cd to directoy and list files cl() { emulate -L zsh cd $1 && ls -a } # use colors when GNU grep with color-support alias grep='grep --color=auto' alias GREP='grep -i --color=auto' # d():Copyright 2005 Nikolai Weibull # note: option AUTO_PUSHD has to be set #f5# Jump between directories d() { emulate -L zsh autoload -U colors local color=$fg_bold[blue] integer i=0 dirs -p | while read dir; do local num="${$(printf "%-4d " $i)/ /.}" printf " %s $color%s$reset_color\n" $num $dir (( i++ )) done integer dir=-1 read -r 'dir?Jump to directory: ' || return (( dir == -1 )) && return if (( dir < 0 || dir >= i )); then echo d: no such directory stack entry: $dir return 1 fi cd ~$dir } vman() { emulate -L zsh man $* | col -b | view -c 'set ft=man nomod nolist' - } # function readme() { $PAGER -- (#ia3)readme* } #f5# View all README-like files in current directory in pager readme() { emulate -L zsh local files files=(./(#i)*(read*me|lue*m(in|)ut)*(ND)) if (($#files)) ; then $PAGER $files else print 'No README files.' fi } # function ansi-colors() #f5# Display ANSI colors ansi-colors() { typeset esc="\033[" line1 line2 echo " _ _ _40 _ _ _41_ _ _ _42 _ _ 43_ _ _ 44_ _ _45 _ _ _ 46_ _ _ 47_ _ _ 49_ _" for fore in 30 31 32 33 34 35 36 37; do line1="$fore " line2=" " for back in 40 41 42 43 44 45 46 47 49; do line1="${line1}${esc}${back};${fore}m Normal ${esc}0m" line2="${line2}${esc}${back};${fore};1m Bold ${esc}0m" done echo -e "$line1\n$line2" done } # }}} ## END OF FILE ################################################################# # vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4