Describe the bug
I have some xml files with the .xpm
extension. I created ~/.vim/ftdetect/xml.vim
with the following contents:
autocmd BufNewFile,BufRead * if getline(1) =~ '<?\s*xml.*?>' | set filetype=xml | endif
When I open my file it shows filetype=xpm
when it should show filetype=xml
. My ftdetect script works as long as the extension is not ".xpm".
To Reproduce
Detailed steps to reproduce the behavior:
~/.vim/ftdetect/xml.vim
with contents autocmd BufNewFile,BufRead * if getline(1) =~ '<?\s*xml.*?>' | set filetype=xml | endif
test.xxx
with contents <?xml version="1.0" encoding="UTF-8"?>
vim test.xxx
:set filetype
filetype=xml
test.xpm
with contents <?xml version="1.0" encoding="UTF-8"?>
vim test.xpm
:set filetype
filetype=xpm
Expected behavior
I expect my ftdetect script to be able to override the filetype for files with an .xpm extension.
Environment (please complete the following information):
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.
I also tried commenting out the xpm sections of /usr/share/vim/vim82/filetype.vim
to no effect.
Have you tried using *.xpm
instead of *
?
Maybe you can take a look at one of tpope's plugin here as reference.
It is because of these lines in filetype.vim:
" X Pixmap (dynamically sets colors, use BufEnter to make it work better)
au BufEnter *.xpm
\ if getline(1) =~ "XPM2" |
\ setf xpm2 |
\ else |
\ setf xpm |
\ endif
This seems nasty, if you had set the filetype manually or with another rule, just switching buffers would reset it to "xpm" or "xpm2".
I'm not so sure the original reason to do it this way is still valid. X pixmap files are hardly used, I guess, and the requirement for using BufEnter isn't so clear to me. I would expect ":edit" to reload the colors.
Try changing those lines to:
au BufNewFile,BufRead *.xpm
\ if getline(1) =~ "XPM2" |
\ setf xpm2 |
\ else |
\ setf xpm |
\ endif
au BufNewFile,BufRead *.xpm2 setf xpm2
@brammool That change solves my issue. Should I file a PR for that?