Wikia

Vim Tips Wiki

Modeline magic

Talk0
1,612pages on
this wiki
Revision as of 10:11, March 20, 2014 by 113.104.213.252 (Talk)

Tip 331 Printable Monobook Previous Next

created 2002 · complexity basic · version 6.0


Options set in your vimrc will apply to all files that you edit. You can also set:

  • Different options for all files of a certain type (for example, all *.py files). See filetype.vim and filetypes and an example.
  • Different options for a particular file using modelines (this tip).


The modeline cannot be anywhere in the file: it must be in the first or last few lines. The exact location where vim checks for the modeline is controlled by the modelines variable; see :help 'modelines'. By default, it is set to 5 lines.

In a Python file, the '#' starts a comment so the modeline is not interpreted by Python.

The following examples show some alternatives that could be in a C file:

// vim: noai:ts=4:sw=4
   -or-
/* vim: noai:ts=4:sw=4
*/
   -or-
/* vim: set noai ts=4 sw=4: */
   -or-
/* vim: set fdm=expr fde=getline(v\:lnum)=~'{'?'>1'\:'1': */

" Use substitute() instead of printf() to handle '%%s' modeline in LaTeX " files. function! AppendModeline()

 let l:modeline = printf(" vim: set ts=%d sw=%d tw=%d %set :",
       \ &tabstop, &shiftwidth, &textwidth, &expandtab ?  : 'no')
 let l:modeline = substitute(&commentstring, "%s", l:modeline, "")
 call append(line("$"), l:modeline)

endfunction nnoremap <silent> <Leader>ml :call AppendModeline()<CR> </pre>

In a C file, you would get a modeline like this:

/* vim: set ts=8 sw=4 tw=0 noet : */

Alternatively, you could use a simple menu entry, for example:

amenu Edit.Insert\ &modeline <C-
==Security==
{{Todo}}
*Need note on security implications of modelines and reference to alternatives.
*Text other than "vim:" can be recognised as a modeline.
*Google "vim modeline vulnerability" (without quotes) for information.

==References==
*{{help|auto-setting}}
*{{help|modeline}}
*{{help|'modeline'}} determines whether to look for modelines
*{{help|'modelines'}} number of lines checked to find each modeline

==Comments==
This mechanism can be extended to 'let' – see {{script|id=83}}.

See {{script|id=1876}} - securemodelines
----
How can we make this page more discoverable? New users may have seen a comment at the top of a file, with vim settings in it, but they don't know that this is called a 'modeline'. They may search for something like ''"set options in a comment"'' and we should return this page. <small>--Preceding [[Vim Tips Wiki:Quick reference|unsigned]] comment added bIt should help Google find that as well. In practice it is difficult anticipating what people might search for, but your suggested text is very reasonable so a redirect is worthwhile. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 09:10, November 26, 2013 (UTC)
::I just tried "vim set options in a comment" (without quotes) in Google, and this page was #3, behind Stackoverflow at #1 and #2. It's hard to improve on #3, but we'll see what happens in a few days. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 09:12, November 26, 2013 (UTC)

----

Around Wikia's network

Random Wiki