+
+# PROMPT SETTINGS
+
+# Use colorized output, necessary for prompts and completions.
+autoload -Uz colors && colors
+
+# Necessary for $EPOCHSECONDS, the UNIX time.
+zmodload zsh/datetime
+
+# Some shortcuts for colors. The %{...%} tells zsh that the data in between
+# doesn't need any space, necessary for correct prompt drawing.
+local red="%{${fg[red]}%}"
+local blue="%{${fg[blue]}%}"
+local green="%{${fg[green]}%}"
+local yellow="%{${fg[yellow]}%}"
+local default="%{${fg[default]}%}"
+
+# vcs_info was added in 4.3.9 but it works in earlier versions too. So load it
+# if the necessary files are available in ~/.zsh/functions/vcs_info (often a
+# symbolic link to current checkout of Zsh's sources).
+if [[ $ZSH_VERSION == (4.3.<9->*|4.<4->*|<5->*) ||
+ -d ~/.zsh/functions/vcs_info ]]; then
+ # Update fpath to allow loading the vcs_info functions.
+ if [[ -d ~/.zsh/functions/vcs_info ]]; then
+ fpath=(~/.zsh/functions/vcs_info/
+ ~/.zsh/functions/vcs_info/Backends
+ $fpath)
+ fi
+
+ # Load vcs_info to display information about version control repositories.
+ autoload -Uz vcs_info
+ # Only look for git and mercurial repositories; the only I use.
+ zstyle ':vcs_info:*' enable git hg
+ # Check the repository for changes so they can be used in %u/%c (see
+ # below). This comes with a speed penalty for bigger repositories.
+ zstyle ':vcs_info:*' check-for-changes yes
+
+ # Set style of vcs_info display. The current branch (green) and VCS (blue)
+ # is displayed. If there is an special action going on (merge, rebase)
+ # it's also displayed (red). Also display if there are unstaged or staged
+ # (%u/%c) changes.
+ if [[ $ZSH_VERSION == (4.3.<11->*|4.<4->*|<5->*) ||
+ -d ~/.zsh/functions/vcs_info ]]; then
+ zstyle ':vcs_info:*' formats \
+ "($green%b%u%c$default:$blue%s$default)"
+ zstyle ':vcs_info:*' actionformats \
+ "($green%b%u%c$default/$red%a$default:$blue%s$default)"
+ else
+ # In older versions %u and %c are not defined yet and are not
+ # correctly expanded.
+ zstyle ':vcs_info:*' formats \
+ "($green%b$default:$blue%s$default)"
+ zstyle ':vcs_info:*' actionformats \
+ "($green%b$default/$red%a$default:$blue%s$default)"
+ fi
+ # Set style for formats/actionformats when unstaged (%u) and staged (%c)
+ # changes are detected in the repository; check-for-changes must be set to
+ # true for this to work. Thanks to Bart Trojanowski
+ # (http://jukie.net/~bart/blog/pimping-out-zsh-prompt) for the idea
+ # (2010-03-11 00:20 CET).
+ zstyle ':vcs_info:*' unstagedstr '¹'
+ zstyle ':vcs_info:*' stagedstr '²'
+
+ # Default to run vcs_info. If possible we prevent running it later for
+ # speed reasons. If set to a non empty value vcs_info is run.
+ zshrc_force_run_vcs_info=1
+
+ # Cache system inspired by Bart Trojanowski
+ # (http://jukie.net/~bart/blog/pimping-out-zsh-prompt).
+ zstyle ':vcs_info:*+pre-get-data:*' hooks pre-get-data
+ +vi-pre-get-data() {
+ # Only Git and Mercurial support and need caching. Abort if any other
+ # VCS is used.
+ [[ "$vcs" != git && "$vcs" != hg ]] && return
+
+ # If the shell just started up or we changed directories (or for other
+ # custom reasons) we must run vcs_info.
+ if [[ -n $zshrc_force_run_vcs_info ]]; then
+ zshrc_force_run_vcs_info=
+ return
+ fi
+
+ # Don't run vcs_info by default to speed up the shell.
+ ret=1
+ # If a git/hg command was run then run vcs_info as the status might
+ # need to be updated.
+ case "$(fc -ln $(($HISTCMD-1)))" in
+ git* | g\ *)
+ ret=0
+ ;;
+ hg*)
+ ret=0
+ ;;
+ esac
+ }
+
+ # Must run vcs_info when changing directories.
+ prompt_chpwd() {
+ zshrc_force_run_vcs_info=1
+ }
+ chpwd_functions+=(prompt_chpwd)
+
+ # Used by prompt code below to determine if vcs_info should be run.
+ zshrc_use_vcs_info=1
+else
+ zshrc_use_vcs_info=
+fi
+
+typeset -a zshrc_longrun_data
+zshrc_longrun_data=()
+# Display runtime in seconds for long running programs (> 60 seconds) and send
+# a bell to notify me.
+zshrc_longrun_preexec() {
+ local program="$3"
+
+ # Handle fg.
+ local REPLY
+ zshrc_resolve_fg_to_resumed_job_name "$program"
+ program="$REPLY"
+
+ # Don't track the time for certain (possible) long running processes which
+ # need no automatic notification.
+ for ignore in elinks man mutt vim; do
+ case $program in
+ $ignore | $ignore\ *)
+ zshrc_longrun_data=()
+ return
+ ;;
+ esac
+ done
+
+ zshrc_longrun_data=("$program" $EPOCHSECONDS)
+}
+zshrc_longrun_precmd() {
+ # No previous timestamp available or disabled for this command, ignore.
+ if [[ -z $zshrc_longrun_data ]]; then
+ return
+ fi
+
+ local difference=$(( EPOCHSECONDS - zshrc_longrun_data[2] ))
+ if [[ $difference -gt 60 ]]; then
+ echo
+ echo -n "${fg[yellow]}"
+ echo -n "~> ${(V)zshrc_longrun_data[1]} took $difference seconds."
+ echo -n "${fg[default]}"
+ echo "\a" # send bell
+ fi
+
+ # Clear status. Prevents displaying old status information when pressing
+ # enter with an empty command line.
+ zshrc_longrun_data=()
+}
+preexec_functions+=(zshrc_longrun_preexec)
+precmd_functions+=(zshrc_longrun_precmd)
+
+# Set the prompt. A two line prompt is used. On the top left the current
+# working directory is displayed, on the right vcs_info (if available) and the
+# current time in hex. On the bottom left current user name and host is shown,
+# the exit code of the last command if it wasn't 0, the number of running jobs
+# if not 0.
+#
+# The prompt is in green and blue to make easily detectable, the error exit
+# code in red and bold and the job count in yellow. Designed for dark
+# terminals.
+#
+# Thanks to Adam's prompt for the basic idea of this prompt.
+zshrc_prompt_precmd() {
+ # Regex to remove elements which take no space. Used to calculate the
+ # width of the top prompt. Thanks to Bart's and Adam's prompt code in
+ # Functions/Prompts/prompt_*_setup.
+ local zero='%([BSUbfksu]|([FB]|){*})'
+
+ # Call vcs_info before every prompt.
+ if [[ -n $zshrc_use_vcs_info ]]; then
+ vcs_info
+ else
+ vcs_info_msg_0_=
+ fi
+
+ # Setup. Create variables holding the formatted content.
+
+ # Current directory in yellow, truncated if necessary (WIDTH is replaced
+ # below).
+ local directory="${yellow}%WIDTH<..<%~%<<${default}"
+
+ # Information about the VCS in this directory.
+ local vcs="${vcs_info_msg_0_}"
+ # Current time (seconds since epoch) in Hex in bright blue.
+ local seconds="${blue}%B0x$(([##16] EPOCHSECONDS))%b${default}"
+
+ # User name (%n) in bright green.
+ local user="${green}%B%n%b${default}"
+ # Host name (%m) in bright green; underlined if running on a remote system
+ # through SSH.
+ local host="${green}%B%m%b${default}"
+ if [[ -n $SSH_CONNECTION ]]; then
+ host="%U${host}%u"
+ fi
+
+ # Number of background processes in yellow if not zero.
+ local background="%(1j.${yellow}%j${default}.)"
+ # Exit code in bright red in parentheses if not zero.
+ local exitcode="%(?..(${red}%B%?%b${default}%) )"
+
+ # Prefix characters in first and second line.
+ local top_prefix="${blue}%B.-%b${default}"
+ local bottom_prefix="${blue}%B'%b${default}"
+
+ # Combine them to create the prompt.
+
+ local top_right="${vcs}(${seconds})"
+
+ local width_top_prefix=${#${(S%%)top_prefix//$~zero/}}
+ local width_top_right=${#${(S%%)top_right//$~zero/}}
+
+ # Calculate the maximum width of ${top_left}. -2 are the braces of
+ # ${top_left}, -1 is one separator from ${top_separator} (we want at least
+ # one between left and right parts).
+ local top_left_width_max=$((
+ COLUMNS - $width_top_prefix - 2 - 1 - $width_top_right
+ ))
+ # Truncate directory if necessary.
+ local top_left="(${directory/WIDTH/${top_left_width_max}})"
+ local width_top_left=${#${(S%%)top_left//$~zero/}}
+
+ # Calculate the width of the top prompt to fill the middle with "-".
+ local width=$((
+ COLUMNS - width_top_prefix - width_top_left - width_top_right
+ ))
+ local top_separator="%B${blue}${(l:${width}::-:)}%b${default}"
+
+ PROMPT="${top_prefix}${top_left}${top_separator}${top_right}
+${bottom_prefix}${user}@${host} ${background}%# ${exitcode}"
+}
+precmd_functions+=(zshrc_prompt_precmd)
+
+
+# When GNU screen, tmux, xterm or rxvt is used set the name of the window to
+# the currently running program.
+#
+# When a program is started preexec() sets the window's name to it; when it
+# stops precmd() resets the window's name to 'zsh'. 'fg' is supported and sets
+# the window's name to the resumed job.
+#
+# It works with GNU screen, tmux, xterm and rxvt.
+#
+# If a command is run with sudo or if the shell is running as root then a ! is
+# added at the beginning of the command to make this clear. If a command is
+# running on a different computer with ssh a @ is added at the beginning. If
+# screen/tmux is running on the remote machine instead of @screen @:hostname
+# (or @tmux ..; hostname replaced by the machine's hostname) is displayed.
+# This only works if the .zshrc on the server also uses this command.
+#
+# screen* is necessary as `screen` uses screen.linux for example for a linux
+# console.
+if [[ $TERM == screen* || $TERM == xterm* || $TERM == rxvt* ]]; then
+ # Is set to a non empty value to reset the window name in the next
+ # precmd() call.
+ zshrc_window_reset=yes
+
+ zshrc_window_preexec() {
+ # Get the program name with its arguments.
+ local program_name=$1
+
+ # When sudo is used, use real program name instead, but with an
+ # exclamation mark at the beginning (handled below).
+ local program_sudo=
+ if [[ $program_name == sudo* ]]; then
+ program_name=${program_name#sudo }
+ program_sudo=yes
+ fi
+
+ # Handle fg.
+ local REPLY
+ zshrc_resolve_fg_to_resumed_job_name "$program_name"
+ program_name="$REPLY"
+
+ # Remove all arguments from the program name.
+ program_name=${program_name%% *}
+
+ # Ignore often used commands which are only running for a very short
+ # time. This prevents a "blinking" name when it's changed to "cd" for
+ # example and then some milliseconds later back to "zsh".
+ [[ $program_name == (cd*|d|ls|l|la|ll|clear|c) ]] && return
+
+ # Change my shortcuts so the real name of the program is displayed.
+ case $program_name in
+ e)
+ program_name=elinks
+ ;;
+ g)
+ program_name=git
+ ;;
+ m)
+ program_name=mutt
+ ;;
+ v)
+ program_name=vim
+ ;;
+ esac
+
+ # Add an exclamation mark at the beginning if running with sudo or if
+ # running zsh as root.
+ if [[ -n $program_sudo || $UID -eq 0 ]]; then
+ program_name=!$program_name
+ fi
+
+ # Add an at mark at the beginning if running through ssh on a
+ # different computer.
+ if [[ -n $SSH_CONNECTION ]]; then
+ program_name="@$program_name"
+
+ # If screen is running in SSH then display "@:hostname" as title
+ # in the term/outer screen.
+ if [[ $program_name == @screen || $program_name == @tmux ]]; then
+ program_name="@:${HOST//.*/}"
+ # Use "@:!hostname" for root screens.
+ elif [[ $program_name == @!screen || $program_name == @!tmux ]]; then
+ program_name="@:!${HOST//.*/}"
+ fi
+ fi
+
+ # Set the window name to the currently running program.
+ zshrc_window_title "$program_name"
+
+ # Tell precmd() to reset the window name when the program stops.
+ zshrc_window_reset=yes
+ }
+
+ zshrc_window_precmd() {
+ # Abort if no window name reset is necessary.
+ [[ -z $zshrc_window_reset ]] && return
+
+ # Reset the window name to 'zsh'.
+ local name=zsh
+ # If the function was called with an argument then reset the window
+ # name to '.zsh' (used by clear alias).
+ if [[ -n $1 ]]; then
+ name=.zsh
+ fi
+
+ # Prepend prefixes like in window_preexec().
+ if [[ $UID -eq 0 ]]; then
+ name="!$name"
+ fi
+ if [[ -n $SSH_CONNECTION ]]; then
+ name="@$name"
+ fi
+ zshrc_window_title $name
+
+ # Just reset the name, so no screen reset necessary for the moment.
+ zshrc_window_reset=
+ }
+
+ # Sets the window title. Works with GNU screen, tmux (which uses screen as
+ # TERM), xterm and rxvt. (V) escapes all non-printable characters, thanks
+ # to Mikachu in #zsh on Freenode (2010-08-07 17:09 CEST).
+ if [[ $TERM == screen* ]]; then
+ zshrc_window_title() {
+ print -n "\ek${(V)1}\e\\"
+ }
+ elif [[ $TERM == xterm* || $TERM == rxvt* ]]; then
+ zshrc_window_title() {
+ print -n "\e]2;${(V)1}\e\\"
+ }
+ else
+ # Fallback if another TERM is used.
+ zshrc_window_title() { }
+ fi
+
+ # Add the preexec() and precmd() hooks.
+ preexec_functions+=(zshrc_window_preexec)
+ precmd_functions+=(zshrc_window_precmd)
+else
+ # Fallback if another TERM is used, necessary to run screen (see below in
+ # "RUN COMMANDS").
+ zshrc_window_preexec() { }
+fi
+
+
+# COMPLETION SETTINGS
+
+# Load the complist module which provides additional features to completion
+# lists (coloring, scrolling).
+zmodload zsh/complist
+# Use new completion system, store dumpfile in ~/.zsh/cache to prevent
+# cluttering of ~/. $fpath must be set before calling this. Thanks to Adlai in
+# #zsh on Freenode (2009-08-07 21:05 CEST) for reminding me of the $fpath
+# problem.
+autoload -Uz compinit && compinit -d ~/.zsh/cache/zcompdump
+
+# Use cache to speed up some slow completions (dpkg, perl modules, etc.).
+zstyle ':completion:*' use-cache yes