Fresh Blurbs

Developer's Ultimate Bash Prompt

Default bash prompt is limited. Good news is that it's highly customizable. Personally I have two requirements to bash prompt:

  • I want two levels of folder hierarchy displayed (one is too confusing, full path is too long)
  • I use Git for all my code (when I have it my way), so I want to see git repo of the current folder, and whether anything is uncommitted, in prompt.

To achieve these, first you need to copy couple of scripts from Git source to your home. Here's how you do it:

git clone --depth 1
cp git/contrib/completion/git-completion.bash ~/
cp git/contrib/completion/ ~/
rm -rf git

after which you can paste following code in your .bash_profile:

source $HOME/

txtylw='\033[1;33m' # Yellow
txtwht='\e[0;37m' # White
fgcolor="\033[0m"    # unsets color to term's fg color
twolevelprompt='$([ "$PWD" != "${PWD%/*/*/*}" ] && echo "/...${PWD##${PWD%/*/*}}" || echo "$PWD")'
gitprompt='$(__git_ps1 "(%s)")'
export PS1="$USER - \[$txtwht\]$twolevelprompt\[$fgcolor\]$gitprompt\[$txtylw\] $arrow \[$fgcolor\]"

# This one isn't required for bash prompt and is a small bonus 
# which gives you git-specific auto-completion. Sometimes this can slow 
# things down. Feel free to disable it if you are experiencing latency.
source $HOME/

or, if you like having your path and prompt on separate lines, replace corresponding line in the configuration with the following:

txtcyn='\e[0;36m' # Cyan
export PS1="\[$txtcyn\]$USER@$HOSTNAME - $twolevelprompt\[$fgcolor\]$gitprompt\[$txtylw\] \n$arrow \[$fgcolor\]"

Lastly, as a bonus, I recommend setting up following colors for your terminal:

  • Background: #4B4498 with 45% transparency
  • Foreground: #B7FEDD
comments powered by Disqus