Freitag, 13. September 2013

Changing dinamically the terminal title in Mac using the rvm gemset name

I usually work with the Terminal on my Mac to run different commands, so I open different tabs, for example, one for voluntary, another for voluntary_classified_advertising and so on. Mathias suggested that it was a good idea to use a meaningful name as title of each terminal tab, so I can have the title "voluntary" if this terminal tab is in the directory "voluntary". To do that, you should open the Shell option in the Menu of Terminal, and select "Edit Title", and then change the Title.

However I thought: How I can make the title dinamically?
Well, when I change to the directory "voluntary", the gemspec is changed also. To do that I moved to my voluntary directory and executed:
rvm --create use --rvmrc ruby-1.9.3-p362@voluntary

So a  .rvmrc file is created, and it changes the gemset for using ruby-1.9.3-p362@voluntary each time you move to "voluntary" directory.

Then, I wrote the script ~/bin/get_gemset.sh:

#!/bin/sh

DEFAULT_GEMSET="/Users/mcberros/.rvm/gems/ruby-1.9.3-p362"
GEMSET=`rvm gemset name`

if [ "$GEMSET" == "$DEFAULT_GEMSET" ]; then
        echo 'default'
else
        echo $GEMSET

fi

You must execute:

chmod u+x get_gemset.sh

Also, I modified my .bash_profile to add the following lines:

export PATH=~/bin:$PATH
PROMPT_COMMAND='echo -ne "\033]0;`get_gemset.sh`"; echo -ne "\007"'

Important: This line has to be the last in your .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Then, reload your .bash_profile with the command:
source .bash_profile

If you leave the voluntary directory and move to other directory without a specific gemset, then your gemset will be "default" and this will be the title of your terminal tab.

By the way, this weekend I am in the Baruco conference (http://www.baruco.org/) in the beautiful city of Barcelona.

Nice weekend!!!




Keine Kommentare:

Kommentar veröffentlichen