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 Answers 2

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

2

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. Let's look at two other ways. These don't involve your vimrc file and are the recommended approaches per :help new-filetype (which I encourage you to read!)

With the first option you create a file, let's call it perlmako.vim, in ~/.vim/ftdetect (Unix) or $USERPROFILE\vimfiles\ftdetect (Win) and add a line like either of these:

Method A:

au BufRead,BufNewFile *.pl.mako set filetype=perl

Method B:

au BufRead,BufNewFile *.pl.mako setfiletype perl

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.


For the second option, 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 *.pl.mako setfiletype perl
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 and acknowledge you have read our privacy policy.

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