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@collectiveidea.com

Comments

  1. chris@christopher-richards.net
    Chris
    August 02, 2011 at 11:12 AM

    This is most excellent my friend!

  2. thecatwasnot@gmail.com
    cole
    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 ∴

  3. truce.tringsteen@gmail.com
    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.

  4. rob@iblargz.com
    Rob
    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:

    https://github.com/RobertLowe/bash-it/blob/master/themes/rob/rob.theme.bash

    It looks like this:

    http://i.imgur.com/bUrAV.png

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

    Regards

  5. wojtek@wojtekmach.pl
    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:  http://bit.ly/pMRRAY

  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)

    https://gist.github.com/1043432

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

    It looks like this: http://i.imgur.com/YhCHq.png

  8. john.hinnegan@gmail.com
    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:
    http://rookery9.aviary.com.s3.amazonaws.com/9242500/9242608_c0b5.png

  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

  12. eisaks.andrejs@gmail.com
    coderxin
    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…

    https://github.com/wayneeseguin/rvm/blob/master/contrib/ps1_functions

  14. dgaytan6@gmail.com
    Daniel
    August 24, 2011 at 13:10 PM

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

    https://github.com/daniel-g/BranchOnCommandLine/blob/master/.git-config.bash

  15. seanliugm@gmail.com
    Sean
    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 git-prompt.sh instead git-completion.bash