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?
1 Answers
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.
- 49,782
- 19
- 148
- 225
-
Sometimes some plugins take precedence and override
runtimepath/ftdetect/*. In this case you could put your file detection inruntimepath/after/ftdetect/*. – Dzintars May 19 '22 at 18:08
set ft=confafter yourautocmd. Try putting it in.vim/after/ftdetect/velocity.vim. And I suggest you put yourautocmdin an group, e.gaugroup FtVelocity(see this). You will than be able to see if it is taken into account with:au FtVelocity– perelo Jul 26 '19 at 11:34set ft=velocityto~/.vim/after/ftdetect/velocity.vim, but still not working. – Fisher Jul 26 '19 at 11:50*.vmbuffer use:verbose set ft?it will show you where the filetype was last set, that should help you debugging your issue. – statox Jul 26 '19 at 12:33~/.vim/ftdetect/. OP may also trysetfiletype(though I dont expect it to work) – D. Ben Knoble Jul 26 '19 at 12:41:verbose set ft?as answer, I think it's good to debug this kind of issue with the command. – Fisher Jul 26 '19 at 12:49