Hi, Meino Christian Cramer wrote: > > I am searching for a way to switch "hlsearch" half on (or half > off...). > > Normally hlsearch is nice when searching regulary for a keyword or > phrase. > > On the other side it highlight half of my text, if I do things like > > y/<space> > > . Is there a way to sitch off hlsearch when using y and other action > which are not "searching for something" actions from the users point > of view ?
you can achieve this with two mappings that toggle the state of the 'hlsearch' option depending on which type of search you are about to execute. After you have typed "y" VIM is in operator-pending mode which has its own set of mappings. At this point you want to turn 'hlsearch' off. Normally this can be done with "set nohls" in command-line mode but you can't switch there from operator-pending mode like with "<c-o>" from insert mode. So you need a little trick -- evaluate the expression register only for making use of a side effect. For this you need a helper function which executes a command and returns an empty string. The empty string can then be "inserted" into the right hand side of the mapping without causing any harm. function! Execute(command) execute a:command return '' endfunction onoremap / /<c-r>=Execute('set nohls')<cr> "Regular" searches are started from normal mode. At this point you want to turn 'hlsearch' on again. For this you need a normal mode mapping: nnoremap / /<c-r>=Execute('set hls')<cr> Note that there is (at least) one potential problem with this: by doing "y/anything" the last search pattern is set to "anything". Executing ":set hls" after that will cause "anything" to be highlighted -- not the search pattern of your last "regular" search. Regards, Jürgen -- Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us. (Calvin)