I know that the geeks amongst you would like to believe that only one text editor program exists in this world. Depending on which of the two groups you fall into, its either Emacs or Vi. Its a cool life for such people: they don’t need to bother checking out the greatest and the latest of the development environments getting released in the market every day. Emacs and Vi always grow up to become the greatest editors soon after every technological innovation that takes place in the software world. They are stable, powerful, feature-rich, modular, customisable, extendable, ubiquitous and available in multiple flavours to suit the slightly different tastes even by the geek standards. The only downside of these two evergreen editors is the steep learning curve associated with them. But as with all great things, it more than pays off by the end.

Ruby Program in Vim 7.0 Editor

In this article, we’ll witness the support for the Ruby language present in the latest version of the Vim editor – Vim 7. If the latest version of Vim is not available on your preferred distribution(through its package manager, for example), then you can build it from the source, and install it in the user home directory if you don’t want to mess up with the file organization of your package manager. Here is the procedure to install Vim 7.0 from the sources in the user’s home directory. If you already have it installed, then skip to the next section.

  1. Download the source package for Vim 7.0 editor.
  2. Extract it in the home directory and ‘cd’ into it:
    sh# tar xvjf vim-7.0.tar.bz2
    sh# cd vim70
  3. Install using the usual method for building from the source:
    sh# mkdir $HOME/vim7
    sh# ./configure –prefix=$HOME/vim7 –enable-rubyinterp
    sh# make && make install

    Make sure that you run the above commands as an unprivileged user(non-root) so that the generated files get proper file ownerships. (Run the above commands as root and drop the ‘–prefix’ option to install it in system directories and make it available to all the users.)

  4. Now you can run the editor by running the command ‘./vim’ from the ~/vim7/bin directory. The procedure to run the vi editor can be simplified using one of the following techniques:

    • Add the ‘bin’ directory of Vim installation(~/vim70/bin in our case) to $PATH variable in the user’s profile(~/.bash_profile).
    • As root user, create links to all the executables present in ~/vim7/bin directory into the /usr/bin directory under different names(vim7, vimdiff7 etc so as not to conflict with the files from a possibly already existing Vim installation) and then run them from anywhere using these new names.
    • If you have installed it in the system directories, then of course you don’t have to worry about all these settings and can directly run the editor using the ‘vim’ command from any directory.

Configuring Vim 7.0 For Ruby Support

The first step that we had taken to get Ruby support in the Vim editor was to compile it using the ‘–enable-rubyinterp’ configure option. To get complete support for the Ruby programming language in the Vim editor, we need to add the following lines to the Vim configuration file .vimrc present in the home directory.
[Create it if it's not already there; an easy way to do this is to make a copy the sample vimrc file that comes with Vim 7.0, i.e.

sh# cp /home/user/vim7/share/vimrc_example.vim /home/user/.vimrc

Then add the following lines at the bottom of the .vimrc file]

set nocompatible
syntax on
filetype on
filetype indent on
filetype plugin on

Creating & Running Ruby Programs Using Vim 7.0

Now you are set to create your first Ruby program in the Vim editor.

sh# cd ~/vim7/bin
sh# mkdir -p ~/progs/ruby
sh# ./vim ~/progs/ruby/first.rb

Type the following program in the editor.
[ruby]
class Greeting
def say(name)
puts “Hello, #{name}”
end
def shout(name)
puts “HELLOOOOOOO, #{name}”
def
end

greet = Greeting.new
greet.say(“Marc”)
[/ruby]

The editor should now look like this:
Ruby Program in Vim 7.0 Editor

Apart from the neat syntax colouring that is visible in the picture, the Vim editor also does automatic code indentation and helps in code completion too. You can even run the programs from within the editor. Let us see how to access these features in Vim.

Add one more method(optional) to the ‘Greeting’ class that we had created earlier and name it something starting with ‘s’. I have added a function called ‘stash()’. Go to the end of the Ruby program and type ‘greet.s’ and stop – don’t complete the function name. If you press the key combination Ctrl-X Ctrl-O at this stage, Vim will pop-up a list of possible completions of the code that you have typed until now and lets you select one of those options. Below is a picture demonstrating the same.
Automatic Code Completion of Ruby Programs in Vim 7.0

To run the Ruby program from the Vim editor, let’s first introduce a small mistake in it: change the code from ‘Greeting.new’ to ‘Greeting.ne’ and issue the following command in the vi command mode(presss the escape key and type the following line):

:rubyf ~/progs/ruby/greet.rb

Run Ruby programs from Vim 7.0
Run Ruby programs from Vim 7.0

You should see the following error message displayed at the bottom of the Vim screen.
Ruby error messages in Vim 7.0

Now go back to the program and remove the error that we had introduced and re-execute the program. You should see the output of the program as shown below.
Ruby program output in Vim 7.0

For more help on the Ruby support in the Vim editor, issue the “:help ruby” command and scroll through the information displayed.

End Notes

The biggest advantage of developing Ruby programs using the Vim editor is that one gets all the powerful features of the vi editor which we have been using for years and have completely gotten used to. If you are searching for an easy to learn and easy to use programming editor, then perhaps you need to search elsewhere(see links below for some options) but if you are primarily a vi user, or don’t mind investing some time initially to get used to this powerful editor, then the Ruby support available in it should make it an ideal choice for developing Ruby programs.