Last Updated: February 25, 2016
·
1.197K
· kormie

Get rid of search highlighting with return

I have a very love hate relationship with highlighted searching in vi. I absolutely love how quickly I can find things with it, but if it stays on screen for any amount of time after I'm done using it it suddenly becomes the worst thing about the editor. So, I mapped highlight clearing to return:

nmap ^M :nohlsearch<CR>

I'm using a literal return so there's no interference with all the commands which use <CR>. I'd never had any problems with it until a few weeks ago when I started using Ack in vim. Since the Ack results pop up in a quick fix buffer, the intended behavior is to hit return (you can also use 'o' which makes sense but it's not nearly as satisfying). It turns out I can have my cake and eat it too. All you have to do is add an auto command that unmaps the custom binding for quickfix windows:

autocmd BufReadPost quickfix nmap <buffer> ^M <CR>

4 Responses
Add your response

For some reason the ^M didn't map for me, not sure why.
I ended up using:
nmap <CR> :nohlsearch<CR>

Thanks for that though, the lingering highlighting was driving me crazy! I would usually just do a search for 'asdfasdfasdf', heh :)

over 1 year ago ·

@kid-icarus Same here, had to use <CR>

Thanks for that, it was really annoying!

over 1 year ago ·

I think more elegant way is to set it on cursor hold.

autocmd cursorhold * set nohlsearch

over 1 year ago ·

@kid-icarus curious, did you use control-v control-m to insert the character ^M?

over 1 year ago ·