Compiling Vim With Ruby Integration On Snow Leopard
by jperras
One of my favourite plugins for Vim is Command-T:
an extremely fast, intuitive mechanism for opening files with a minimal number of keystrokes. It’s named “Command-T” because it is inspired by the “Go to File” window bound to Command-T in TextMate.
Sadly, the default installation of Vim on Snow Leopard does not have support for the ruby interpreter compiled in, which is a pre-requisite for using the plugin. Luckily, that’s easy enough to remedy, and in the process we’ll learn a thing or two about compiling your own custom Vim binary.
Let’s start off by getting the source code from the official Mercurial repository:
$ cd vim
(Note: You don’t need to use the Mercurial repository – there are mirror sources for Subversion, CVS, as well as good ol’ tarballs with patches.)
The default branch for the mercurial repository contains the code for Vim 7.2 at the time of this writing. There is also a vim73 branch available for those feeling a bit more adventurous and wishing to compile the beta release of the next version. For this article, we’ll be sticking to the stable 7.2 release in the default branch.
Now, let’s take a look at the possible configuration options:
There are quite a few, and I suggest that you take the time to read through them – most are quite self-explanatory. For Command-T, the one that we are interested in is the
option.
So let’s take a shot at the simplest installation for terminal-based Vim usage, one without the GUI interface and (Linux) mouse daemon support:
$ make
After the compilation process finishes (presumably with no errors), the first thing you’ll want to do is ensure that the binary you just built functions as expected:
# you should see a `+ruby` line entry
$ ./src/vim
If you see the +ruby entry in the –version output and the binary launches without any errors, rejoice in your own awesomeness. That’s all there is to it.
If, however, you see something similar to this:
Vim: Finished.
zsh: segmentation fault ./src/vim
you’ve probably fallen prey to a (currently) not very well documented issue: Vim 7.2 does not support the integration of Ruby 1.9.x on Snow Leopard.
This means that if you’ve used a package manager such as Homebrew, MacPorts or Fink (shudder) to install the latest version of Ruby, Vim will link to that latest version instead of the system default installation of ruby 1.8.7.
Let’s fix that.
We’re going to edit the src/auto/config.mk generated by configure that was run earlier. Note that if you re-run configure at a later time, your changes to config.mk will be lost.
Find the lines that look like this:
RUBY_SRC = if_ruby.c
RUBY_OBJ = objects/if_ruby.o
RUBY_PRO = if_ruby.pro
RUBY_CFLAGS = -I/usr/local/Cellar/ruby/1.9.1-p378/include/ruby-1.9.1 -I/usr/local/Cellar/ruby/1.9.1-p378/include/ruby-1.9.1/i386-darwin10.4.0 -DRUBY_VERSION=19
RUBY_LIBS = -lruby -lpthread -ldl -lobjc
(Note: Your specific paths and/or versions may differ depending on the package manager that you are using. The above paths are actually not important, however, since we actually want to reset them to the system defaults.)
and replace them with the following:
RUBY_SRC = if_ruby.c
RUBY_OBJ = objects/if_ruby.o
RUBY_PRO = if_ruby.pro
RUBY_CFLAGS = -I/System/Library/Frameworks/Ruby.framework/Versions/1.8 -I/usr/lib/ruby/1.8/universal-darwin10.0
RUBY_LIBS = -framework Ruby
Alright, let’s see if this worked.
Before we check the binary as we did before, let’s see if we linked to the correct ruby libraries:
src/vim:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
/usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/libruby.1.dylib (compatibility version 1.8.0, current version 1.8.7)
/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 44.0.0)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 550.29.0)
Looking good so far – the binary is linked to the framework version of Ruby that comes as a default on Snow Leopard.
Let’s do a version check:
# you should see a `+ruby` line entry
$ ./src/vim
And voilĂ : A custom-built Vim with ruby integration that will happily run the Command-T plugin.
All that’s left is to install it:
Assuming your PATH is setup to correctly find the new Vim binary, you should be all set.
Comments
Any way to get this to work on macvim? Do I have to recompile it?
MacVim actually uses it’s own pre-built binary of Vim and not the one that is found on your system. You can find it in:
If you run otool -L on that Vim binary, you’ll see that it is pre-linked with the system default libs. So, as long as you haven’t nuked the version of ruby that shipped with the OS, you should be fine.
If you’re still having trouble with getting MacVim to integrate with ruby, you might want to try building your own binary – the steps are almost identical.
You might also find this of note: http://www.mail-archive.com/vim_mac@googlegroups.com/msg00601.html.
Haha well I nuked the system ruby as it was causing trouble with hashbangs, so that’s probably my problem. I am using RVM so I switched to 1.8.7, and make seemed to link against that no problem. Thanks for the help!
I”ve followed your instructions to the T and i still get the SEGV. I am using the vim 7.3. It runs fine without ruby, but SEGV’s with ruby.
The new Macvim also is giving the same error.
I suppose i should dial back to 7.2 although i love some of the 7.3 features.
Ok. Ignore last post. I had a load of ruby macports also in addition to RVM. RVM’s 1.8.7 did not work.
So i unsinstalled all the macports, and then did “rvm use system” which picked up the original system ruby. Now Vim compiled fine and command-t loads (this is vim 7.3).
Thanks a lot for your blog post!
Hey you also might want to add –with-features=big to your configuration so that features like +multi_byte otherwise unicode is not supported
@Sonny: I try to refrain from specifying the ‘big’ or ‘huge’ feature set, since they add a lot of stuff that most people don’t need and can usually be obtained by specifying the appropriate configuration argument. For example, you can enable multibyte with the
option.
Thanks so much, you would not believe the amount of time I’ve burnt on this.
hey, thanks a lot for the guide. after running
./src/vim –version | grep ruby
i get the appropriate response, but after running
sudo make install
the vim version and config remains unchanged.
@Matt: Check to ensure that your $PATH environment variable is set to pick up the new vim binary, where ever the installation process put it.
Just wanted to point out that I got tripped up on the “gotcha” with ruby 1.9.2, as my compiled binary did “launch without any errors”, I didn’t think I had to take those extra steps to compile against 1.8.x. Of course, trying to run Command-T threw the error mentioned in your post. You might want to make it a bit clearer that one should try to actually execute Command-T from the newly compiled binary rather than *just* launching the newly compiled vim… Your workaround steps worked flawlessly, I just didn’t know initially that they applied to me.
Otherwise, thanks so much for the info!
Thanks a lot for this post. I was struggling to get all the layers to cooperate (between brew, rvm, etc) and this made it clear which pieces needed the prodding. Thanks!
this was helpful on 10.6 using `rvm use system`, thanks
I’m a little late to the party here, but I recently swapped out Command T for ctrlp (https://github.com/kien/ctrlp.vim). It’s essentially Command T but written in Vimscript so you don’t have to mess with baking in Ruby support into Vim.
Maybe you’ll enjoy it, too!