Skip to content

Instantly share code, notes, and snippets.

@groks
Created June 9, 2011 12:10
Show Gist options
  • Save groks/1016614 to your computer and use it in GitHub Desktop.
Save groks/1016614 to your computer and use it in GitHub Desktop.
fancy bash prompt with colors, trimmed CWD and git status
# Shows exit status in red if previous command exited with
# an error code, the CWD in blue up to 30 chars or the 3 parent
# directories, and the git branch and status in grey, if in a
# git repo.
PS1="\n[\u@\h \[\e[1;31m\]\$(_exit_status)\[\e[0m\]\[\e[0;36m\]\$(_short_dir \w)\[\e[0m\]\[\e[1;30m\]\$(_git_status)\[\e[0m\]]\\$ "
# Return the exit status with padding if exit status != success
_exit_status() {
exit_status=$?
if [[ $exit_status != "0" ]]
then
echo "$exit_status "
fi
}
# Return only 3 parent dirs if CWD > 30 chars
_short_dir() {
cwd=${PWD/#$HOME/\~}
if [ ${#cwd} -gt 30 ]
then
cwd="...$(expr match $cwd '.*\(/.*/.*/.*\|.\{30\}\)$')"
else
echo "$cwd"
fi
}
# GIT prompt
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWSTASHSTATE=true
_git_status() {
declare -F __git_ps1 &>/dev/null && __git_ps1 ' (%s)'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment