3

I noticed Vim will choose the correct syntax when I open a file that ends with .c.in or .pl.in.

I would like to tell Vim to behave the same with extensions like .c.mako or .pl.mako.

How is it possible?

2

You can use autocommands in your .vimrc like

au BufNewFile,BufRead ?\+.c.mako setf c

This will make all *.c.mako files behave like *.c files.

Make an analogous for *.pl.mako

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"))

1

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 mako.vim, in ~/.vim/ftdetect (Unix) or $USERPROFILE\vimfiles\ftdetect (Win) and add a line like either of these:

Method A:

au BufRead,BufNewFile *.mako  set filetype=foo

Method B:

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 filetype.vim in ~/.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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.