Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the proper version of Ruby when executing through MacVim (!ruby)

Tags:

vim

ruby

macvim

rvm

Recently I saw Gary Bernhardt show off a vim shortcut he uses to execute Ruby code from within vim. The shortcut is

:map ,t :w\|:!ruby %<cr>.  

It seems that this method always execute's the System Ruby, which in my case is 1.8.7. I'm hesitant to upgrade this as I've heard that changing the System Ruby interpreter can cause some wonky issues. Is there anyway to get this command to use the version setup for the directory with RVM?

like image 548
Ryan Avatar asked Aug 04 '11 01:08

Ryan


2 Answers

The most direct way to specify a ruby is to give its full pathname in the :! command (instead of relying on whichever ruby is found first in the PATH directories):

  • To use the Ruby at /path/to/your/preferred/ruby:

    :!/path/to/your/preferred/ruby %
    
  • To use RVM-installed ruby-1.9.2-head:

    :!~/.rvm/bin/ruby-1.9.2-head %
    
  • To use RVM-installed ruby-1.9.2-head with your rails3-dev gemset:

    :!~/.rvm/bin/ruby-1.9.2-head@rails3-dev %
    

So your map command might end up like this:

:map ,t :w\|:!~/.rvm/bin/ruby-1.9.2-head@rails3-dev %<cr>

It is also usually possible to adjust the effective value of the PATH environment variable so that your desired ruby is the first one found, but it may not always be as simple as you might like. Specifically, the :! commands are processed by the shell configured by the shell Vim option (:set shell? to see its value). The configuration files for that shell may modify the PATH value that Vim gives the shell (compare Vim’s PATH (:echo $PATH) to the PATH that :! commands end up using (:!echo $PATH) to see if your shell’s configuration files might be adjusting the PATH).

You might try modifying the PATH that Vim and its children use like this:

:let $PATH = $HOME . '/.rvm/wrappers/ruby-1.9.2-head@rails3-dev:' . $PATH

You should check the effective PATH with :!echo $PATH and :!which ruby to find whether your shell is further modifying the PATH (maybe :set shell=/bin/sh if you have this problem).

Note: I have never seen this particular use of RVM’s wrapper directories, future versions of RVM may break it. The normal usage is to create a wrapper and invoke it directly out of ~/.rvm/bin (similar to the first part of this post) instead of putting ~/.rvm/wrapper/whatever in the PATH.

like image 107
Chris Johnsen Avatar answered Nov 12 '22 12:11

Chris Johnsen


In your map, instead of using simply ruby (which will be the first one in your $PATH) use a full path. Let's say, for example, that you want to use a MacPorts installed Ruby:

:map ,t :w\|:!/opt/local/ruby %<cr>.
like image 1
sidyll Avatar answered Nov 12 '22 14:11

sidyll