Sunday, February 8, 2015

Color Bash Prompt for Git on Linux/Mac

If you use a bash prompt often then it really helps to highlight the information that matters for you most. For my professional git usage I care about these pieces of information:
  • the current working directory
  • the name of the git branch (if the current directory is a git working copy)
  • whether or not there are uncommitted changes

The above prompt is produced with this one-liner:

Prompt Contents

Let's first dissect the parts of the prompt that define what it actually shows:
  • \w - this is the bash shortcut for current working directory. Simple.
  • $(git status -s 2>/dev/null | head -1 | sed 's/.*/ */') - this will print an asterisk if the current working directory is a git working copy and if it has uncommitted changes (this can be also achiewed with __git_ps1 and
    GIT_PS1_SHOWDIRTYSTATE but I prefer to highlight the asterisk with different color)
  • $(git rev-parse --abbrev-ref HEAD 2>/dev/null | sed 's/.*/ &/') - this will print the branch name with a space in front of it

Prompt Colors

The prompt uses ANSI escape sequences to change the colors. I decided to use tput to generate the sequence instead of putting it in directly. It is simply more readable with explicit capability names. Here setaf sets the foreground color and sgr0 resets all attributes. You can check the man page terminfo(5) if you need for more details.
Finally, it is necessary to mark the ANSI escape sequences as non-printable in order for bash to correctly figure out the actual length of the prompt. This is done by adding \[ and \] around the control characters.