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
Zach Moazeni


Post a Comment

(optional — will be included as a link.)
  1. This is most excellent my friend!

    August 02, 2011 at 11:12 AM
  2. or, RVM will do the whole thing for you:
    [[ -s "$HOME/.rvm/contrib/ps1_functions" ]] && source "$HOME/.rvm/contrib/ps1_functions" ps1_set --prompt ∴

    August 02, 2011 at 11:18 AM
  3. 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.

    truce tringsteen
    truce tringsteen
    August 02, 2011 at 12:49 PM
  4. 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 ]


    August 02, 2011 at 13:28 PM
  5. 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:

    Wojtek Mach
    Wojtek Mach
    August 02, 2011 at 14:05 PM
  6. 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.

    August 02, 2011 at 14:52 PM
  7. 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:

    August 02, 2011 at 16:18 PM
  8. 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:

    john Hinnegan
    john Hinnegan
    August 02, 2011 at 19:20 PM
  9. 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

    August 03, 2011 at 2:49 AM
  10. Wow, amazing, thank you very much Zack and everybody

    August 04, 2011 at 1:01 AM
  11. Wow, amazing, thank you very much Zack and everybody

    August 04, 2011 at 1:07 AM
  12. Works like a magic! Thank you!

    August 04, 2011 at 4:00 AM
  13. Wayne E. Seguin has created something similar…

    August 04, 2011 at 22:30 PM
  14. This is what I use to show the git branch in my console

    August 24, 2011 at 13:10 PM
  15. Cool! But just curious why the ruby version didn’t show up in my machine until I used ‘rvm use’ command.

    August 25, 2011 at 23:47 PM
  16. 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

    October 02, 2012 at 0:41 AM