Collective Idea

Collective Idea Logo

Zach Moazeni

Command Line Feedback from RVM and Git

By Zach Moazeni on August 02, 2011 in git and ruby

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
By Zach Moazeni on August 02, 2011 in git and ruby

16 Comments

  1. Chris

    Chris August 02, 2011

    This is most excellent my friend!

  2. cole

    cole August 02, 2011

    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

    truce tringsteen August 02, 2011

    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

    Rob August 02, 2011 http://iblargz.com

    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 Mach

    Wojtek Mach August 02, 2011

    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. Robert Evans

    Robert Evans August 02, 2011 http://www.codewranglers.org

    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. Oliver Ponder

    Oliver Ponder August 02, 2011 http://ponderdesign.nl

    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

    john Hinnegan August 02, 2011

    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. Seban

    Seban August 03, 2011 http://blog.sebastiannowak.com

    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. Ismael Marin

    Ismael Marin August 04, 2011 http://www.nebulaideas.com

    Wow, amazing, thank you very much Zack and everybody

  11. Ismael Marin

    Ismael Marin August 04, 2011 http://www.nebulaideas.com

    Wow, amazing, thank you very much Zack and everybody

  12. coderxin

    coderxin August 04, 2011

    Works like a magic! Thank you!

  13. Anthony Bouch

    Anthony Bouch August 04, 2011 http://www.58bits.com

    Wayne E. Seguin has created something similar…

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

  14. Daniel

    Daniel August 24, 2011

    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. Sean

    Sean August 25, 2011

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

  16. William Notowidagdo

    William Notowidagdo October 02, 2012 http://codeindsgn.com

    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

Post a Comment

Contact Us

Find us on Google Maps
Collective Idea
44 East 8th Street, Suite 410
Holland, Michigan 49423 USA 42.790334-86.105251

Follow us on the Interwebs

We are currently available for medium and long term projects. Please get in touch if we can be of service.