I put the following lines from this page into my ~/.vimrc
to highlight trailing whitespace in red:
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()
I would like to have this be the default behavior, but be able to toggle this behavior on and off with something like \w. I found a code snippet from this answer that looks promising. After editing it I had something like this:
let g:toggleHighlightWhitespace = 1
function! ToggleHighlightWhitespace(...)
if a:0 == 1 "toggle behaviour
let g:toggleHighlightWhitespace = 1 - g:toggleHighlightWhitespace
endif
if g:toggleHighlightWhitespace == 1 "normal action, do the hi
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()
else
call clearmatches()
endif
endfunction
autocmd BufWinEnter * call ToggleHighlightWhitespace()
autocmd InsertEnter * call ToggleHighlightWhitespace()
autocmd InsertLeave * call ToggleHighlightWhitespace()
autocmd BufWinLeave * call ToggleHighlightWhitespace()
nnoremap <leader>w :call ToggleHighlightWhitespace(1)<cr>
With these lines, typing \w in normal mode gets rid of the highlighting, but as soon as I re-enter insert mode, the highlighting returns. How do I get the toggling to work?
EDIT
Thanks to help from Grochmal, my final snippet from ~/.vimrc
is shown below. I split the toggle function into a toggle function and a refresh function for clarity.
let g:toggleHighlightWhitespace = 1
function! ToggleHighlightWhitespace()
let g:toggleHighlightWhitespace = 1 - g:toggleHighlightWhitespace
call RefreshHighlightWhitespace()
endfunction
function! RefreshHighlightWhitespace()
if g:toggleHighlightWhitespace == 1 " normal action, do the hi
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
augroup HighLightWhitespace
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()
augroup END
else " clear whitespace highlighting
call clearmatches()
autocmd! HighLightWhitespace BufWinEnter
autocmd! HighLightWhitespace InsertEnter
autocmd! HighLightWhitespace InsertLeave
autocmd! HighLightWhitespace BufWinLeave
endif
endfunction
autocmd BufWinEnter * call RefreshHighlightWhitespace()
autocmd BufWinLeave * call RefreshHighlightWhitespace()
nnoremap <leader>w :call ToggleHighlightWhitespace()<cr>
:help :autocmd
: When your.vimrc
file is sourced twice, the autocommands will appear twice. To avoid this, put:autocmd!
in your.vimrc
file, before defining autocommands. See this answer to know how.