1

The issue

Consider the following two Vimrc configuration lines for the YouCompleteMe plugin:

au BufRead, BufNewFile *.py,*.pyw match BadWhitespace /\s\+$/
au FileType python match BadWhitespace /\s\+$/

When opening an existing file (vim somefile.py), the first one loads and works correctly (wrong whitespace gets highlighted), while the second one results in the following error message but the functionality still works correctly (wrong whitespace gets highlighted):

Error detected while processing BufRead Autocommands for "*.py"..FileType Autocommands for "python":
E28: No such highlight group name: BadWhitespace

Questions

This issue is confusing on so many aspects...

  1. Based on the Q&A linked below, I understood that the two main differences between BufRead+BufNewFile vs FileType are the following:
  • BufRead+BufNewFile does not affect writing to an existing file
  • FileType is not aware of all file formats

However, neither of these explains the reported behavior, because FileType is, in fact, aware of Python files, and in this case we are not writing a file (but reading one).

  1. Why does the functionality still work in spite of the error message?

  2. The error message is slightly confusing, because it talks about BufRead Autocommands, whereas the used keyword was FileType (and there is no error message when BufRead is used for real). So is FileType just a wrapper around BufRead?

Remark

As stated, I am aware of this Question and the related Answers (1, 2), but as I mentioned, they don't explain the case above. So I believe this is some kind of an edge case and hence a new question is appropriate.

5
  • What creates the BadWhitespace highlight group?
    – D. Ben Knoble
    Commented Sep 29, 2023 at 13:12
  • 1
    @D.BenKnoble Not sure... For some reason, I thought it was YouCompleteMe plugin, but after some search, I believe that is not the case, so maybe it is not created at all... But then, the question is still open: 1.) Why no error messages in the first case 2.) Why does it still work even if that highlight is not defined.
    – Attilio
    Commented Sep 29, 2023 at 16:40
  • Is the space between BufRead, and BufNewFile really there or is it just a typo in your question? Commented Oct 2, 2023 at 6:45
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Oct 4, 2023 at 5:46
  • @JürgenKrämer: it is really there (I already saw somewhere else that it is important to have it)
    – Attilio
    Commented Oct 10, 2023 at 9:54

1 Answer 1

1

The BufRead event is triggered after the FileType event if the corresponding autocommand is after the filetype plugin on command (or after the plug#end() call if you are using plug)

I suppose the BadWhitespace is defined in between the two.

Which make the the second trigger a warning where the second does not.

Remark: the FileType event is not equivalent to the BufRead event. But practically it come close for a lot of files. If the file is recognized by Vim there is a BufRead BufNew event autocmd that set the filetype (e.g.: in $VIMRUNTIME/filetype.vim) triggering the FileType event.

Associating your command to FileType makes that you associate it indirectly to one of the first BufRead. Associating your command to BufRead makes that you add it to the list of commands that is already associated to it. It depends where you have it in you vimrc But if it is after filetype plugin on it is after FileType.

4
  • 1
    Thanks, I believe this address questions 1 and 2. (BadWhitespace is not yet visible when FileType is run, but it becomes accessible later.)
    – Attilio
    Commented Oct 1, 2023 at 14:43
  • 1
    Thanks for the feedback :-) What remains open in your question? What is the result of the :verbose hi BadWhitespace? Commented Oct 1, 2023 at 15:02
  • 1
    Thanks Vivian. I believe I understand the difference between BufRead+BufNew vs FileType better now :)) Result of :verbose hi BadWhitespace is the same in both cases: highlight group not found: BadWhitespace. So apparently it is not defined at all, but the first option swallows the warning (and then, in both cases, I get the highlight anyway for some other reason). I need to look better into it, but I believe you did answer my original question :))
    – Attilio
    Commented Oct 10, 2023 at 9:53
  • Thanks for the feedback :-) Commented Oct 10, 2023 at 10:04

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.