Search code examples
vimsyntax-highlightingfile-type

Vim filetype not properly set when text in file is starting with "#", therfore syntax is not properly highlighted


I created a dedicated syntax file .vim/syntax/foo.vim

and I would like to apply this syntax file for all files having the coresponding ending "myfile.foo"

For doing this, I added the following line in the .vimrc

au BufNewFile,BufRead *.foo setf foo

this is perfectly working, but...

If the text inside the file is starting with a "#", a different filetype is applied: filetype=conf

I can override this by using :set filetype=foo to get my desired type.

It is a bit annoying and I don't know how can I overcome this workaround.


Solution

  • Do not put anything to your vimrc!

    Insert your command into ~/.vim/ftdetect/foo.vim instead.

    The point is that your command must get into filetypedetect auto-group, otherwise any subsequent :filetype detect will overwrite your filetype to the defaults.