Installing Vim with Ruby support
2013. 09. 21.

Everybody seems to hate Vimscript.

As being one of the few who have written a considerable amount of Vimscript, I can say it's not that bad, especially considering its notoriety.

But obviously it's not great either, which is quite understandable, as it's not a general purpose, full-fledged scripting language like Ruby or Python. We sometimes forget that Vimscript is simply a tool for configuring a text editor, I mean, a text editor.

So, if you don't want to dive deep into this exotic, notorious Vimscript but still want to customize your Vim experience, or need some advanced features that Vimscript just can't provide, such as threading or network I/O, you will find external language support of Vim intriguing. Yes, you can control Vim with Ruby or Python script. Sounds great, right? However, I must mention that, you won't be able to go too far without a certain amount of knowledge in Vimscript. But worry about that later, let's give it a shot.

In this article I'm going to provide instructions to install Vim with Ruby support on various platforms.

Do I have it?

Before moving on to the instructions, let's first see if you're already running a Ruby-enabled Vim. Type in the following command in your Vim session.

:echo has('ruby')

If it shows 1, then you're in luck. Congratulations, you are ready to write Ruby script inside Vim. Additionally, you can check which version of Ruby is being used with the following command.

:ruby puts RUBY_VERSION

Installation On Mac OS X

As of now, the latest version of Mac OS X (Mountain Lion) is shipped with a Ruby-enabled Vim located at /usr/bin/vim. However, it's running an old version of Ruby, 1.8.7.

If you find it limiting, follow the instructions below, I'll show you the easiest way to install the latest version of Vim with proper Ruby support.

Installing Ruby with RVM

There are several ways to install Ruby. But in this article, I'll show you how you can install the latest Ruby with RVM, which may not be the best tool out there, but by far the easiest one to use.

Type in the following command in your shell, and follow the instructions given in the output. You may need to edit your shell configuration file.

curl -L | bash -s stable

The latest version of Ruby is 2.0.0, but unfortunately the current version of Vim seems to have some issues with it, so it's safer to install 1.9.3 instead.

rvm install 1.9.3

Installing Vim with Homebrew

Homebrew is a crucial package manager for OS X. If you're not yet using it, go install it with the following command.

ruby -e "$(curl -fsSL"

When it's done, install Vim with Ruby 1.9.3.

rvm use 1.9.3
brew install vim

If you run vim --version, you'll see that the Ruby you installed with RVM is linked to Vim.

Installation on Ubuntu

Simply install vim-nox with apt-get.

sudo apt-get install ruby rubygems vim-nox

(Reference: Installing vim with ruby support (+ruby))

Installation on Windows

  1. Download and install the official GVim
  2. Download a newer build of GVim executable from here and replace the existing one
  3. Download and install Ruby 1.9.3 using RubyInstaller. You must check Add Ruby executable to your PATH option.

What next?

I'm going to leave you with this simple example.

function! s:shuffle() range
ruby << RB
  first, last = %w[a:firstline a:lastline].map { |e| VIM::evaluate(e).to_i }
  (first..last).map { |l| $curbuf[l] }.shuffle.each_with_index do |line, i|
    $curbuf[first + i] = line
command! -range Shuffle <line1>,<line2>call s:shuffle()

Happy Vimming. And please be nice to Ruby.

» capture | close