Customising my Bash Prompt

Bash Jul 20, 2015

Customising Bash Prompt

Update 26/07/2015: I now use variables to make the PS1 definition more readable.

Lately, I've been looking at my prompt feeling that it could be better. Even though it displayed useful information, I felt that I could make it more useful.

My new prompt is now:

BLUE="\[\e[1;34m\]"
RED="\[\e[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOUR="\[\033[0m\]"
WHITE="\[\e[1;37m\]"
RESET="\[$(tput sgr0)\]"
export PS1="$BLUE[\t] $RED\u$BOLD_WHITE@$RED\h$WHITE:\w$GREEN\$(__git_ps1 \" (%s) \")$WHITE\n\$ $RESET"

Time in bold blue:

\[\e[1;34m\][\t]

User@host in bold red with the @ in bold white:

\[\e[1;31m\]\u\[\e[1;37m\]@\[\e[1;31m\]\h\[\e[1;37m\]:

Git branch in bold green:

\[\e[1;32m\]\$(__git_ps1 \" (%s) \")

The actual prompt is then displayed in bold white on the next line after resetting the formatting.

\[$(tput setaf 7)\]\n\$ \[$(tput sgr0)\]"

Cheat Sheet

  • \[\e[color\] - start colouring
  • \[\e[m\]- end colouring
  • \t - current time in 24h HH:MM:SS format
  • \u - username.
  • \h - hostname
  • \w - current absolute path to working directory
  • \W - base of current working directory
  • \$ - the prompt character (eg. # for root, $ for regular users).
  • tput bold - bold text
  • tput rev - display inverse colours
  • tput sgr0 - reset everything
  • tput setaf {CODE}- foreground colour
  • tput setab {CODE}- background color

References

Tags