Toggling a match in vimrc

天大地大妈咪最大 提交于 2019-12-23 12:27:02

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/11269066/toggling-a-match-in-vimrc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!