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()
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
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?