0

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

0

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.

0

I would do:

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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