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

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

Apparently there’s a Vi gang sign

(image taken from Kandinsky’s photostream)

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.

Tagged ,

2 thoughts on “How to set the filetype in Vim when the extension doesn’t match

  1. Chris Read says:

    I dig the gang sign…I too an a follower of the vi way. I once heard someone describe emacs as a useful operating system that’s greatest missing feature is a decent text editor…

  2. floehopper says:

    Recently I’ve been using vim more and more – specifically for remote pair programming using a multi-user GNU screen session. I’m increasingly impressed with the features I keep finding :-)

Comments are closed.


Get every new post delivered to your Inbox.

Join 3,109 other followers

%d bloggers like this: