Feb 03

Friendlier shell prompts for version control systems

Including version control status in your Bash prompt

Like many developers, I'm using multiple version control systems these days - lots of Subversion, increasingly git and even some old RCS we haven't migrated away from yet. Here's handy little shell addition which includes info in the prompt, combining this git-specific tip with my older checks:

__vcs_name() {
 if [ -d .svn ]; then
 echo " [svn]";
 elif [ -d RCS ]; then
 echo " [RCS]";
 else
 echo "$(__git_ps1 ' [git %s]')"
 fi
}

PS1='\[\033]0;\u@\h:\w\007\]\u@\h:\w$(__vcs_name) $ '

if [ -d ~/.bash_completion.d ]; then
 for c in ~/.bash_completion.d/*; do
 . "$c"
 done
fi

Note that the last bit is needed to make the git part work - depending on your bash distribution you may not need to have the for loop to process ~/.bash_completion.d. In my case, using the Mac git distribution I simply created that directory and added a symlink to /usr/local/git/contrib/completion/git-completion.bash

The final result will look something like this:

chris@Enceladus:~ $ cd Development/pymacadmin.googlecode/
chris@Enceladus:~/Development/pymacadmin.googlecode [svn] $ cd ../pymacadmin
chris@Enceladus:~/Development/pymacadmin [git master] $