#################################### ## Configuration file for Z Shell ## ## By: Thiam H Lee ## ## Last Updated: Jul 13 2011 ## #################################### ## Zsh versions after 3.1 have a new completions system and bytecode compilation ## Returns true if $ZSH_VERSION > 3.1 and false otherwise new_zsh_version () { [[ $ZSH_VERSION > 3.1 ]] } ############################################ ## Contents: ## ## 1. Aliases ## ## 2. Environment Options ## ## 3. Prompt ## ## 4. Key Bindings ## ## 5. Programmable Completions ## ## 6. Shell Functions Specific to Z Shell ## ## 7. Other Functions ## ## 8. Cleanup ## ############################################ ############## # 1. Aliases # ############## alias mv='nocorrect mv' ## no spelling correction on mv ## alias cp='nocorrect cp' ## no spelling correction on cp ## alias mkdir='nocorrect mkdir' ## no spelling correction on mkdir ## alias j='jobs' alias pu='pushd' alias po='popd' alias d='dirs -v' alias h='history' alias grep='egrep' ## The following aliases assume the variable ${LS_OPTIONS} is a string that ## contains the machine-specific ls flags for colors and other options, which ## should be set in ~/.zprofile or ~/.zshenv or ~/.profile . The flags used ## below ( -a , -d , -l ) should be common to ls across most Operating Systems # List all (including hidden) files in long listing format alias l='\ls ${=LS_OPTIONS} -al' # List unhidden files in long listing format alias ll='\ls ${=LS_OPTIONS} -l' # List unhidden files alias ls='\ls ${=LS_OPTIONS}' # List all files alias lsa='\ls ${=LS_OPTIONS} -a' # List unhidden regular files in the current directory alias lsr='test "${options[glob]}" = "on" && \ls ${=LS_OPTIONS} [^.]*(.)' # List all regular files in the current directory alias lsar='test "${options[glob]}" = "on" && \ls ${=LS_OPTIONS} *(.)' # List unhidden directories in the current directory alias lsd='test "${options[glob]}" = "on" && \ls ${=LS_OPTIONS} -d [^.]*(/)' # List all directories in the current directory alias lsad='test "${options[glob]}" = "on" && \ls ${=LS_OPTIONS} -d *(/)' ## Import machine-specific aliases from .sh_aliases if it exists ## Note: Aliases for distribution-specific tools (for instance an alias to ## 'zypper --no-refresh' in OpenSUSE) and also for userland tools to be ## overloaded to use flags that may differ across Operating Systems (e.g. the ## flag enabling confirmation for rm) should be defined in ~/.sh_aliases if [[ -f ~/.sh_aliases ]] then source ~/.sh_aliases fi ########################## # 2. Environment Options # ########################## ## Z Shell options setopt \ ALWAYS_LAST_PROMPT \ AUTO_LIST \ AUTO_MENU \ COMPLETE_ALIASES \ COMPLETE_IN_WORD \ CORRECT_ALL \ EXTENDED_GLOB \ GLOB \ HIST_IGNORE_DUPS \ HIST_IGNORE_SPACE \ ZLE if new_zsh_version then setopt \ HIST_EXPIRE_DUPS_FIRST \ HIST_REDUCE_BLANKS \ HIST_IGNORE_ALL_DUPS \ INC_APPEND_HISTORY \ SHARE_HISTORY else setopt \ APPEND_HISTORY fi ## History settings HISTSIZE=1000 SAVEHIST=1000 HISTFILE=~/.zsh_history ## Use ViM as default editor export EDITOR='vim' export VISUAL=$EDITOR ############# # 3. Prompt # ############# ## Left prompt : [username@hostname:pwd(max 30 chars)]{#(root) or %(non-root)} ## Right prompt : (time) PROMPT="$(print '[%{\e[0;31m%}%B%n%b%{\e[0m%}@%{\e[0;34m%}%B%m%b%{\e[0m%}:%{\e[0;35m%}%30<..<%~%{\e[0m%}]%{\e[0;36m%}%#%{\e[0m%} ')" RPROMPT="$(print '%{\e[1;34m%}(%{\e[1;33m%}%T%{\e[1;34m%})%{\e[0m%}')" ## Dynamically set titlebar case $TERM in (screen*) precmd () { print -Pn "\ekzsh\e\\" } preexec () { local CMD=${1[(wr)^(*=*|sudo|-*)]} print -Pn "\ek$CMD\e\\" } ;; (xterm*|rxvt*|(a|dt|k|E)term) precmd () { print -Pn "\e]0;%n@%m: %35<...<%~\a" } preexec () { print -Pn "\e]0;%n@%m: $1%35>...>\a" } ;; esac ################### # 4. Key bindings # ################### ## Use Emacs key bindings (otherwise Zsh uses bindings of $EDITOR by default) bindkey -e ## Do history expansion on space bindkey ' ' magic-space ################################# ## 5. Programmable Completions ## ################################# if new_zsh_version then ## If Z Shell supports the new completion implemented after 3.1.6, use it autoload -U compinit compinit -C ## completions ## autoload -U zstyle+ ## General completion technique ## complete as much u can .. zstyle ':completion:*' completer _complete _list _oldlist _expand _ignored _match _correct _approximate _prefix ## complete less #zstyle ':completion:*' completer _complete _list _expand _ignored _approximate ## complete minimal #zstyle ':completion:*' completer _complete _ignored ## Additional completion options ## fuzzy matching of completions when commands or arguments are mistyped zstyle ':completion:*' completer _complete _match _approximate zstyle ':completion:*:match:*' original only ## allow number of errors allowed by _approximate to vary with entry length zstyle ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)' ## ignore completion functions for commands that do not exist zstyle ':completion:*:functions' ignored-patterns '_*' ## formatting and messages zstyle ':completion:*' verbose yes zstyle ':completion:*:descriptions' format $'%{\e[0;31m%}%d%{\e[0m%}' zstyle ':completion:*:messages' format $'%{\e[0;31m%}%d%{\e[0m%}' zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches for: %d%{\e[0m%}' zstyle ':completion:*:corrections' format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}' zstyle ':completion:*' group-name '' ## determine in which order the names (files) should be ## listed and completed when using menu completion. ## 'size' : sort by the size of the file ## 'links' : sort by the number of links to the file ## 'modification' or `time' or `date' : sort by the last modification time ## 'access' : sort by the last access time ## 'inode' or `change' : sort by the last inode change time ## 'reverse' : sort in decreasing order ## If the style is set to any other value, or is unset, files will be ## sorted alphabetically by name. zstyle ':completion:*' file-sort name ## how many completions switch on menu selection ## use 'long' to start menu compl. if list is bigger than screen ## or some number to start menu compl. if list has that number ## of completions (or more). zstyle ':completion:*' menu select=long ## case-insensitive (uppercase from lowercase) completion #zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' ## case-insensitive (all) completion #zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' ## case-insensitive,partial-word and then substring completion zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' ## offer indexes before parameters in subscripts zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters ## insert all expansions for expand completer zstyle ':completion:*:expand:*' tag-order all-expansions ## ignore completion functions (until the _ignored completer) zstyle ':completion:*:functions' ignored-patterns '_*' ## completion caching zstyle ':completion::complete:*' use-cache on zstyle ':completion::complete:*' cache-path ~/.zcompcache/$HOST ## add colors to completions zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} ## don't complete backup files as executables zstyle ':completion:*:complete:-command-::commands' ignored-patterns '*\~' ## filename suffixes to ignore during completion (except after rm command) zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.(o|c~|old|pro|zwc)' ## add colors to processes for kill completion zstyle ':completion:*:*:kill:*:processes' command 'ps -axco pid,user,command' zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' fi if ! new_zsh_version then ## If Zsh does not support the new completion system, set up programmable ## completions using old method (compctl). ## To see more examples, check Misc/compctl-examples in the zsh distribution. ## Filename suffixes to ignore on completion fignore=(.o .c~ .old .pro) compctl -g '*(-/)' cd pushd compctl -g '*(/)' rmdir dircmp compctl -j -P '%' -x 's[-] p[1]' -k signals -- kill compctl -j -P '%' fg bg wait jobs disown compctl -A shift compctl -caF type whence which compctl -F unfunction compctl -a unalias compctl -v unset compctl -e disable compctl -d enable ## Commands that are usually used on non-generated files. compctl -g '*.c *.cc *.C *.cxx *.cpp *.h *.tex *.txt *.html *.asc *.log' \ + -g '^*.(o|a|so|aux|dvi|log|swp|fig|bbl|blg|bst|idx|ind|out|toc)' \ + -g '*.web *.docbook' \ + -g '*.gz *.Z *.bz2' \ + -g '.*' \ + -g '*(/)' vi vim gvim less grep zgrep ## Latex, pdflatex: complete only .tex files compctl -g '*.tex *(/)' latex pdflatex pdftex ## Gv: complete only .ps and .pdf files compctl -g '*.ps *.ps.gz *.ps.Z *.pdf *(/)' gv ## Typeset, declare, export, local, integer, readonly: match variable ## names by default. compctl -v -x 'R[-*f,^*]' -F \ - 's[-][+]' -k '(L R Z f i l r t u x m)' \ -- typeset declare local integer export readonly ## getopts: match names, second parameter only. compctl -x 'p[2]' -v -- getopts ## ls, dir: complete files, options (both - and -- kind), and option params. ## This is for GNU ls. compctl -f \ -x s'[--format]' -P '=' -k '(long verbose commas horizontal across vertical single-column)' \ - s'[--sort]' -P '=' -k '(none time size extension)' \ - s'[--time]' -P '=' -k '(atime ctime access use status)' \ - s'[--width=][--tabsize=][--ignore=][-w][-T][-I],c[-1,-w][-1,-T][-1,-I]' \ -k '( )' \ - s'[--]' -S '' -k '(all\ escape\ directory\ inode\ kilobytes\ numeric-uid-gid\ no-group\ hide-control-chars\ reverse\ size\ width= tabsize= almost-all\ ignore-backups\ classify\ file-type\ full-time\ ignore= dereference\ literal\ quote-name\ no-color\ 7bit\ 8bit\ recursive\ sort= format= time= help\ version\ )' \ - s'[-]' -k '(a b c d f g i k l m n o p q r s t u x A B C F G L N Q R S U X 1 w T I)' \ -- ls dir ## man: complete commands, otherwise complete by search of $MANPATH. ## This is placed as an all-encompassing pattern at the end because ## making it the default before the -x doesn't work. (It becomes ## '-c + (-K 'match-man' -x ...), not (-c + -K 'match-man') -x ...). ## We also complete paths for -M (override manpath), commands for ## -P (pager) and disable for -S (search sections). After an explicit ## number (which it helps to complete for you), these completion rules ## assume a thorough search is needed and no longer uses the '-c' hashed ## commands, relying entirely on what's really in the manpath. compctl -x 'S[1][2][3][4][5][6][7][8][9]' -k '(1 2 3 4 5 6 7 8 9)' \ - 'R[[1-9nlo]|[1-9](|[a-z]),^*]' -K 'match-man' \ - 's[-M],c[-1,-M]' -g '*(-/)' \ - 's[-P],c[-1,-P]' -c \ - 's[-S],s[-1,-S]' -k '( )' \ - 's[-]' -k '(a d f h k t M P)' \ - 'p[1,-1]' -c + -K 'match-man' \ -- man ## tar: complete tar files (only .tar or .tar.* format) after -f, disable ## completion for certain options, let user choose directory with -C, ## complete GNU tar long options beginning with --. The match-taropts ## function prompts for GNU tar options, ensures one of the seven ## mandatory options is given in the first argument to tar, and enforces ## spaces after options that take an argument (this is required by GNU ## tar and also makes filename completion possible for the -f option). ## Note that the -[0-7][lmh] options are not completed, but they're ## hardly ever used. compctl -f \ -x 'C[-1,-*f],p[2] C[-1,*f],c[-1,--file]' -g '*.tar(|.*)' + -g '*(-/)' \ - 'C[-1,-*[bLN]],p[2] C[-1,*[bLN]],c[-1,--block-size][-1,tape-length][-1,--after-date][-1,--newer]' -k '( )' \ - 'C[-1,-*C],p[2] C[-1,*C],c[-1,directory]' -g '*(-/)' \ - 'C[-1,-*[FgKTV]],p[2] C[-1,*[FgKTV]],c[-1,--info-script][-1,--new-volume-script][-1,--starting-file][-1,--files-from][-1,--label][-1,--exclude]' -f \ - 's[--]' -k '(catenate concatenate create diff compare delete append list update extract get atime-preserve block-size read-full-blocks directory checkpoint file force-local info-script new-volume-script incremental dereference ignore-zeros ignore-failed-read keep-old-files starting-file one-file-system tape-length modification-time multi-volume after-date newer old-archive portability to-stdout same-permissions preserve-permissions absolute-paths preserve record-number remove-files same-order preserve-order same-owner sparse files-from null totals verbose label version interactive confirmation verify exclude exclude-from compress uncompress gzip ungzip use-compress-program block-compress)' \ - 's[-],p[1]' -S '' -K 'match-taropts' \ -- tar fi ########################################## # 6. Shell Functions Specific to Z Shell # ########################################## ## Enable setenv() for csh compatibility setenv() { typeset -x "${1}${1:+=}${(@)argv[2,$#]}" } ## Reload zsh, i.e. remove ~/.zcompdump* and re-source .zshrc ## which recompiles functions if necessary reload_zsh() { if new_zsh_version then ## Recompilation of functions only required for zsh version > 3.1.x [[ -f ~/.zcompdump ]] && rm -f ~/.zcompdump [[ -f ~/.zcompdump.zwc ]] && rm -f ~/.zcompdump.zwc fi exec ${SHELL} } ###################### # 7. Other Functions # ###################### ## Import additional shell functions from .sh_functions if it exists if [[ -f ~/.sh_functions ]] then source ~/.sh_functions fi ############## # 8. Cleanup # ############## ## Make sure compiled functions (only for newer zsh versions) are up to date if new_zsh_version then ! [[ -f ~/.zshrc.zwc ]] && zcompile ~/.zshrc ! [[ -f ~/.zcompdump.zwc ]] && zcompile ~/.zcompdump autoload -U zrecompile [[ ~/.zshrc -nt ~/.zshrc.zwc ]] && zrecompile -p ~/.zshrc [[ ~/.zcompdump -nt ~/.zcompdump.zwc ]] && zrecompile -p ~/.zcompdump [[ -f ~/.zshrc.zwc.old ]] && rm -f ~/.zshrc.zwc.old [[ -f ~/.zcompdump.zwc.old ]] && rm -f ~/.zcompdump.zwc.old fi