A modeline is a specially formatted line in a text document or source file which can be interpreted by a text editor. It is often used to tell the editor what kind of file is being edited and how to configure indent settings. The feature originated in the ex line editor and was inherited by the vi visual editor and clones. A similar feature with incompatible syntax is present in emacs.

An ex modeline is in the form:

    ex: command arguments :

Arbitrary text can be included before and after the modeline, allowing it to be embedded in a comment. The trailing colon is important — without it, most ex implementations will not recognise the line. Usually only the first and last few lines in a document will be checked for modelines.

In theory, any ex command could be included in a modeline. However, for security reasons, generally only set is permitted.

Most full vi implementations recognise any valid ex modeline, and also recognise a vi: prefix. Vim will also recognise vim:, and allows a version number to be specified — for example, vim630: will only be handled by version 6.3 or later.

Putting this together, a c source file may include a line like:

    /* vim: set filetype=c shiftwidth=4 expandtab textwidth=80 : */

Or, equivalently but more tersely:

    /* vim: set ft=c sw=4 et tw=80 : */

This says that the file is a c source file using indents four positions wide, that tabs should be expanded to spaces and that the text should be wrapped on or before column 80.

Many Linux distributions have modelines disabled in the default editor configuration because of previous security concerns. Despite only allowing the set command, there were sneaky ways of executing arbitrary code via modelines from some Vim versions. One example was setting the statusline option to include an item which executed code; another was by passing malformed names to the filetype which could lead to a malicious file being executed as code. To re-enable the feature (and potentially leave yourself open to the Unix answer to Microsoft's macro virus feature), add a line saying set modeline to your ~/.vimrc.

Log in or registerto write something here or to contact authors.