10

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

 4 years ago
source link: https://www.devroom.io/2010/04/25/ruby-version-and-gemset-in-your-bash-prompt-yes-sir/?utm_campaign=Feed%3A+ariejan+%28ariejan%7Cdevroom.io%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Posted: 2010-04-25 - Last updated: 2019-06-05

Tagged ruby bash rvm gemset profile

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:

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

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

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

The results? For standard ruby 1.8.7

@1.8.7 ~ $

Or with the rails3 gemset enabled:

@1.8.7@rails3 ~ $

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

My site is free of ads and trackers. I record privacy-respecting usage statistics with Fathom.

Was this post helpful to you? Why not ☕ Buy me a coffee

Copyright © 1999-2020 Ariejan de Vroom <[email protected]>

Live now; make now always the most precious time. Now will never come again.
    – Jean-Luc Picard


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK