+# Complete arguments and fix spelling mistakes when possible.
+zstyle ':completion:*' completer _complete _match _correct _approximate
+
+# Make sure the list of possible completions is displayed after pressing <TAB>
+# the first time.
+setopt nolistambiguous
+# Allow completions in the middle of a text, i.e. "/usr/bin/<TAB>whatever"
+# completes like "/usr/bin/<TAB>". Useful when adding new options to commands.
+bindkey "^I" expand-or-complete-prefix
+# Try uppercase if the currently typed string doesn't match. This allows
+# typing in lowercase most of the time and completion fixes the case.
+zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}'
+
+# Use ls like colors for completions.
+zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
+
+# Make completion lists scrollable so "do you wish to see all n possibilities"
+# is no longer displayed.
+zstyle ':completion:*' list-prompt '%p'
+# Display group name (like 'external command', 'alias', etc.) when there are
+# multiple matches in bold.
+zstyle ':completion:*' format ' %B%d%b:'
+# Display different types of matches separately.
+zstyle ':completion:*' group-name ''
+
+# Ignore completion functions.
+zstyle ':completion:*:functions' ignored-patterns '_*'
+# Ignore parent directory.
+zstyle ':completion:*:(cd|mv|cp):*' ignore-parents parent pwd
+# When unsetting variables make sure every variable name is only suggested
+# once.
+zstyle ':completion:*:unset:*' ignore-line yes
+# When working with Mercurial and Git don't complete the same file multiple
+# times. Very useful when completing file names.
+zstyle ':completion:*:(hg|git)*:*' ignore-line yes
+
+
+# CUSTOM ALIASES AND FUNCTIONS
+
+# Make sure aliases are expanded when using sudo.
+alias sudo='sudo '
+
+# Simplify calls to less, automatically redirects all output.
+alias -g L='2>&1 | less'
+# Simplify calls to colordiff, output is also piped through less.
+alias -g D='2>&1 | colordiff L'
+# Simplify calls to grep.
+alias -g G='| grep'
+
+# Automatically use unified diffs.
+alias diff='diff -u'
+
+# Display all files and use human readable sizes.
+alias du='du -sh'
+
+# Use human readable sizes.
+alias df='df -h'
+
+# Multiple files given to Vim are opened in tabs, supported since Vim 7.
+if [[ ${${${(f)"$(vim --version)"}[1]#VIM - Vi IMproved }%% *} == 7* ]]; then
+ alias vim='vim -p'
+fi
+
+# Shortcuts for often used programs.
+alias e='elinks'
+alias g='git'
+alias m='mutt'
+alias v='vim'
+alias vi='vim'
+
+# I sometimes confuse editor and shell, print a warning to prevent I exit the
+# shell.
+alias :q='echo "This is not Vim!" >&2'
+
+# Edit the mercurial patch queue series file for the current mercurial
+# repository in Vim. Also change Vim's pwd to the patches directory so other
+# patches can easily be opened.
+alias vqs='vim -c "cd $(hg root)/.hg/patches/" "$(hg root)/.hg/patches/series"'
+
+# Make going up directories simple.
+alias -g ...='../..'
+alias -g ....='../../..'
+alias -g .....='../../../..'
+
+# Improved ls which displays the files in columns (-C), visualizes
+# directories, links and other special files (-F) and pages everything through
+# less (L).
+#
+# If available use GNU ls with colorized output. If it isn't available use
+# normal ls which needs CLICOLOR_FORCE so it displays colors when used with a
+# pager.
+ls --color &> /dev/null
+if [[ $? -eq 0 ]]; then
+ alias ls='ls --color'
+else
+ alias ls='CLICOLOR_FORCE=1 ls -G'
+fi
+# Main ls function.
+function ls() {
+ command ls -C -F $* L
+}
+# Helper function to list all files.
+function la() {
+ ls -a $*
+}
+# Helper function to list all files in list format with access rights, etc.
+function ll() {
+ la -l $*
+}
+
+# If ^C is pressed while typing a command, add it to the history so it can be
+# easily retrieved later and then abort like ^C normally does. This is useful
+# when I want to abort an command to do something in between and then finish
+# typing the command.
+TRAPINT() {
+ # Store the current buffer in the history.
+ zle && print -s $BUFFER
+
+ # Return the default exit code so zsh aborts the current command.
+ return $1
+}
+
+# Display TODOs stored in ~/.todo if this file exists.
+todo() {
+ if [[ -f ~/.todo ]]; then
+ cat ~/.todo | $PAGER
+ fi
+}
+
+# Colorize stderr. Very useful when looking for errors. Thanks to
+# http://gentoo-wiki.com/wiki/Zsh
+exec 2>>(while read line; do
+ print '\e[91m'${(q)line}'\e[0m' > /dev/tty; print -n $'\0'; done &)
+
+
+# RUN COMMANDS
+
+# If not already in screen reattach to a running session or create a new one.
+#
+# screen* is necessary as `screen` uses screen.linux for example for a linux
+# console which would otherwise cause an infinite loop.
+if [[ $TERM != screen* && $TERM != 'dumb' ]]; then
+ # Create a new session if none is running.
+ if [[ $(screen -list | grep "Detached" | wc -l) == 0 ]]; then
+ screen
+ # Reattach to a running session.
+ else
+ screen -r
+ fi
+fi
+
+
+# Load rc file for current OS.
+source_config ~/.zsh os rc $(uname) nolocal
+# Load rc file for current hostname (first part before a dot) or rc.local.
+source_config ~/.zsh host rc ${$(hostname)//.*/}
+
+source_debug "finished sourcing ~/.zsh/rc"
+
+# vim: ft=zsh