# Fix Vim Modeline Ignored When Setting Is Disabled in Configuration

You open a file with a modeline at the top or bottom:

c
/* vim: set tabstop=4 shiftwidth=4 expandtab: */

But Vim ignores it. The tab settings do not change, and :set tabstop? shows the global default instead of the modeline value.

Why Modelines Are Disabled

For security reasons, many distributions ship Vim with modelines disabled by default. A malicious modeline could execute arbitrary commands or change critical settings. Check your modeline status:

vim
:set modeline?

If this returns nomodeline, modelines are completely disabled.

Enabling Modelines

Add to your ~/.vimrc:

vim
set modeline
set modelines=5

The modelines setting controls how many lines Vim checks at the beginning and end of the file. The default is 5, meaning Vim checks the first 5 and last 5 lines for modelines.

The modelineexpr Security Setting

Even with modelines enabled, the modelineexpr option controls whether modelines can contain expressions:

vim
:set modelineexpr?

If this is off (the default), modelines can only change options with set. They cannot execute arbitrary expressions:

```vim " This works with modelines enabled: /* vim: set tabstop=4: */

" This requires modelineexpr (and is dangerous): /* vim: set expr=...: */ ```

Do not enable modelineexpr unless you trust all files you open. It allows arbitrary code execution through modelines.

Modeline Format

The standard modeline format is:

bash
vim: set option=value option=value:

Or the alternative format (first/last line only):

bash
/* vim: tabstop=4 shiftwidth=4 expandtab */

Common options set via modeline:

python
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab textwidth=79
javascript
// vim: set tabstop=2 shiftwidth=2 softtabstop=2 expandtab:
yaml
# vim: set tabstop=2 shiftwidth=2 expandtab foldmethod=indent:

Modeline Not Working: Common Issues

Issue 1: Syntax Error in Modeline

A typo in the modeline causes Vim to ignore the entire line:

bash
# vim: set tabstop=4 shiftwith=4:
#                            ^^^^ Typo: should be shiftwidth

Vim does not warn about invalid options in modelines. It silently skips them.

Issue 2: Modeline Outside Check Range

If modelines=5 and the modeline is on line 10 of a 100-line file, Vim will not see it. Modelines must be in the first or last N lines of the file (where N is the modelines value).

Issue 3: Overridden by autocmd

An autocommand may override modeline settings after the file is loaded:

vim
:verbose set tabstop?

This shows the last place tabstop was set. If it says "Last set from ~/.vimrc line 45", an autocmd or filetype plugin is overriding the modeline.

Fix by using autocmd FileType with lower priority:

vim
autocmd FileType python setlocal tabstop=4

The setlocal ensures it only affects the buffer, and modelines in specific files can still override it if the modeline is processed after the autocmd.

Issue 4: Sandboxed Modelines

If Vim was compiled with the +sandbox feature and you open a file from an untrusted location, modelines may be restricted:

vim
:verbose set modeline?

Disabling Modelines for Specific Filetypes

If you want modelines enabled generally but disabled for specific filetypes (e.g., files from untrusted sources):

vim
autocmd FileType mail setlocal nomodeline
autocmd BufRead /tmp/* setlocal nomodeline

This is a good security practice when editing files downloaded from the internet.