Note: only tested on Ubuntu Linux (jaunty), with ruby 1.9.2 and gvim 7.1.
gvim is can be customised whilst you are running the application, or the customisations can be preserved and used everytime your start gvim by editing the file ~/.gvimrc. You should create this file if it does not exist. Add the options as described below, or see the end for my complete .gvimrc
When Writing Ruby Code
To make gvim aware of ruby code when you open a ruby program, you need to tell it to be aware of the language it is editing, and it can tell that from the file extension, or filetype. The following options make sure gvim uses the appropriate plugin when we open a program:
filetype on
filetype plugin on
There are three code-specific functions I need from my text editor: syntax colouring, auto-indentation and auto-completion. The first two improve the visual appearance of the code, and the last speeds up the typing process.
Writing code in black-and-white is to be avoided at all costs. The extra visual cues you get from having def-end pairs one colour, do-end blocks a second, etc is invaluable. The colours soon become intuitive, and help in identifying syntax errors. Also, I find the colours make it easier to recognise blocks and recurring patterns when refactoring code. To turn on syntax highlighting, you need the option:
syntax on
Explore the options under the 'Edit/Colour Scheme' menu to find a colour scheme you like, then add the option (replace 'slate' with your favourite scheme name):
colors slate
The structure of a Ruby program is naturally captured with indentation. For me, the 'right' level of indent is two spaces per level. I also do not like tab characters, and prefer them expanded into spaces. The following options turn on the automatic indentation, set the size of indentation to two, and convert all tab commands:
filetype indent on
set tabstop=2
set shiftwidth=2
set expandtab
Not an option as such, but something worth knowing. When you are entering text, use CTRL-P to auto-complete your text. gvim will complete the word you are entering, or present a list of available choices. You can move through the list either using CTRL-P repeatedly, or using the cursor keys to move up and down and 'enter' to select the completion. For example, if I were writing this text in gvim I could type 'comp<ctrl-p>' and expect to press 'enter' to complete the word 'completion'. After a little while, this becomes second nature, and saves on typing. In addition, it saves a lot of mistakes, as variables or method names completed through auto-completion should agree with each other!
Helpful Editor Functions
Other functions that are helpful include the ability to 'fold' long definitions. I don't use this too much, but it can be helpful in large classes whose code is mostly static. To fold a long definition:
- Put the cursor on the first line of the definition
- Type 'zf'
- Click with the mouse on the last line of the definition
To expand a folded definition, type 'za'.
Another useful function is the ability to highlight and search for a particular name or variable. The command 'gd' will highlight all occurrences of the name under the cursor, and 'n' 'p' will move you forwards and back through the list, as with the usual gvim search.
Ruby Tools
Documentation through ri
The 'ri' command-line tool allows access to the Ruby documentation, and can be a quick and easy way to check Class definitions or argument lists for methods in the standard library. The following mapping connects the Function key 'F3' to a call to the 'ri' command-line tool. After pressing <f3> the user must enter details of the Class or method name to be shown.
map <f3> :!ri

Executing Code from gVim
You can execute the code in the current file you are editing from within gVim. To do this, you would normally go into command mode (press ':') then enter '!ruby %' which executes ruby as a shell command. The '%' sign means to pass ruby the current filename. The output is then shown in the window.
You can make this a single key press by adding a key mapping terminated with <cr> to your .gvimrc file:
map <f1> :!ruby %<cr>
Final .gvimrc
My complete .gvimrc file is below. It contains some extra settings: incsearch so that searching is incremental, stepping to intermediate matches as you type; hlsearch to show all search results with a yellow highlight (use :noh to remove the highlighting); and scrolloff to encourage gvim to keep at least the given number of lines around the cursor when scrolling. nowrap and the call to guioptions stops gvim wrapping long lines of source code, and provides a handy horizontal scrollbar where needed. And yes, that is a key mapping to start up an instance of irb within the editor!
set nocompatible
syntax on
filetype on
filetype indent on
filetype plugin on
set nowrap
set guioptions+=b
set tabstop=2
set shiftwidth=2
set expandtab
set hlsearch
set scrolloff=2
set incsearch
map <f1> :!ruby %<cr>
map <f2> :!ruby -r debug %<cr>
map <f3> :!irb<cr>
map <f4> :!ri

