Vim Tips Wiki

Modeline magic

Revision as of 11:30, December 22, 2012 by (Talk)

1,619pages on
this wiki
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).

Note that the 'modeline' option must be set in order to take advantage of this tip. This option is set by default for Vim running in nocompatible mode, but some notable distributions of Vim disable this option in the system vimrc for security. In addition, it is off by default when editing as root. See :help 'modeline' for more information.


For example, in a particular file you may want each tab character that you type to be expanded to spaces. To achieve this, put the following modeline near the top or the bottom of that file:

# vim: set expandtab:

The space between the comment opening and vim: is required, otherwise the modeline will not be recognized.

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
/* vim: noai:ts=4:sw=4
/* vim: set noai ts=4 sw=4: */
/* vim: set fdm=expr fde=getline(v\:lnum)=~'{'?'>1'\:'1': */

With "set", the modeline ends at the first colon not following a backslash. Without "set", no text can follow the options, so for example, the following is invalid:

Error E518: Unknown option: */
/* vim: noai:ts=4:sw=4 */

Adding a modeline

With the following in your vimrc and the default leader key, you could type \ml to add a modeline based on your current settings:

" Append modeline after last line in buffer.
" 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)
nnoremap <silent> <Leader>ml :call AppendModeline()<CR>

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-\><C-N>ggOvim:ff=unix ts=4 ss=4<CR>vim60:fdm=marker<Esc>

Choosing the menu item would insert two modelines like this:

vim:ff=unix ts=4 ss=4



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



This mechanism can be extended to 'let' – see script#83.

See script#1876 - securemodelines

Around Wikia's network

Random Wiki