Smokes your problems, coughs fresh air.

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

  1. Julien

    Thanks for writing down in words what a modeline was. Useful when you don’t know ๐Ÿ˜‰

  2. Perry

    Heh, I just spent 10 minutes trying to remember what modelines are called *again* ๐Ÿ™‚

    Thanks for the post.

  3. Rowan Rodrik

    Haha ๐Ÿ˜€ Glad I could help! Apparently, I made up some good strings to be indexed by. ๐Ÿ˜‰

  4. Vince

    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. Roman

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

© 2024 BigSmoke

Theme by Anders NorenUp ↑