12 votes

Is there a way to set an abbreviation in a modeline so it is always available local to one file buffer?

: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 ...
user avatar
  • 28.1k
11 votes
Accepted

Is it possible to create custom "set" commands in my rc file and call them from a modeline?

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 ...
user avatar
  • 16.2k
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 ...
user avatar
  • 3,879
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=...
user avatar
  • 12.8k
7 votes
Accepted

Is there a way to set an abbreviation in a modeline so it is always available local to one file buffer?

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 ...
user avatar
  • 16.2k
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 ...
user avatar
  • 15.2k
4 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 ...
user avatar
  • 26.2k
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'
user avatar
  • 46.5k
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' ...
user avatar
  • 1,453
3 votes
Accepted

Is it possible to include coding style vim modelines e.g. psr-2 or drupal?

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 ...
user avatar
  • 2,135
3 votes
Accepted

Modeline folding not working on help file

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 ...
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 ...
user avatar
  • 28.1k
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, ...
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 ...
user avatar
  • 23.9k
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 ...
user avatar
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 ...
user avatar
  • 16.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 ...
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 ...
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:}[...
user avatar
1 vote

Is it possible to include coding style vim modelines e.g. psr-2 or drupal?

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 ...
user avatar
  • 1,152

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