How to set the filetype in Vim when the extension doesn't match

Over 10 years ago, I had a choice to make: Emacs or Vi. I started with Emacs in my first week as a systems administrator. It made Olof, the lead developer very happy. But it wasn't to be. Emacs was only installed on some of the Solaris boxes, and Vi was always reliably there. So I drank the Vi Kool-Aid.

I fully accept that Emacs is probably better. But I'm happy with Vim. It knows about most syntaxes known to mankind and the graphical client takes just under eight megabytes of memory. Even .NET developers are using Vim. Which makes me smile, as I never thought that 30 year old Unix editor would become fashionable amongst Windows developers.

Anyway, what I wanted to write about today was the Vimrc file. There are several configuration files that Vim will respond to. But the one I'd suggest you use is .vimrc (on Unix) or_vimrc (on Windows). Put it in your home directory. Mine looks something like this:

set number
set autoindent
syntax on
au BufRead,BufNewFile *.build set filetype=xml

What it does is:

  • displays with line numbers
  • continues indents automatically
  • syntax highlighting
  • and the last one, which tells you that .build files should use the XML syntax.

This one is the one that started this post. It's so I can edit NAnt files from Vim, and not set the syntax automatically. I don't know the history of why NAnt came to depart from Ant's useful habit of calling buildfiles 'build.xml', but now, I don't have to.

DevOps New Zealand