Why does Vim ignore my modeline values?
I tracked the problem to a local plugin, called local_vimrc.vim. The fact that modeline does not work is a side-effect of the plugin.
If you are saving and loading views, for example with something like the following, your modeline will not be reevaluated when you reopen a file.
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview
Here is what you check...
Step 1
Make sure your settings are right. Do...
:verbose set modeline? modelines?
If you see either of these values returned, it's not going to work.
nomodeline
modelines=0
(By adding verbose
before the set
command, you will be told what file produced that setting. Thank you, drew010)
What you need to see is this (where that 4 is anything greater than 0)
modeline
Last set from /usr/share/vim/vimrc
modelines=4
Last set from ~/.vim/vimrc
Note from the comments: If you have either nomodeline
or modelines=0
, you are going to need to add set
and the corresponding setting from the previous code block. (Thank you @pilat)
Step 2
Do you have a line with a vim:
that is not touching any else within the last <modelines> lines of your document?
Seriously, that's all it takes.
These modelines all work...
# vim: cursorline
// vim: cursorline
; vim: cursorline
vim: cursorline
# anything here vim: cursorline
even without a valid comment vim: cursorline
literally anything as long a space separates> vim: cursorline
# vim: set cursorline: <-that colon is required if you use the word "set" and this comment after the colon does not affect it.
Notice that the only way to have a comment after the modeline is to use the word set
. But if you use set
, you must put a colon at the end of your settings even if you have no comment. Thank you, Amadan
Step 3
And this is an important one! Are you using the word set
? (as described as the "second form" in the options doc) Take out the set
or add a colon to the end.
None of these work...
# vim: set cursorline
^^^ issue: use of the word set without a trailing colon (:)
# vim:cursorline
^ issue: no space after the colon
#vim: cursorline
^ issue: no space before the word vim
Step 4
Have you been awake for more than 18 hours? Before you lose your mind, get some sleep. It'll still be broken tomorrow. You'll be more likely to notice the issue when you go through this list again then.
Update notes
This answer used to include this claim:
Are you using the word set? (as described as the "second form" in the options doc) Well, that doesn't work in version 8, and I don't know when it stopped working. Use the "first form".
I was wrong. It was pointed out by @Amadan and I have corrected Step 3.
I had a similar issue with my modeline not working. The answers in this thread helped me find my answer (which was adding set modeline to my ~/.vimrc)
https://superuser.com/questions/323712/modeline-not-work-in-vim
Also one thing that helped me debug this was to type :set in vim. This will tell you the different values that are currently set.