Toggling a match in vimrc

2019-06-23 18:36发布

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.

标签: linux vim vi
1条回答
Rolldiameter
2楼-- · 2019-06-23 19:33

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>
查看更多
登录 后发表回答