Can I turn autorun on or off?

I have in my .vimrc to hightlight all the words that match one of the current cursor

autocmd CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>')) 

But sometimes it’s a little annoying, so I would like to display a key to turn it on or off, for example. <F10>

How can i do this?

+4
source share
2 answers

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.

+1
source

Clear the auto command and remove the selection:

 nmap <f8> :autocmd! CursorMoved<cr> :call clearmatches()<cr> 

and enable it again with another key:

 nmap <f9> :autocmd CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>'))<cr> 
+1
source

All Articles