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
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.
-
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 , but there are many more. Apr 11, 2011 at 7:12
-
12All 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."– BenJan 14, 2014 at 17:40
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
-
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. vim.fandom.com/wiki/Modeline_magic Jul 19, 2020 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.– user9645Jan 21, 2021 at 11:43 -
spaces are not required other than before vim I.E. <space>vim:sw=4:ts=4:et– AaronMJan 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.– AaronMJan 6, 2022 at 17:39
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
-
sorry Rick was faster with one minute :). For this reason I accepted his answer. +1 for links– kfl62Oct 18, 2010 at 11:33