11

:help modeline is pretty specific about this: No other commands than "set" are supported, for security reasons (somebody might create a Trojan horse text file with modelines). And not all options can be set. For some options a flag is set, so that when it's used the |sandbox| is effective[...] Hint: If you would like to do something else than ...


11

You have a few choices. First one: You can write your own kind of modeline decoder, i.e. a plugin. This is a very similar answer to the one I gave to your other question. Indeed, we cannot add new vim options (that can be set with :set), but we can add new variables. And we cannot use the standard modelines to do more than setting vim options. Second ...


8

Moving to answers, as requested. Modelines are run in a restricted sandbox, there is only a limited number of things they can do, and defining keyboard maps is not among those. You can however set a filetype in a modeline, then define a map in a corresponding ftplugin file. If you add the <buffer> qualifier the map will be defined only for the ...


7

According to the second modeline format (the one with set) listed in :help modeline, you can put any text after the second :. # vim: set autoindent smartindent ts=4 sw=4 sts=4 noet : {# vim: set ft=jinja2: #}# vim: set ft=nginx: In the template source code, the last line is parsed by vim as [text][white]vim: set ft=jinja2: [text] and the trailing text ...


7

As it has been said, it's not possible by design. If you want something like modelines, you'll have to write your own plugin. In the early 2000 I wrote let-modeline that decodes a slightly different format of modelines (i.e. Vim: let var=expr) to set variables. For something more complex than a variable, I'd also use a local vimrc. However I wouldn't use ....


6

If you look at the :h auto-setting help file, you will see that the options are set on different occasions: At initialization On autocommands When starting editing the file with the modeline on You can reload the modeline without actually reloading the buffer by calling manually an autocommand: :doautocmd BufRead This will reload your modeline without ...


4

The name you're looking for is modeline. See the doc for the syntax :h modeline and :h 'modeline'


4

There are a few options for you, depending on what exact settings you'd like to tweak, how you'd like to manage them and whether you're worried about untrusted settings. Use "editorconfig" If the kinds of settings you would like to set are related to indentation style, tab size, file format and charset, then you might want to look into "editorconfig", ...


4

Vim only checks for "a number of lines at the beginning of the end of the file". By default, only the first and last 5 lines are checked. From :help 'modeline': If 'modeline' is on 'modelines' gives the number of lines that is checked for set commands And from help: 'modelines': 'modelines' 'mls' number (default 5) If you wish, you can :...


3

Per the answers to a question I recently asked on the Vi and Vim Stack Exchange, the vim modeline can only be used to trigger a predefined subset of parameters to the :set command already built into vim. It sounds like you could hack something up using .exrc, but a more promising direction that is probably better for your use case is setting up per-project ...


3

Vim treats help files special and resets some options beofre opening them. After trying your example, what seems to work is to add an explicit :set foldenable to your modeline, e.g. this seems to work for me: vim: tw=70 ft=help fdm=marker fmr={{{,}}} fdl=0 fen


3

I think you might be misunderstanding how the 'fileformats' (plural) setting is used. This setting is a global list of file formats that Vim tries when opening any file in order to see which matches the format of the file. When it has detected the format, Vim sets the per-file 'fileformat' (singular) option for that individual file. If you are setting '...


3

You can only set options with the modeline. You can't run Vimscript with it. This is for security reasons, because running code from random text files is not very good security. The reason that running normal zv from the autocommand doesn't work is that the BufReadPost autocommand is run before the modeline is processed. From :help BufReadPost: BufRead or ...


3

No, the 'undodir' option cannot be set from a modeline, because it has the 'SECURE' flag set. Since this is a global option, it does not make sense to set this option differently for a specific file, that would also affect all other buffers that are edited later then. However it might be possible to use a BufRead in combination with a BufWritePost ...


2

That is for security reasons. In the past, there have been a couple of vulnerabilities that resulted in e.g. arbitrary code execution. Also you might not want to have certain options set So one of the measures against it was to disallow modelines for the super user. Another measure was that only certain safe considered options can be set. Options that take ...


2

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 ...


1

Thanks everyone for the helpful comments. I didn't realise that this was not default behaviour. But with some binary searching in disabling plugins, I was able to find out that this was due to my use of the vim-markdown-folding plugin (which in retrospect seems obvious, sorry). Its foldexpr overrides the one I was using and causes this issue. I may ...


1

There are basically, two forms of modelines. Let me copy in the most important parts here together with an example: There are two forms of modelines. The first form: [text{white}]{vi:vim:ex:}[white]{options} [...] Examples: vi:noai:sw=3 ts=6 [...] The second form (this is compatible with some versions of Vi): [text{white}]{vi:vim:|Vim:ex:}[...


1

I tend to support file type specific configs using filetype plugins. Then using ft=myType in my modeline: // vim:set sw=2 ts=2 et ft=php fdm=marker: And it all woks right. You can also use autocommands to find file type and executs scripts. Finally, you can use a local ./.vimrc if :set exrc is on.


Only top voted, non community-wiki answers of a minimum length are eligible