I know how to set up features using :autocmd for specific file extensions:

Eg. ~/.vim/ftplugin/xml.vim

augroup xml
    au BufEnter *.xml call Setup_XML_Menu()
    au BufLeave *.xml call Destroy_XML_Menu()
augroup END

Never mind whatSetup_XML_Menu()does - the point is I'm setting and resetting configuration when I enter or leave an XML buffer.

But is there a way to apply an autocommand for a particular filetype instead?

If I enter a buffer named *.xml, the BufEnter autocmd kicks in as I expect.
But I'd like this to happen when I enter any buffer that has filetype=xml

So if I enter a buffer named server.wsdl (assume I've already arranged for this buffer's filetype to be xml), I'd like the XML autocmd to be applied there too, without configuring autocommands for every conceivable XML file extension.

I hoped I could have something like:

au BufEnter xml call ...


au BufEnter filetype xml call ...

But these don't work (the first example only applies to a buffer named 'xml').

The other possibility I considered: au FileType xml ... only applies when I explicitly change the filetype of the buffer - I want the settings to be applied and removed when I hop in and out of particular buffer windows.

Am I asking too much?


1 Answer 1


You could do something like au BufEnter * if &ft ==# 'xml' | commands | endif

  • What about au Filetype xml commands? Some times au BufEnter * if &ft fail, because 'filetype' does not update untill BufEnter finishes
    – Good Pen
    Commented Apr 3, 2022 at 1:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .