1

I'm using a tip from this page of the vim wiki to highlight trailing spaces when I'm not in insert mode :

highlight ExtraWhitespace ctermbg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()

Now, I would like to disable this highlighting (just like all the other ones) with :syntax off. It turns out that it disables my colorscheme, but not this specific highlight group.

I've tried to add those lines to my colorscheme file, instead of my .vimrc, but it doesn't work either. The ExtraWhitespace group is always highlighted, even with :syntax off. How can I disable it ?


I think there's a bug in vim/Neovim here.

I assume that the answer to my question would be the following one (suggested by D. Ben Knoble, thank you!):

function! TrailingSpaces()
   if exists("syntax_on")
      match ExtraWhitespace /\s\+$/
   else
      match NONE
   endif
endfunction
autocmd Syntax * call s:TrailingSpaces()

The highlight command should then be put in the theme file.

Or the following one should work too with the original code:

autocmd Syntax * if exists("syntax_on") | hi ExtraWhitespace ctermbg=red | else | hi clear ExtraWhitespace | endif

But in fact, none of them is working. It seems that the syntax event is never triggered.

The following code should do something, right?

function! Test()
   echo "This is a test."
endfunction
autocmd Syntax * call Test()

But nothing ever happens when I set :syntax off or :syntax on.

Tested with a clean vimrc, both with vim and Neovim (latest versions from the Arch Linux repos).

Am I missing something here? Could anyone confirm this?

  • 1
    match and syntax use different mechanisms. You can hi clear ExtraWhitespace I think – D. Ben Knoble Apr 17 '20 at 18:10
  • You're right ! hi clear ExtraWhitespace disables it manually... Is there a way to do it each time "syntax off" is set ? I've tried autocmd ColorScheme * hi clear ExtraWhitespace both in my .vimrc and my colorscheme files, but it has no effect at all... Thanx for your help ! – Dupond Apr 17 '20 at 19:53
  • there's an event for syntax you could try – D. Ben Knoble Apr 17 '20 at 22:01
  • I can't figure it out. I tried this in my theme file : autocmd Syntax * if exists("syntax_on") | hi ExtraWhitespace ctermbg=red | else | hi clear ExtraWhitespace | endif but it still has no effect... What am I doing wrong ? – Dupond Apr 18 '20 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.