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?

  • this question didn't used to be how to configure Ruby – mikeserv Sep 5 '14 at 12:51
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.

  • 5
    Thank you for trying to solve the OP's underlying problem. – Deer Hunter Sep 5 '14 at 7:18
  • @jabbrwcky outstanding....a separate badge should be given for that ;) – beginer Sep 5 '14 at 8:10
  • If I recall correctly, .rvmrc is deprecated. Use .ruby-gemset and .ruby-version instead. – Ajedi32 Sep 5 '14 at 14:55
  • @Ajedi32 For me using the new version instead of .rvmrc doesn't automatically switch to the projects gemset. Just found this out today. :/ – pattulus Dec 22 '14 at 15:43
  • @pattulus It does for me... – Ajedi32 Dec 22 '14 at 15:50
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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.