4

It used to work by adding following line into a file even it has no suffix .vim, my vim version is v8.0.1500, on OS X 10.13.3

" vim:ft=vim

UPDATE:

I tried adding following two lines to $MYVIMRC, after filetype plugin indent on

set modeline
set modelines=5

And changed

" vim:ft=vim

to

" vim:set  ft=vim

Still doesn't work :(

Here is my .vimrc for reference, and for example, this file couldn't be detected as a vim file.

SOLUTION

  1. Add following two lines to $MYVIMRC, after filetype plugin indent on
set modeline
set modelines=5
  1. use " vim:ft=vim or " vim:set ft=vim: instead of " vim:set ft=vim
8
  • 1
    Do you have something along the lines of filetype plugin on in your vimrc?
    – statox
    Mar 29, 2018 at 8:53
  • @statox please check my update above :)
    – kenshinji
    Mar 30, 2018 at 7:39
  • 1
    That's a weird problem. If your modeline contains other settings are they applied or are they also ignored? Also how do you check that it doesn't work? (You probably know how to check it properly, it's just to get rid of the obvious :) )
    – statox
    Mar 30, 2018 at 7:42
  • statox raises a good point. Could you add another setting to the modeline and see if it takes effect? Something obvious like nomodifiable. This way you can isolate whether the problem is with your filetype settings or your modeline detection.
    – ZeroKnight
    Mar 30, 2018 at 8:03
  • 1
    As ZeroKnight say I was suggesting to set nomodifiable in your modeline to be sure that the modeline is working. But I see from your update that Wang Shidong's answer was what you were looking for :)
    – statox
    Mar 30, 2018 at 9:46

3 Answers 3

5

As Naumann pointed out, modeline has to be set on. This is the default if you're not root.

Your modeline also has to be within modelines (default 5) lines from the top or bottom of the file in order to be detected.

You should also test that the settings work when you set them manually. You might be missing a dependent setting like filetype on.

2

First, you need make sure your modeline is on. Check it via set modeline?.

Then, update the last line to

" vim:set  ft=vim:

You need to add an :

BTW, new modeline will not be executed until some autocmd is called, for example.

  1. open a new empty file
  2. add " vim:set ft=vim: to then end

Then, you will see the file type will not have changed.

Then, if you use :w or :e, the filetype will change.

3
  • wait, modeline and modelines are two different things, right?
    – kenshinji
    Mar 30, 2018 at 6:43
  • @kenshinji Correct. The former enables or disables modelines entirely. The latter defines how many lines from the beginning or end of the file your actual modeline must be in order to be recognized. See Karl Bielefeldt's answer.
    – ZeroKnight
    Mar 30, 2018 at 7:36
  • Sorry, I just update the answer, Thanks for you info. Mar 30, 2018 at 11:30
1

You need to have :set modeline (e.g. in your ~/.vimrc).

3
  • That shouldn't be needed since modeline is on by default: :h 'modeline'
    – statox
    Mar 29, 2018 at 8:54
  • 2
    @statox I think it depends on the system you are using, please check this, it says "Debian, Ubuntu, Gentoo, OSX, etc. by default disable modelines for security reasons."
    – kenshinji
    Mar 30, 2018 at 7:07
  • @kenshinji Interesting I didn't know these distros did that.
    – statox
    Mar 30, 2018 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.