2

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!

6
  • 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
    Jul 26, 2022 at 15:52
  • @D.BenKnoble I apologize. I've made the edits to further clarify as you reqested! 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
    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. 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
    Jul 26, 2022 at 18:53

1 Answer 1

3

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.

1
  • 2
    @JoseMembreno Does this answer work for you? I ask because you've marked it as accepted, but your comment says it doesn't work. If it does work, please delete the comment so as not to confuse future readers. Thanks
    – mattb
    Jul 26, 2022 at 20:42

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.