Skip to content

VIM modelines for per-file configuration

The MediaWiki developers use tabs instead of spaces for indentation. This can be annoying for someone like me who has configured VIM to work with spaces by default—annoying because I don’t want to enter something like :set tabstop=4 noexpandtab shiftwidth=4 every time that I open a file. Adding different conditions for each project to my .vimrc is probably possible but not much fun.

The other minute, when I opened the sources of my Semantic Gallery extension for MediaWiki, I noticed that I had been mixing tabs and spaces again. So it was time to look up the VIM feature which allows you to put a configuration line in a comment at the bottom of a file. This quote is another perfect example of why it’s good practice to blog about such things. I remembered that the last time that I had wanted to do this, I could not find a useful search string at all and resorted to finding back an example in the code where I had first seen it and modifying that. Now, I thought I’d have to do the same, but from a quick googling I learned that I’m not the only one to use his blog as a memory extension. 🙂

Good. Hopefully, from now on, I’ll remember that this configuration-thingy-at-the-bottom-of-a-file is called a modeline, so that I can just enter :help modeline in VIM the next time that I forget where the colons have to go.

An example for when I do forget:

# vim:set ts=4 sw=4 noexpandtab:

This reminds me of my many searches for Heredoc syntax when I didn’t know that they were called Heredocs.

5 Comments ( Add comment / trackback )

  1. (permalink)
    Comment by Julien
    On April 12, 2009 at 18:10

    Thanks for writing down in words what a modeline was. Useful when you don’t know 😉

  2. (permalink)
    Comment by Perry
    On November 22, 2009 at 00:17

    Heh, I just spent 10 minutes trying to remember what modelines are called *again* 🙂

    Thanks for the post.

  3. (permalink)
    Comment by Rowan Rodrik
    On November 22, 2009 at 13:17

    Haha 😀 Glad I could help! Apparently, I made up some good strings to be indexed by. 😉

  4. (permalink)
    Comment by Vince
    On January 31, 2010 at 06:22

    Thanks. I couldn’t remember what it was called either.
    I searched for “vim per file config” with google and this page came up
    as the first one in list.

  5. (permalink)
    Comment by Roman
    On February 23, 2012 at 19:03

    Yep, I just searched for “vim per-file configuration” and your page came up third on the results.