Why does Vim ignore my modeline values?

后端 未结 6 1908
离开以前
离开以前 2020-12-14 17:01

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 ignor

6条回答
  •  伪装坚强ぢ
    2020-12-14 17:29

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

提交回复
热议问题