Vim modelines


I edit a few different types of files with a few different indentation styles. Now, my favorite text editor is vim, and that supports using a config file to set your indentation style, but since I use a few of them, one config file won't work for me. Now, I'm sure you could also set a different style per filetype -- for example, python indents two spaces, C uses tabs displayed as four spaces. But that doesn't work, because different projects sometimes choose different styles, even though they use the same type of file.

Well, it turns out that you can store your preferred indentation setup (or any other vim commands) right there in the file you're working on. I've been aware of this for awhile, because I've seen the commands in files I've worked on. But I always forget the exact syntax of it. I usually end up looking for a file that has this, and then copying it out and tweaking it.

But now I know the magic word, so I could google it. These things are called "modelines". Here's a comprehensive howto on the vim wiki:
Modeline Magic

In short, here's how to do it (in a C file):

  1. // vim: ts=4:noet:sw=4

That's "tabstop = 4", "do not expand tabs (i.e. use tabs not spaces)", and "shift by four characters when using the >> command".

There. Now I know how to look this stuff up.

Your rating: None Average: 2 (5 votes)