8 votes
Accepted

How to set a key mapping in the modeline?

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 ...
Sato Katsura's user avatar
  • 4,009
7 votes
Accepted

Having two modelines in one file, is that possible?

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=...
Mass's user avatar
  • 14.1k
6 votes

Is there a way to make some modeline that applies to all files in a directory, recursively?

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 ...
filbranden's user avatar
  • 28.8k
6 votes
Accepted

How to apply a modeline added to an unnamed buffer?

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 ...
nobe4's user avatar
  • 16k
4 votes
Accepted

Mode line not working even if everything seems correct

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' ...
husB's user avatar
  • 2,068
4 votes
Accepted

What is the name of the feature where one includes a set of options at the bottom of a file to be invoked immediately when opening it?

The name you're looking for is modeline. See the doc for the syntax :h modeline and :h 'modeline'
statox's user avatar
  • 49.7k
3 votes

List known abbreviations for filetypes

file type abbreviations are not accepted by the :setfiletype command. The :setfiletype set the filetype literally. e.g.: :setfiletype js Set the filetype to js (:set ft? returns js) which is not ...
Vivian De Smedt's user avatar
3 votes
Accepted

Local persistent undo file modeline

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, ...
Christian Brabandt's user avatar
3 votes
Accepted

What are the compatibility differences between the two modelines?

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 ...
muru's user avatar
  • 24.8k
3 votes
Accepted

How to unfold a fold the cursor is in using modeline?

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 ...
Martin Tournoij's user avatar
3 votes
Accepted

modelines not executed when opening new file in new tab

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 ...
Rich's user avatar
  • 31.9k
2 votes
Accepted

Why is modeline off for root?

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 ...
Christian Brabandt's user avatar
2 votes

Extending modeline functionality?

What can and can't be done with modelines is documented under :help modeline. Your two examples are currently impossible with the built-in modeline feature but nothing is stopping you from devising ...
romainl's user avatar
  • 40.4k
2 votes

Vim modeline settings options words appear in selected in red

JSON does not have any comments. Hence, the line is malformed and highlighted as an error. You can disable this by setting let g:vim_json_warnings = 0 before opening the file. However, an error still ...
Matt's user avatar
  • 20.7k
2 votes
Accepted

Cannot set textwidth through modeline in Python files

You are missing the trailing colon (:) You have to do: # vim: set textwidth=120: Or: # vim: textwidth=120 Remark: the set: variant has been introduced because the other variant will parse the entire ...
Vivian De Smedt's user avatar
2 votes
Accepted

noremap not working in modeline

You can't. You are probably thinking of modelines, but those allow you to only specify specific options when starting to edit that particular file (and only if the modeline and modelineexpr options ...
Christian Brabandt's user avatar
1 vote

Extending modeline functionality?

The modeline is limited on purpose for security reasons, to prevent running arbitrary code. This is also why you can't set any of the *expr options that evaluate VimScript. So, you will need some ...
Martin Tournoij's user avatar
1 vote

Is there any way to prevent modelines getting reapplied when files are saved / written?

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 ...
Andrew Ferrier's user avatar
1 vote
Accepted

Vim's "path" variable isn't changing or updating with modeline setting (finding header file in different directory)

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:}[...
Christian Brabandt's user avatar

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