My Bash Prompt

January 16, 2014

I have a fair number of Ruby projects on my laptop at any one time, each running different versions of Ruby and each using their own gemset. Some have more than one git branch. I wanted a way to easily see which ruby version, RVM gemset, git branch, and current directory I am in at any time.

This bash prompt does that for me. One thing that’s different from a lot of prompts I see is that mine is intentionally two lines with the second being a simple ’$’. I like that this puts my next command as far left as possible regardless of the output of the prompt. And I’ve found that having a “full line” of prompt in a different color helps my eye break up the screen a bit better.

image

The relevant portions of my .bashrc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM

# If we're in a git repository return the branch name,
# otherwise nothing.  Include the '...' here so it isn't
# duplicated in the output.
function ps1_git {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo " … "${ref#refs/heads/}
}

ps1="\[\e[0;34m\]"                         # colored text
ps1=$ps1"\u@\h"                            # user@host
ps1=$ps1" … "
ps1=$ps1"\$(rvm-prompt v)\$(rvm-prompt g)" # 1.9.3@gemset
ps1=$ps1"\$(ps1_git)"                      # git branch
ps1=$ps1" … "
ps1=$ps1"\w"                               # relative working dir
ps1=$ps1"\\n"                              # new line
ps1=$ps1"\$"                               # $
ps1=$ps1" "                                # space
ps1=$ps1"\[\e[0m\]"                        # reset color
export PS1=$ps1