I noticed Vim will choose the correct syntax when I open a file that ends with
I would like to tell Vim to behave the same with extensions like
How is it possible?
You can use autocommands in your .vimrc like
au BufNewFile,BufRead ?\+.c.mako setf c
This will make all
*.c.mako files behave like
Make an analogous for
If you genenerally want
*.any.mako to behave like
*.any then you can use this autocommand:
au BufNewFile,BufRead ?\+.mako exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r"))
You need to globally associate a filename extension with a setting for the 'filetype' option when the file is opened. There are a few ways to do this. One way is described by @Naumann in another answer (involving your
.vimrc file). Let's look at two other ways. These are described in
:help new-filetype. Briefly...
With the first option you create a file, let's call it
~/.vim/ftdetect (Unix) or
$USERPROFILE\vimfiles\ftdetect (Win) and add a line like either of these:
au BufRead,BufNewFile *.mako set filetype=foo
au BufRead,BufNewFile *.mako filetype foo
Now (re)start Vim with a
.mako file and check the value of filetype. It
should be set to "foo". The difference between the two is that Method A always overrides the filetype while Method B only works if no filetype has been detected yet for that extension.
The second option is similar to the other answer here except it avoids cluttering up your vimrc file. Create a file named
~/.vim (Unix) or
$USERPROFILE\vimfiles (Win). Then add the following:
if exists("did_load_filetypes") finish endif augroup filetypedetect au! BufRead,BufNewFile *.mako setfiletype foo augroup END
You can add multiple
setfiletype lines for whatever extensions you want. One important point about this method from Vim help:
Your filetype.vim will be sourced before the default FileType autocommands have been installed. Your autocommands will match first, and the ":setfiletype" command will make sure that no other autocommands will set 'filetype' after this.