Silence is Foo Mental notes on Ruby/Git/Rails/Flex

25Jul/103

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

About raf

Ruby/Rails/Flex Developer
Filed under: ruby Leave a comment
Comments (3) Trackbacks (0)
  1. Of course, a little reading makes you stumble into stuff like this:

    http://rvm.beginrescueend.com/workflow/prompt/

    So, all you need is LOVE. And this on your .profile:

    PS1=”[\h:\$(~/.rvm/bin/rvm-prompt)]\W \u\$ “

  2. You probably don’t have a RUBY_VERSION variable on 1.8.7 because you’re using the system ruby.

    Try installing rvm’s 1.8.7 and setting it as default

    rvm install 1.87
    rvm 1.8.7 –default

    Now, what I did was extract the ruby version and the gemset from $GEM_HOME, something like this:

    RUBY_WITH_GEMSET=$GEM_HOME
    RUBY_WITH_GEMSET=${RUBY_WITH_GEMSET%%/[ruby]*}
    export RUBY_WITH_GEMSET=${GEM_HOME:(${#RUBY_WITH_GEMSET} + 1)}
    export PS1=”[\h:${RUBY_WITH_GEMSET}]\W \u\$”

    Now I can see the ruby version and the gemset on the prompt, if you don’t care about the gemset, you can just use RUBY_VERSION on PS1.


Leave a comment


No trackbacks yet.