I'm using a Python file with the following modeline at the end:
# vim: sts=4:ts=4:sw=4
If I start Vim with this file the modeline is ignored. How can I fix this? And BTW, I have nocompatible
set in my .vimrc
.
Join Stack Overflow to learn, share knowledge, and build your career.
I'm using a Python file with the following modeline at the end:
# vim: sts=4:ts=4:sw=4
If I start Vim with this file the modeline is ignored. How can I fix this? And BTW, I have nocompatible
set in my .vimrc
.
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)
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
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
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.
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.
#
and vim
. reading it after traced down problem myself tho. thanks
– vigilancer
Feb 18 '17 at 6:51
/usr/share/vim/vim74/debian.vim
that had set nomodeline
in it which prevented them from working despite all other settings. To figure that out from within vim, run :verbose set modeline?
and it will output something like modeline Last set from /usr/share/vim/vim74/debian.vim
which is where I found and commented out the conflicting setting. Modelines worked fine after that.
– drew010
Nov 1 '18 at 15:28
set
works if one pays attention to the syntax of the two forms. # vim: set cursorline
won't work, because the second (set
) form requires a terminating colon. # vim: set cursorline:
works.
– Amadan
Nov 20 '18 at 5:28
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.
vim -u NONE
to start vim without loading the vimrc (no bundles or plugins). If the file loads properly, then you know it's either your vimrc or your bundles. you can re-add bundles one by one until the modeline breaks to track down which bundle caused the issue.
– stonewareslord
Nov 12 '14 at 23:48
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.
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
The first step is always look in
/usr/share/vim/vim74/debian.vim
and comment out the nomodeline line.
Kubuntu distributions consider it a security
threat.
(EG: libcal allows you to run an arbitrary program)
This is only an issue if someone off system can run vim via a webpage. (don't trust user input.)
It is also possible to do a ddos attack using spell checker. (don't trust user input.)
set nomodeline
in a personal vimrc than to open a potential security hole for the root user by editing system configs which maybe be wiped during an upgrade.
– D. Ben Knoble
May 9 '19 at 0:20
I have had such problem with my modeline and "noexpandtab":
# vim: noexpandtab ft=python
but if I write:
# vim: ft=python noexpandtab
it works again. (Thank you guys for knowledge about :verbose set ... !)