Add the following to your .vimrc:
let g:toggleHighlight = 0 function! ToggleHighlight(...) if a:0 == 1 "toggle behaviour let g:toggleHighlight = 1 - g:toggleHighlight endif if g:toggleHighlight == 0 "normal action, do the hi silent! exe printf('match Search /\<%s\>/', expand('<cword>')) else "do whatever you need to clear the matches "or nothing at all, since you are not printing the matches endif endfunction autocmd CursorMoved * call ToggleHighlight() map <F8> :call ToggleHighlight(1)<CR>
The idea is that if you call a function with an argument, it changes the behavior for printing / without printing. The autocommand simply uses the last parameter, because the function is called there without an argument.
source share