docwhat's avatardocwhat's blog

OS X Vim with Ruby crashes

On my OS X systems, Vim and MacVim has been crashing a lot. The main symptom is that anything using Omni completion, such as NeoComplCache, would try to use the built in Ruby and cause vim to die with Vim: Caught deadly signal SEGV.

It turns out that this is due to vim loading ruby modules/libraries not compatible with the system Ruby.

I have RVM installed on my system and what was happening is that:

  • RVM sets the GEM_PATH and GEM_HOME.
  • I had a version of Ruby 1.8.7 in RVM. This is the same version as (Mac)Vim and OS X has installed in /usr/.
  • I had binary (compiled) gems installed for the RVM 1.8.7 Ruby. These are files that end in .so.
  • So (Mac)Vim tries to do completion, tries loading a binary gem from the GEM_PATH, finds the RVM 1.8.7 version of the .so gem module, loads it, and crashes.

This isn’t surprising. My RVM doesn’t compile Ruby the same way Apple does. This is deliberate, because I want certain performance behaviors, etc.

Anyway, the solution is simple enough.

If you got your Vim and MacVim through HomeBrew (and really, you should), then just add this to your ~/.zshrc:

for i in /usr/local/opt/vim/bin/*(N) /usr/local/opt/macvim/bin/*(N); do
  i=$(basename $i)
  alias "${i}"="env -u GEM_PATH -u GEM_HOME command ${i}"
done

Homebrew installs a symlink for most kegs into /usr/local/opt/*package-name*, which makes it simple to find the list of all binaries in a keg. The (N) is a ZSH-ism that says it is okay for the * to return nothing. If you’re using bash, just remove the (N)

The upshot is that this unsets the GEM_PATH and GEM_HOME.

You may also want your EDITOR environment variable to include the env -u GEM_PATH -u GEM_HOME part as well:

export GIT_EDITOR="env -u GEM_PATH -u GEM_HOME vim +1"
export EDITOR="env -u GEM_PATH -u GEM_HOME vim"

Ciao!

Edit on GitHub