2

I have au BufRead,BufNewFile *.vm set ft=velocity in vimrc.
But if open .vm file and run :set filetype?, still get filetype=conf.
How to fix this?

6
  • Hmm... maybe something else in your config does set ft=conf after your autocmd. Try putting it in .vim/after/ftdetect/velocity.vim. And I suggest you put your autocmd in an group, e.g augroup FtVelocity (see this). You will than be able to see if it is taken into account with :au FtVelocity
    – perelo
    Jul 26, 2019 at 11:34
  • Tried moving set ft=velocity to ~/.vim/after/ftdetect/velocity.vim, but still not working.
    – Fisher
    Jul 26, 2019 at 11:50
  • 2
    In your *.vm buffer use :verbose set ft? it will show you where the filetype was last set, that should help you debugging your issue.
    – statox
    Jul 26, 2019 at 12:33
  • 1
    Found the reason, I was editing another vimrc. Because I'm running vim from Windows, didn't notice I was editing Linux vimrc. Thank you all!
    – Fisher
    Jul 26, 2019 at 12:46
  • 1
    @statox Could you add :verbose set ft? as answer, I think it's good to debug this kind of issue with the command.
    – Fisher
    Jul 26, 2019 at 12:49

1 Answer 1

3

First as a general advice, :h :verbose is a good way to debug stuff with Vim:

                        *:verb* *:verbose*
:[count]verb[ose] {command}
            Execute {command} with 'verbose' set to [count].  If
            [count] is omitted one is used. ":0verbose" can be
            used to set 'verbose' to zero.

So using :verbose set ft? in the buffer with the wrong filetype allows you to see what was the last piece of configuration which set the filetype of your buffer. For example I get this result for a typescript source file:

filetype=typescript
        Last set from ~/.dotfiles/vim/plugged/yats.vim/ftdetect/typescript.vim

Secondly the best practice to write your own filetype is to use the ftdetect directory (:h ftdetect). The idea is to create a directory ftdetect in your runtimepath. In it you should create a file name mynewfiletype.vim (here it would be velocity.vim).

In this file you can create an autocommand like the one you put in your vimrc. Note that you should always put the autocommands in your vimrc in an augroup to avoid side effects but the autocommands used in the ftdetect files should not be in an augroup because Vim already does that for you.

1
  • Sometimes some plugins take precedence and override runtimepath/ftdetect/*. In this case you could put your file detection in runtimepath/after/ftdetect/*.
    – Dzintars
    May 19, 2022 at 18:08

Your Answer

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

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