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?
Asked
Active
Viewed 4.6k times
3 Answers
76
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.

Rick
- 15,484
- 5
- 25
- 29
-
2There 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](http://lwn.net/Articles/20249/) , but there are many more. – James Broadhead Apr 11 '11 at 07:12
-
9All 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 '14 at 17:40
47
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

Andrew_1510
- 12,258
- 9
- 51
- 52
-
2Expanding 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. https://vim.fandom.com/wiki/Modeline_magic – Michael Scheper Jul 19 '20 at 15:59
-
2Also 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 '21 at 11:43
-
-
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 06 '22 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

Zsolt Botykai
- 50,406
- 14
- 85
- 110
-
sorry Rick was faster with one minute :). For this reason I accepted his answer. +1 for links – kfl62 Oct 18 '10 at 11:33