I am trying to enable .xml syntax highlighting in Vim for .xaml files since .xaml files don't have syntax highlighting of its own. I came across a post on Stack Overflow which recommended to use au BufNewFile,BufRead *.xaml setf xml but that did not provide syntax highlighting as soon as I open the .xaml file. I can see below on the console that the .xaml files are changed to .xml filetype.

I tried what this post suggested: autocmd BufEnter *.xaml :setlocal filetype=xml but this does not provide syntax highlighting either.

I even tried modifying it to: autocmd BufEnter *.xaml :setlocal filetype=xml | syntax on but it causes several glitches and formats the syntax higlighting of all my other open tabs & windows as .xml. Any suggestions are deeply appreciated. Thank you!

  • 1
    Don't use syntax on (if you must, use syntax enable). Does syntax highlighting work for .xml files? What filetype is reported in a .xml file? The best way to add simple filetype rules is to create a file ~/.vim/ftdetect/xaml.vim with autocmd BufRead,BufNewFile *.xaml setfiletype xml, though sometimes you want set filetype=xml instead. Can you edit to answer these questions and clarify what "it does not behave…" or "syntax highlighting does not function" mean?
    – D. Ben Knoble
    Commented Jul 26, 2022 at 15:52
  • @D.BenKnoble I apologize. I've made the edits to further clarify as you reqested! Commented Jul 26, 2022 at 16:06
  • Thanks, what about the first questions? Does syntax highlighting work for .xml files? What is their filetype?
    – D. Ben Knoble
    Commented Jul 26, 2022 at 17:09
  • @D.BenKnoble Yes! .xml syntax highlighting does work but I have to enable it while in vim, I have to :set filetype=xml & then :syntax on/enable. This way, I do get proper syntax highlighting for '.xaml' files as well. Commented Jul 26, 2022 at 18:00
  • 1
    Have you tried How to debug my vimrc? What does your minimal vimrc that reproduces the problem look like?
    – D. Ben Knoble
    Commented Jul 26, 2022 at 18:53

1 Answer 1


Assuming all works as you wish it when you enter :set filetype=xml when your XAML file is open in a buffer, try this: create file ~/.vim/ftdetect/xaml.vim containing the following:

" Detects XAML files as XML
au BufNewFile,BufRead *.xaml setlocal filetype=xml

This has worked for me for other "odd" file types.


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.