Last updated

Ruby version and gemset in your Bash prompt? Yes sir!

RVM is an easy way to switch between different ruby implementations and gemsets. If you don’t know about it, go check it out. If you do know about, you’ll know how annoying it is never to know which ruby version and gemset you’re currently using. Here is a nice .profile hack that shows your current ruby version and optional gemset in your prompt.

Firstly, add the following function to your ~/.profile:

1function rvm_version {
2    local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
3    [ "$gemset" != "" ] && gemset="@$gemset"
4    local version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
5    [ "$version" != "" ] && version="@$version"
6    local full="$version$gemset"
7    [ "$full" != "" ] && echo "$full "
8}

Next, you can use this function in your prompt. Like this:

1export PS1="\$(rvm_version) \w \$(parse_git_branch) \$ "

The results? For standard ruby 1.8.7

1@1.8.7 ~ $

Or with the rails3 gemset enabled:

1@1.8.7@rails3 ~ $

So, now you always know which ruby you’re using! Happy coding!