Every Flavour Beans

“The time has come…to talk of many [technologies].” –Lewis Carroll(’The Walrus and the Carpenter’)
Development Tools. Web Frameworks. GNU/Linux. Nokia N800. Video Encoding.

September 22, 2006

Develop Ruby Applications Using Vim 7.0 Editor

Filed under: GNU/Linux, Ruby/Rails — tabrez @ 11:01 pm

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:
  1. class Greeting
  2.   def say(name)
  3.     puts "Hello, #{name}"
  4.   end
  5.   def shout(name)
  6.     puts "HELLOOOOOOO, #{name}"
  7.   def
  8. end
  9.  
  10. greet = Greeting.new
  11. greet.say("Marc")

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.


If you want to receive future posts by email, enter your email address here:

Related Posts:

  • Develop Ruby Applications Using SciTE Editor
  • Develop Ruby Applications Using JEdit Editor
  • Programming in Ruby Using SciTE - Windows Issues and Other Tips
  • Develop Ruby Applications Using Komodo IDE
  • Develop Ruby Applications Using Eclipse IDE
  • Setting Up Rails Development Environment on Ubuntu GNU/Linux
  • Installing Ruby on GNU/Linux(Gentoo, (K)Ubuntu, Fedora, SUSE) And MS Windows

  • 13 Comments »

    1. Develop Ruby Applications Using Vim 7.0...

      "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 multi...

      Quote

      Trackback by Anonymous — September 24, 2006 @ 11:40 pm

    2. Should

      --ebable-rubyinterp

      be

      --enable-rubyinterp

      Thanks for the sound articles.

      Don

      Quote

      Comment by Don — September 25, 2006 @ 8:46 pm

    3. tabrez on September 25, 2006 at 9:03 pm said:

      Don on September 25, 2006 at 8:46 pm said:

      Should

      --ebable-rubyinterp

      be

      --enable-rubyinterp

      Thanks for the sound articles.

      Don

      Oh yeah, of course. Thanks for pointing out the typo, it's corrected now.

      Quote

      Comment by tabrez — September 25, 2006 @ 9:03 pm

    4. Hello,

      I'm having trouble using the "configure" command.

      [seanlerner]$ ./configure --prefix=$HOME/vim7 --enable-rubyinterp
      -bash: ./configure: No such file or directory

      Do I need to install "configure"?

      I've tried searching for help on "configure", but the word is used so much I can't find a particular Unix application/command called configure.

      Thanks,

      Sean

      Quote

      Comment by Sean Lerner — October 10, 2006 @ 9:43 pm

    5. Sean Lerner,

      You don't have to install anything: just first change to the directory of the extracted vim folder and then run the './configure' command.

      sh# tar xvjf vim-7.0.tar.bz2
      sh# cd vim70
      sh# mkdir $HOME/vim7
      sh# ./configure --prefix=$HOME/vim7 --enable-rubyinterp
      sh# make && make install

      The step to 'cd' into vim70 was missing from the post; I have added it now.

      Quote

      Comment by tabrez — October 11, 2006 @ 3:50 pm

    6. Hi Tabrez,

      Thanks--that worked.

      I've run into a new problem now. When I run:

      make && make install
      ...

      ...
      objects/os_unix.o objects/pathdef.o objects/popupmnu.o objects/quickfix.o objects/regexp.o objects/screen.o objects/search.o objects/spell.o objects/syntax.o objects/tag.o objects/term.o objects/ui.o objects/undo.o objects/window.o objects/if_ruby.o objects/netbeans.o objects/version.o -lncurses -lnsl -ldl -lruby-static -ldl -lcrypt -lm
      /usr/bin/ld: cannot find -lruby-static
      collect2: ld returned 1 exit status
      make[1]: *** [vim] Error 1
      make[1]: Leaving directory `/home/.grotton/seanlerner/vim70/src'
      make: *** [first] Error 2
      [vim70]$

      I'm using dreamhost, btw.

      Any suggestions? (even pointers to the appropriate manual is appreciated)

      Thanks,

      Sean

      Quote

      Comment by Sean Lerner — October 11, 2006 @ 9:09 pm

    7. Are you sure that Ruby is installed on the machine? If not, install the ruby package first and then repeat the above steps. Help on installing Ruby on various GNU/Linux distros is available here:

      http://beans.seartipy.com/category/ruby/

      If you don't want to install ruby support for Vim and want to use it for some other purpose, then you can drop the –enable-rubyinterp option when configuring its installation:

      sh# ./configure –prefix=$HOME/vim7
      sh# make && make install

      Quote

      Comment by tabrez — October 12, 2006 @ 9:24 pm

    8. Oh yes, I'm sure Ruby is installed on the machine:

      [log]$ ruby -v
      ruby 1.8.4 (2005-12-24) [i686-linux]
      [log]$

      And I'm purposely doing it to take advantage of the coloured ruby code.

      Right now I'm using dreamhost.com's VIM (version 6.3.82) and editing ruby, and it's all one colour. It would be really nice to have the extra ruby support available in VIM. Maybe another option is to add it to the current version of VIM that I'm using (that's installed system wide)?

      Any pointers appreciated,

      Sean

      Quote

      Comment by Sean Lerner — October 12, 2006 @ 9:32 pm

    9. I don't know of a way to add ruby support by making changes to the Vim configuration files. For full Ruby support, VIM has to be recompiled with the appropriate option.

      The error "cannot find -lruby-static" pretty clearly states that it couldn't find the ruby-static library file(search for libruby-static.a file in LIB directories). To confirm this fact, you can do a test install by dropping the –enable-rubyinterp option when running the configure script - Vim should get installed without any problems in this case. I don't know which distributions use the ruby-static library(neither ubuntu nor gentoo install them with the Ruby package), perhaps it is part of the Ruby package for RPM based distros(fedora, suse etc).
      Here is the only page where I was able to find an RPM package for ruby-static:
      http://rpm.rutgers.edu/rpm2php/?id=1781
      and it is for Solaris 2.9 UltraSPARC. The library might be a part of a different RPM package for other distributions.

      Quote

      Comment by tabrez — October 15, 2006 @ 9:30 pm

    10. Hello,

      I just thought I'd let you know that I decided to stop using dreamhost, as it was very slow, and I bought a virtual server package from vpslink.com and I've installed ubuntu on there. I used your procedure and I now have vim7 running successfully.

      Thanks,

      Sean

      Quote

      Comment by Sean Lerner — November 9, 2006 @ 1:45 am

    11. vpslink.com seems to be too cheap for VPS hosting. Nice find! It is not as easy to install our own applications on Dreamhost as it only gives user access on its servers, and root access would be more desirable for managing all the dependencies for applications like Vim.

      Quote

      Comment by tabrez — November 13, 2006 @ 1:44 pm

    12. The syntax highlighting in your screenshots uses dark colours that are designed to be seen against a white background, not a black one like is used in your screenshots.

      To fix this, use this command (and put it into your .vimrc)
      :set background=dark

      This tells vim to use bright colours for syntax highlighting.

      Quote

      Comment by Jimbo — December 7, 2006 @ 12:28 am

    13. To run the Ruby program currently edited,
      you can do this; '%' stands for the current filename:

      :rubyf %
      or
      :!ruby %
      (slightly different output from the above)

      Quote

      Comment by taki — April 27, 2007 @ 8:57 pm

    RSS feed for comments on this post. TrackBack URI

    Leave a comment

    Subscribe without commenting


    Copyright (c) 2006, 2007 Tabrez Iqbal.
    Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. Verbatim copying and distribution of this entire article is permitted in any medium without royalty provided this notice is preserved. A copy of the license is included in the section entitled "GNU Free Documentation License".


    Powered by WordPress
    This website is hosted by Dreamhost