Toggling a match in vimrc

linux, vi, vim

Solution

Similar idea to kev's but toggles the hilighting instead of the match

let s:hilightws = 1

hi ExtraWhitespace cterm=NONE ctermbg=green ctermfg=green guibg=green guifg=green
hi link MaybeExtraWhitespace ExtraWhitespace
match MaybeExtraWhitespace /\s\+$/    

fun ToggleWhitespaceMatching()
  if s:hilightws
    hi link MaybeExtraWhitespace NONE
    let s:hilightws = 0
  else
    hi link MaybeExtraWhitespace ExtraWhitespace
    let s:hilightws = 1
  endif
endfun

map <F8> :call ToggleWhitespaceMatching()<CR>

Problem

I have the following lines in my vimrc file: ``` hi ExtraWhitespace cterm=NONE ctermbg=green ctermfg=green guibg=green guifg=green match ExtraWhitespace /\s\+$\|\t/ ``` These lines will highlight all lines with no characters other than spaces as well as any tab. I would like to add a function that will toggle highlighting the whitespace. ``` map <F8> :call ToggleWhitespaceMatching()<cr> ``` I have tried to write my own, but have not been able to get it working. Could someone please suggest a function to accomplish this. Also, i would like the matching to be on, by default.

Original source