8

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?

sidyll
  • 57,726
  • 14
  • 108
  • 151
Ryan
  • 83
  • 3

2 Answers2

5

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.

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
  • Thanks, this post helped a lot. One question, if I were to alter the PATH so that the RVM version of Ruby were first, would that affect anything with the OS looking for the version it is expecting? In other words, is it safe to assume that the OS would specify the path to Ruby it expects as opposed to just issuing the "ruby" command itself and getting back whatever is defined in the PATH? – Ryan Aug 04 '11 at 15:00
  • It is unlikely to affect any uses of Ruby by “the system”, but it is possible (to a limited extent). At most, it would only affect “system” Ruby programs started with the modified PATH and that have `#!/usr/bin/env ruby` as the first line, or are invoked as `ruby /path/to/program` (arguably a bug for any program that is sensitive to which Ruby installation is used). PATH, like any environment variable, is inherited from the parent process, so changing PATH in your instances of Vim will only affect its children (i.e. not processes started by *launchd*, *init*, *inetd*, *cron*, et cetera). – Chris Johnsen Aug 04 '11 at 23:39
1

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>.
sidyll
  • 57,726
  • 14
  • 108
  • 151