:h modeline says the following:

There are two forms of modelines.  The first form:


   vi:noai:sw=3 ts=6 ~
   vim: tw=77 ~

The second form (this is compatible with some versions of Vi):

    [text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text]


   /* vim: set ai tw=75: */ ~
   /* Vim: set ai tw=75: */ ~

The first form appears to be specific to Vim, whilst the second form appears to have some compatibility between different spin-offs of Vi. I am writing coding standards for a package and trying to recommend one form over the other.

The first form is much simpler because it does not need the ending colon but you cannot add text after it, for example if I add the following to a Haskell file:

-- vim: ft=haskell noai --

I get an error!

Error detected while processing modelines:
line    1:
E518: Unknown option: --

So am inclined towards the second form. Yet, given the two versions:

  • Is the first from Vim (and NeoVim according to my tests) only?

  • What other spin-offs of Vi support the second form? Does the original Vi support it?

1 Answer 1


The original Vi does support modelines of the second form, however, modelines has to be set before the file is read. The manpage of ex says:

 modelines, ml  default: nomodelines
        If modelines is set, then the first 5 lines and  the  last  five
        lines  of  the file will be checked for ex command lines and the
        comands issued.  To be recognized as a command  line,  the  line
        must  have the string ex: or vi: in it.  This string may be any-
        where in the line and anything after the : is interpeted as edi-
        tor commands.  This option defaults to off because of unexpected
        behavior when editting files such as /etc/passwd.

The Changes file says:

Release 2/17/02
* POSIX.2: Backslash has no special meaning inside RE bracket expressions.
* Made modelines compatible to SVr4 ex: The option name is "modelines" or
  "ml" now; a space or tab preceeding "ex:" or "vi:" is not required.

This indicates that modelines support was present earlier, it was changed in 2002.

Sven Guckes' list of vi options says modelines was supported by SVR3.1 vi, but not which format. I'd wager it would be the second form.

As for other implementations, nvi's manpage says:

modelines, modeline [off]
  Read  the first and last few lines of each file for ex commands.
  This option will never be implemented.

(Emphasis varies depending on the age of the manpage.)

For Elvis:

modeline, ml (Boolean, global)
modelines, mls (Number, global)

The modeline option controls whether Elvis will look for modelines in each buffer after it has been loaded from a file. If modelines is true, then Elvis will search through the first and last modelines lines of the buffer for something that looks like "ex:commands:" or "vi:commands:" and if found, it executes the commands as an ex command line. (This behavior is implemented in the elvis.arf file.) This is typically used for changing tabstops and the like. The modeline option is false by default, and modelines is 5.

It looks like the first is only for Vim (and descendants).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.