开发者

Why does Vim ignore my modeline values?

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开发者_JS百科? And BTW, I have nocompatible set in my .vimrc.


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.


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


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.


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


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 ... !)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜