The following snippet is from the vim-pandoc-syntax plugin README:

If you want to use vim-pandoc-syntax without vim-pandoc, you'll need to tell Vim to load it for certain files. Just add something like this to your vimrc:

augroup pandoc_syntax
    au! BufNewFile,BufFilePre,BufRead *.md set filetype=markdown.pandoc
augroup END

What might be the reason to use BufNewFile here? Isn't it that BufFilePre and BufRead are sufficient?

When we create a new empty buffer, its filetype value is blank in either case.

1 Answer 1

  1. Using :h ftdetect is preferred over direct approach. This way you add commands into predefined filetypedetect augroup and so you don't need to create yet another one.

  2. BufNewFile is needed to handle :e newfile.md and such. And, no, in this case a newly created file can get a correct filetype even though it has neither contents nor it was read from disk.

  3. BufFilePre is not useful here. I guess you mean BufFilePost instead. But it's only triggered after :file or :saveas commands, so usually no one cares.

  • It seems that in addition to BufNewFile, BufFilePost, and BufRead, there should be FileChangedShellPost.
    – john c. j.
    Aug 25, 2022 at 8:34
  • @johnc.j. There's no need for that.
    – Matt
    Aug 25, 2022 at 9:50
  • I have tested it with autocmd BufFilePost,BufNewFile,BufReadPost,FileChangedShellPost *.md set syntax=off. If I change a file to read-only by using the shell, Vim will prompt me to re-load it, and then if I choose to do so, syntax highlighting will be turned on (unless I add FileChangedShellPost).
    – john c. j.
    Aug 25, 2022 at 9:57
  • @johnc.j. As expected, as you have automatic syntax.
    – Matt
    Aug 25, 2022 at 10:28

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.