Silence is Foo Mental notes on Ruby, Git, Rails and whatever geeky thing

25Jul/10Off

Setting your terminal title to include your current ruby version

Using RVM to switch ruby versions is really awesome, but I've found myself issuing ruby -v a lot of times because sometimes I have terminals with 1.8 and others with 1.9 and I need to know which version a given terminalĀ is running , so I ended putting the current ruby version in the terminal title.

In order to set the title of an xterm you have to use the following escape characters in your PS1 variable:

\e]2;YOUR_TITLE_HERE\a

My system have 1.8.7, I installed 1.9.2 with RVM, I don't know why but when I use 1.8.7 I don't have a RUBY_VERSION environment variable, only when I use 1.9.2, besides $rvm_major_version, $rvm_minor_version, $rvm_gemset_name, etc are only available when you installed the current ruby version using RVM, so I extracted the version with the 'cut' command, in your ~/.bash_profile or ~/.bashrc put the following:

function __ruby_version
{
   ruby -v | cut -d " " -f2
}

PS1="your_current_ps1_string \e]2;$(__ruby_version)\a"

UPDATE: I don't have RUBY_VERSION because I haven't installed 1.8.7 with RVM, besides RVM bundles a script calledĀ rvm-prompt, look the comment below. [Thanks, David Padilla]

PS1="your_current_ps1_string \e]2;$(~/.rvm/bin/rvm-prompt)\a"
PS1="your_current_ps1_string \e]2;$(rvm-prompt)\a" #this works for me as well

rvm-prompt returns de ruby version and gemset, if you don't want the latter:

$ rvm-prompt i v #ruby-1.9.2-rc2
PS1="your_current_ps1_string \e]2;$(rvm-prompt i v p)\a"

For it to work you must add your ~/.bash_profile or ~/.bashrc manually to your current terminal:

$ source ~/.bash_profile
$ source ~/.bashrc

...and the ruby version will show up in your title terminal.

If you want to avoid this hassle you can make the ruby version to show up in your prompt:

PS1="your_current_ps1_string $(rvm-prompt)"

Do you think your PS1 is getting longer and longer? Use a multi-line prompt:

line_1="[\u:\h \w] $(rvm-prompt)"

line_2="$ "

PS1="$line_1\n$line_2"

Ruby1.8_and_1.9

Filed under: ruby 3 Comments