This goes into your .bashrc
__prompt_command() { # global variables to cache bits of information to detect changes # __last_col - Stores the last width of the terminal # __last_git - Stores the last output of __git_ps1 # __last_pwd - Stores the last working directory # Compute status code and prompt colour local res="${?}" # Must be first! local clr="" if [ "${res}" -eq 0 ] then __ls="" __lc=2 else __ls=${res} __lc=3 fi # Compute directory name to be no wider than terminal; ellipsis in the middle. local git tag dir width have need left right type -t __git_ps1 >/dev/null && git="$(__git_ps1)" # Don't bother updating if the width, git status, or PWD has not changed. if [ "${__last_col}" != "${COLUMNS}" -o "${__last_git}" != "${git}" -o "${__last_pwd}" != "${PWD}" ] then # Something changed! Cache the values and recompute the prompt data __last_col="${COLUMNS}" __last_git="${git}" __last_pwd="${PWD}" tag="${FLAG:+(########)}${USER}@${HOSTNAME%%.*} " dir="${PWD}" # Replace homedir with ~ to save space [ "${dir##${HOME}}" == "${dir}" ] || dir="~${dir##${HOME}}" width="$(printf "%s%s" "${tag}" "${git}" | wc -c)" have="$(( ${COLUMNS:-80} - ${width} ))" need="$(echo "${dir}" | wc -c)" # If there is enough space to show something useful, but not enough to show # everything, then show the beginning and end, with ellipsis in the middle. if [ "${have}" -ge 15 -a "${have}" -lt "${need}" ] then have=$(( ${have} - 3 )) half=$(( ${have} / 2 )) left="${half}" right="$(( ${need} - ${half} ))" [ $(( ${have} % 2 )) -gt 0 ] && left="$(( ${left} + 1 ))" dir="$(echo "${dir}" | cut -c1-${left})...$(echo "${dir}" | cut -c${right}-${need})" fi __ld="${dir}" __lg="${git}" fi # Set xterm title echo -ne "\033]0;${HOSTNAME%%.*}(${USER})\007" } PROMPT_COMMAND=__prompt_command PS1="${FLAG+(${FLAG})}"'\[\033[1;32m\]\u@\h \[\033[1;36m\]${__ld}\[\033[1;35m\]${__lg}\[\033[00m\]\n\[\033[1;31m\]${__ls}\[\033[1;3${__lc}m\]\$\[\033[00m\] '