I need Vim to ignore the .tmpl file extension to enable syntax highlighting.

How can I rewrite this:

autocmd BufNewFile,BufRead *.lua.tmpl set filetype=lua

to be more general? I want to match the lua and use it in the filetype.

Something like this is what I want:

autocmd BufNewFile,BufRead *.\(.*\).tmpl set filetype=\1

I use Neovim and I am currently wrapping the above in vim.cmd(). If this is somehow easier to do with vim.api.nvim_create_autocmd, then this is fine, too.

One way would be to copy and paste for each file extension. Because I don't want to do that, I was going to make a chezmoi (dotfile manager) template that automatically finds all extensions that I need. I doubt that this will be good for performance, though. Also seems like a lot of work for something that sounds easy.

I tried to escape the pattern and somehow use \v magic but I am unable to. I wasn't able to find an example so I came here.

2 Answers 2


You could use something like

" ~/.vim/ftdetect/tmpl.vim
autocmd BufNewFile,BufRead *.tmpl execute 'set filetype='..matchlist(expand('<afile>'), '.*\.\(.*\)\.tmpl$')[1]

Better error handling would be worthwhile, however.


I would do:

for ext in ['lua', 'tex']
  execute 'autocmd BufNewFile,BufRead *.' .. ext .. '.tmpl set filetype=' .. ext

But bear in mind that, in general, the file extension (e.g. .py) could be different than the filetype (e.g. python)

  • Remark the file pattern and the text pattern are different in Vim see :help file-pattern Commented Apr 24, 2023 at 1:39
  • This is what I settled on for now. But is there no way to capture any arbitrary ext? Commented Apr 24, 2023 at 8:37
  • Not that I'm aware off. Commented Apr 24, 2023 at 8:56
  • 1
    I'm assuming capital EX was a typo?
    – D. Ben Knoble
    Commented Apr 24, 2023 at 13:21
  • @Ben, thanks :-) I have updated the code. Commented Apr 24, 2023 at 13:32

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.