The Myth of the Muttering Madman is a project in self-realization.

Saturday, June 24, 2006

Vim for Windows Programmers

I've been oscillating between different editors lately. At home I tended to use Vim for my programming tasks. At work I used a combination of Notepad++, XEmacs, Vim, and IDEs like Visual Studio 2005 for .NET work. I've finally had a gutful and have decided to use Vim as my primary editor (especially for programming tasks).

I think Vim is a really great editor on any platform. It certainly feels very natural on *nixs and there is a Vim port for Mac OS X. This tutorial however is targetted at the total Vim beginner who is interested in giving Vim a try on Windows. It explains how to install and configure Vim (for Windows XP), how to use some of its basic features and how to get further information.

Installation

  1. Download a Vim from www.vim.org. I suggest getting Vim 7.0 as this is currently the latest version. The self-installing executable is available here.

  2. Run the installer

  3. Do a custom install

  4. Click "Create .bat files for command line use"

  5. Make whatever other modifications you like to the installer configuration

  6. Hit the magic button!

Configuring Vim (_vimrc and _gvimrc)

Vim is a very powerful editor. As such it has a dizzying array of options which can be customised. In fact Vim can even be scripted, and this allows you to extend it in very cool ways. This section will describe some of the most basic customisations that you can make to setup the editor as you see fit.

All configuration for gVim is kept in a single file, _gvimrc. This file will probably have been created in your HOME directory. For me on Windows XP, this is C:\Documents and Settings\snarkyboojum. If you can't find a _gvimrc file there create one. _gvimrc is where you configure the graphical version of Vim (as opposed to the terminal version which you can run in a Command prompt - try typing "vim" at the command prompt and you'll see what I mean). The other configuration file you should be interested in is _vimrc. This file configures the terminal version of vim. Many options specified in one can be specified in the other. There are a few options that are only relevant for gVim, but the configurations I'll specify below are interchangable between both Vims.

_gvimrc configuration

The first thing you want in your _gvimrc file is:

set nocompatible

This improves the usefulness of (g)Vim by not trying to make it more compatible with Vi. There are subtle differences between Vi and Vim for a reason. If you want Vim to behave more closely to Vi you'll want to set compatible.

Without going into a rant about tabs vs. spaces I really do prefer expanding my tabs to spaces. I tend to use 4 spaces to represent a tab and I like for my code to autoindent while I'm writing it and have this indented by 4 spaces aswell. To set this up add the following to your _gvimrc file:

set autoindent
" replace a tab with 4 space characters
set tabstop=4
" configure autoindent to indent using 4 spaces aswell
set shiftwidth=4
set expandtab

Every programmer I've met or know loves syntax highlighting. I know I do. To turn this on by default in Vim add the following to your _gvimrc file

syntax on

In Vi, the backspace key didn't allow you to backspace over newlines amongst other things. This will quickly drive you crazy if you don't "fix" that. To make backspace work as you would expect add:

set backspace=indent,eol,start

To highlight search matches when performing a search and to enable incremental search where potential matches are found as you type the search pattern add

set hlsearch
set incsearch

Fonts are an important element of text editing. Programming fonts are usually different to standard word processing fonts in that they clearly distinguish between such characters as 0 and O. I like a proggy_fonts font called "Proggy Clean". To change the font used by default in gVim, add the following:

set guifont=ProggyCleanTT:h12:cANSI

where ProggyCleanTT is the name of the font, h12 is the height of the font in points (this can be a floating point number), and cANSI is the character set used. You can specify more than one font here in case a font isn't found. For fonts with spaces in the name just use the \ character to escape the space in the name. E.g. Andale\ Mono. On Windows a _ character can be used to replace spaces in font names too.

I like the mouse to hide when I'm typing text so I also add the following

set mousehide

It is possible to define the size of the gVim window when Vim starts aswell. This option is controlled by specifying the number of rows and columns to use. E.g.

" open a long window
set lines=55

So after all that, you should have a better idea on how customizable Vim is. This isn't even stratching the surface of what Vim can do. A good way to find out how to improve your configuration is to visit www.vim.org and check the massive collection of tips they have there. Another good way to learn about this stuff is to find other people's configs. To find out what any given setting does just type :help <command> in Vim and you'll be taken straight to the relevant help section for that command e.g. :help softtabstop. For reference, here is what my _gvimrc looks like:

" Snarkyboojum's Vim configuration
" Last updated: 24/06/2006

set nocompatible

" turn syntax highlighting on and change tabs to 4 spaces
syntax on
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
" make the spaces feel like tabs when moving over them
set softtabstop=4

" turn the annoying visual bell off
set novb

" set up search behaviour
set showmatch
set incsearch
set hlsearch

" make command line two lines high
set ch=2
" hide the mouse when typing text
set mousehide
" make backspace behave the way I'd expect it to
set backspace=indent,eol,start
filetype plugin on

" setup font and window size
set guifont=ProggyCleanTT:h12:cANSI
set lines=55

" make it obvious when there are nasty tabs and trailing whitespace in a file
set list listchars=tab:»·,trail:·

Using Vim

Still being written

Further information and other resources

The Vim homepage. There is a wealth of information about Vim here. The tips and scripts sections are very very useful.
Vim documentation including links to a PDF version of Steve Oualline's Vim book.
Vim for Perl developers. Good intro to some interesting Vim stuff for the Perl geeks.
Seven habits of effective text editing by Bram Moolenaar (the creator of Vim)

No comments:

about me