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 Answer
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.
-
Sometimes some plugins take precedence and override
runtimepath/ftdetect/*
. In this case you could put your file detection inruntimepath/after/ftdetect/*
.– DzintarsCommented May 19, 2022 at 18:08
set ft=conf
after yourautocmd
. Try putting it in.vim/after/ftdetect/velocity.vim
. And I suggest you put yourautocmd
in an group, e.gaugroup FtVelocity
(see this). You will than be able to see if it is taken into account with:au FtVelocity
set ft=velocity
to~/.vim/after/ftdetect/velocity.vim
, but still not working.*.vm
buffer use:verbose set ft?
it will show you where the filetype was last set, that should help you debugging your issue.:verbose set ft?
as answer, I think it's good to debug this kind of issue with the command.