Command Line Feedback from RVM and Git

If you’re like me, most of your day is working in Git and RVM. I am frequently typing git branch to remember what branch I’m on, and rvm gemdir to remember what ruby version and gemset I’m using. With all those wasted cycles, there has to be a better way. And there is.

RVM comes with a built in shell prompt, and with a little magic you can combine that with git information. Add the following to your ~/.bashrc:

export PS1="\[\033[01;34m\]\$(~/.rvm/bin/rvm-prompt) \[\033[01;32m\]\w\[\033[00;33m\]\$(__git_ps1 \" (%s)\") \[\033[01;36m\]\$\[\033[00m\] "

Then in the terminal as my daily workflow moves me between git branches and rvm versions (whether explicitly or by .rvmrc), the prompt will always keep me up to date with my context. (The command line uses colors that aren’t shown in the snippet below).

ruby-1.9.2-p180 ~ $ cd projects/harvested/code/
ruby-1.9.2-p180 ~/projects/harvested/code (master) $ git checkout test-branch
Switched to branch 'test-branch'
ruby-1.9.2-p180 ~/projects/harvested/code (test-branch) $ rvm use 1.8.7
Using /Users/zmoazeni/.rvm/gems/ruby-1.8.7-p334
ruby-1.8.7-p334 ~/projects/harvested/code (test-branch) $ rvm use 1.8.7@mygemset --create
Using /Users/zmoazeni/.rvm/gems/ruby-1.8.7-p334 with gemset mygemset
ruby-1.8.7-p334@mygemset ~/projects/harvested/code (test-branch) $ 

Try it out and see if you like it. It’s been a big sigh of relief for me. Kudos to Chris Gaffney for the inspiration and for figuring out the git portion.

If you have trouble with the Git and/or RVM portions, make sure you have bash-completion installed. Git should install by default with homebrew, but you will need to put something similar in your ~/.bashrc before the PS1 declaration:

[[ -r $rvm_path/scripts/completion ]] && . $rvm_path/scripts/completion # for RVM completion
if [ -f `brew --prefix`/etc/bash_completion.d/git-completion.bash ]; then source `brew --prefix`/etc/bash_completion.d/git-completion.bash; fi # for Git completion


    August 02, 2011 at 11:12 AM

    This is most excellent my friend!

    August 02, 2011 at 11:18 AM

    or, RVM will do the whole thing for you:
    \[\[ -s "$HOME/.rvm/contrib/ps1\_functions" \]\] && source "$HOME/.rvm/contrib/ps1\_functions" ps1\_set --prompt ∴

    truce tringsteen
    August 02, 2011 at 12:49 PM

    Very cool.  Note: on Ubuntu with rvm 1.6.20 I had no .rvm/bin/rvm-prompt, but using .rvm/scripts/rvm works just dandy.  Thanks for the tip.

    August 02, 2011 at 13:28 PM

    Dude, you should use bash-it or oh my zhell with a proper theme.

    For instance I run my shells with bash-it, I wrote my own theme:

    It looks like this:

    It shows me: [battery power] [ ruby version@gemset/branch [ dirty/clean ]] [ folder ] [ user ]


    Wojtek Mach
    August 02, 2011 at 14:05 PM

    Nice one with the RVM.
    BTW, as the prompt gets longer and longer (ex. while browsing gems) you might find my little utility, wdalias, useful:

  6. August 02, 2011 at 14:52 PM

    Thanks for the plug Rob on the bash-it framework. If you haven’t already, you should submit your theme to my repo for inclusion. :)

    Zach, I agree with Rob, you should try out either bash-it or oh-my-zsh. I’ve actually switched to zsh and have my own little framework setup for it - it’s great to have info already parsed for you.

  7. August 02, 2011 at 16:18 PM

    Hey guys,

    Inspired by a peepcode screencast, I have one where it shows you what folder you are in, the branch and the time since last commit (up to 2 hours)

    It’s quite messy, but it works and is pretty cool/handy to see.

    It looks like this:

    john Hinnegan
    August 02, 2011 at 19:20 PM

    Love it.  Thanks.  Have incorporated into my prompt (which is a hodgepodge of things clobbered together from around the net).  

    Here’s my new look:

  9. August 03, 2011 at 2:49 AM

    My workmate adds color at the end to see if there are some changes or it is clean state. Your prompt is very simmilar to mine, I drop Ruby patchlevel info

  10. August 04, 2011 at 1:01 AM

    Wow, amazing, thank you very much Zack and everybody

  11. August 04, 2011 at 1:07 AM

    Wow, amazing, thank you very much Zack and everybody

    August 04, 2011 at 4:00 AM

    Works like a magic! Thank you!

  13. August 04, 2011 at 22:30 PM

    Wayne E. Seguin has created something similar…

    August 24, 2011 at 13:10 PM

    This is what I use to show the git branch in my console

    August 25, 2011 at 23:47 PM

    Cool! But just curious why the ruby version didn’t show up in my machine until I used ‘rvm use’ command.

  16. October 02, 2012 at 0:41 AM

    In my case (brew installed git) I got this error

    -bash: __git_ps1: command not found

    I resolved this by source instead git-completion.bash