My project has decided to use XML for configuration files, and the
file extension is .cfg. Because of the extension, Vim wants to "setf
cfg" in filetype.vim, but I'd like to override that with a "setf xml".
I can do so manually with ":setf xml" after the file loads, but I'd
love to automate that. And, of course, I have other .cfg files that
are not XML, so I can't use file extension for type detection. I'd
like to automate this.
The first line of the XML .cfg file looks like:
<?xml version = "1.0" encoding="ISO-8859-1"?>
It sounds like you want the following (somewhat taken from ":help
ftdetect"):
1) make a $HOME/.vim/ftdetect directory
2) create a file (called something like "work.vim") in that
folder and give it a line something like
au BufRead,BufNewFile *.cnf
\ if getline(1) =~ '<? xml version' |
\ setf xml |
\ else |
\ setf cnf|
\ endif
If you look at your $VIMRUNTIME/filetype.vim you can see some
examples of this sniffing going on.
HTH,
-tim