[vim/vim] Unable to override .xpm filetype detection (#8914)

16 views
Skip to first unread message

Eric Lathrop

unread,
Sep 24, 2021, 4:29:37 PM9/24/21
to vim/vim, Subscribed

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:

  1. Create ~/.vim/ftdetect/xml.vim with contents autocmd BufNewFile,BufRead * if getline(1) =~ '<?\s*xml.*?>' | set filetype=xml | endif
  2. Create a file test.xxx with contents <?xml version="1.0" encoding="UTF-8"?>
  3. Run vim test.xxx
  4. Run :set filetype
  5. Observe that filetype=xml
  6. Create a file test.xpm with contents <?xml version="1.0" encoding="UTF-8"?>
  7. Run vim test.xpm
  8. Run :set filetype
  9. Observe that 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):

  • Vim version: 8.2.2815
  • OS: Ubuntu 20.04
  • Terminal: GNOME Terminal


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.

Eric Lathrop

unread,
Sep 24, 2021, 4:34:26 PM9/24/21
to vim/vim, Subscribed

I also tried commenting out the xpm sections of /usr/share/vim/vim82/filetype.vim to no effect.

Robertus Diawan Chris

unread,
Sep 25, 2021, 6:34:21 PM9/25/21
to vim/vim, Subscribed

Have you tried using *.xpm instead of *?
Maybe you can take a look at one of tpope's plugin here as reference.

Eric Lathrop

unread,
Sep 25, 2021, 7:51:32 PM9/25/21
to vim/vim, Subscribed

Yes. Using "*.xpm" didn't make a difference.

On September 25, 2021 6:33:54 PM EDT, Robertus Diawan Chris ***@***.***> wrote:
>Have you tried using `*.xpm` instead of `*`?
>Maybe you can take a look at one of tpope's plugin [here](https://github.com/tpope/vim-git/blob/master/ftdetect/git.vim) as reference.
>
>--
>You are receiving this because you authored the thread.
>Reply to this email directly or view it on GitHub:
>https://github.com/vim/vim/issues/8914#issuecomment-927192247

Bram Moolenaar

unread,
Sep 27, 2021, 5:05:29 PM9/27/21
to vim/vim, Subscribed

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

Eric Lathrop

unread,
Oct 1, 2021, 12:32:40 PM10/1/21
to vim/vim, Subscribed

@brammool That change solves my issue. Should I file a PR for that?

Bram Moolenaar

unread,
Oct 9, 2021, 11:30:27 AM10/9/21
to vim/vim, Subscribed

Closed #8914 via 6e77b88.

Reply all
Reply to author
Forward
0 new messages