ViM: Modelines

When working on a project with several developers, some coding style has to be agreed on. This will not necessarily correspond with individual point of view. A very common disagreement upon developers is the indentation of source code: How many spaces does a single level of indentation correspond to? Is it represented by a series of spaces of by a single tab stop?

ViM modelines are configuration options that are embedded in the file itself. These options are parsed and included upon loading of the file.

There are two options that control the parsing of modelines:

ViM modelines can be added to a file using the following format

[text]{white}vim:set {options}:[text]

Where [text] is any text or empty, {white} represents at least one whitespace.

A typical example is a enforcing a policy for indenting source code: /* vim:set tabstop=3 shiftwidth=3: */

ViM also supports versioned modelines to restrict configuration options to the specified versions of ViM. To use this feature the vim: tag will have to be substituted with one of the following:

Where ver can be calculated from the desired version of ViM by (major version)*100 + (minor version).

Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.