devroom.io/content/posts/2010-04-25-ruby-version-and-gemset-in-your-bash-prompt-yes-sir.md

44 lines
1.3 KiB
Markdown
Raw Normal View History

2015-03-26 11:28:08 +00:00
+++
date = "2010-04-25"
title = "Ruby version and gemset in your Bash prompt? Yes sir!"
tags = ["Ruby", "BASH", "rvm", "gemset", "profile"]
slug = "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][1]. 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.
[1]: http://rvm.beginrescueend.com/
2017-03-20 15:35:19 +00:00
2015-03-26 11:28:08 +00:00
Firstly, add the following function to your `~/.profile`:
2017-03-20 15:35:19 +00:00
``` shell
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 "
}
```
2015-03-26 11:28:08 +00:00
Next, you can use this function in your prompt. Like this:
2017-03-20 15:35:19 +00:00
``` shell
export PS1="\$(rvm_version) \w \$(parse_git_branch) \$ "
```
2015-03-26 11:28:08 +00:00
The results? For standard ruby 1.8.7
2017-03-20 15:35:19 +00:00
``` text
@1.8.7 ~ $
```
2015-03-26 11:28:08 +00:00
Or with the `rails3` gemset enabled:
2017-03-20 15:35:19 +00:00
``` text
@1.8.7@rails3 ~ $
```
2015-03-26 11:28:08 +00:00
So, now you always know which ruby you're using! Happy coding!