90

In some files I can see a commented line, usually the last, with vim settings. Does vim read these settings? If it does, are any limitations of what kind of settings man can put there?

3 Answers 3

77

They're called modelines and while I'm not sure the extent you can go with them, here's a link to the vimtips wiki with some examples:

http://vim.wikia.com/wiki/Modeline_magic

help modeline

from within vim to check out the official docs.

2
  • 2
    There are security problems with modelines; that's why they're disabled by default. It would probably be better for you to configure per-project .vimrc files. For example , but there are many more. Apr 11, 2011 at 7:12
  • 12
    All known security problems with modelines have been fixed for years now. Any "dangerous" options, like 'makeprg' or 'shell' cannot be set from a modeline. The help is full of options that say at the end "cannot be set in a modeline, for security reasons."
    – Ben
    Jan 14, 2014 at 17:40
49

It's this line of code:

[other chars]<spaces>vim:<spaces>settings

Put it in the first or last few lines of the file, note it needs < spaces >. For example:

# vim: tabstop=2 shiftwidth=2 expandtab

In short version:

# vim: ts=2 sw=2 et

Put one of the above line in the file, in top or bottom lines, done. For more information, use vim help:

:h modeline
4
  • 2
    Expanding on this helpful summary: These lines need to be 'near' the top or bottom of the file. By default, 'near' means it has to be one of the first or last 5 lines of the file. vim.fandom.com/wiki/Modeline_magic Jul 19, 2020 at 15:59
  • 2
    Also note everything after vim: has to be valid VIM modeline syntax, e.g. you can't use an end-comment delimiter on the same line as the modeline, e.g. /* vim: ts=2 sw=2 */ will not work.
    – user9645
    Jan 21, 2021 at 11:43
  • spaces are not required other than before vim I.E. <space>vim:sw=4:ts=4:et
    – AaronM
    Jan 6, 2022 at 17:30
  • This worked in a JSON document on one line 1515 " vim set sw=4 ts=4 et:": {"type": "keyword"} Using the short syntax it worked but generated an error. With the short syntax nothing can follow on the line.
    – AaronM
    Jan 6, 2022 at 17:39
10

You can check out in the online manual: http://vimdoc.sourceforge.net/htmldoc/options.html#modeline

And this faq item also refers to it: http://vimdoc.sourceforge.net/htmldoc/vimfaq.html#19.5

1
  • sorry Rick was faster with one minute :). For this reason I accepted his answer. +1 for links
    – kfl62
    Oct 18, 2010 at 11:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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