7

I open a terminal and navigate to my project folder (ex: cd /proj/tickecting_app). After this I execute this command: rvm use ruby 1.9.x@gemset .

I want this command to run this every time I open the terminal.

Is there a way to run this automatically? And to be more specific I navigate to different projects in separate tabs.

For example:

  • First tab cd /proj/tickecting_app and run rvm use ruby 1.9.x@gemset.
  • Second tab cd /proj/rss_feed_app and run rvm use ruby 2.1.x@gemset.

I want to run different commands depending upon the project. Is this possible?

Braiam
  • 35,380
  • 25
  • 108
  • 167
Sridhar
  • 73
  • 4

3 Answers3

10

If you are using RVM then it is just a matter of putting a file named .rvmrc in your project directory with the following content:

rvm use ruby 1.9.x@gemset

For your second project it is the same, just change the rvm invocation.

l0b0
  • 50,672
  • 41
  • 197
  • 360
jabbrwcky
  • 216
  • 1
  • 3
6

Aliases can be of use to you. You can create alias for each specific directory and the command you need to run. So for the two example you have given it would be:

alias ticketapp='cd /proj/tickecting_app && rvm use ruby 1.9.x@gemset'

alias feedapp='cd /proj/rss_feed_app && rvm use ruby 2.1.x@gemset'

So next time you only need to give the alias(no need to give cd either) i.e ticketapp and feedapp in the above case.

To make your aliases permanent, simply:

vim ~/.bashrc

and give those alias commands as-is in the .bashrc file.

beginer
  • 2,648
  • 16
  • 18
3
[mikeserv@localhost ~]$ PS1=$PS1'$(
    [ ${last_hist=\!} -ne \! ] && {
    h=$(history 1)
    [ -n "${h#"${h##* cd *}"}" ] &&    
    . ./.dircmd >&2 2>/dev/null        
})${0##*["$0"$((last_hist=\!))]*}'
[mikeserv@localhost ~]$ cd .
[mikeserv@localhost ~]$ 
[mikeserv@localhost ~]$ echo

[mikeserv@localhost ~]$ cat <<\DIRCMD >.dircmd
echo this is my home directory
> DIRCMD
[mikeserv@localhost ~]$ 
[mikeserv@localhost ~]$ echo

[mikeserv@localhost ~]$ cd .
this is my home directory
[mikeserv@localhost ~]$

Something like that should work for a POSIX system - though I think only bash requires the \backslash before the !bang like that. bash also seems to update the history in a way that doesn't work with fc - which is portable - but does work with history. In other shells the history 1 bit should be exchangeable with fc -l -1 but bash does it a command behind, I guess. It was pretty frustrating.

Anyway, every time the prompt is drawn it defines the variable $last_hist as the history number for the last command. It does this in the match field on a parameter substitution that will always equal nothing so that doesn't show in the prompt.

The fc check just makes sure that the last history number actually incremented - as in, you didn't just hit enter or whatever. It also checks that the last command was a cd. If both are true it tries to . source a file in the current directory named .dircmd. If it exists it runs. If not, oh well.

mikeserv
  • 57,448
  • 9
  • 113
  • 229