I find :set hlsearch useful while I am entering the pattern; but I want the effect of :noh as soon as I press enter. Is that possible?
3 Answers
From a excellent book -- "Practical Vim":
Typing
:noh<CR>to mute search highlighting is laborious. You can speed things up by creating a mapping such as this:nnoremap <silent> <C-l> :<C-u>nohlsearch<CR><C-l>Normally, clears and redraws the screen (see
:h CTRL-L). This mapping builds on top of the usual behavior by muting search highlighting.
You can add this line to you vimrc file. If you hit Ctrl+l, Vim will mute search highlighting and redraw the screen at the same time.
- 659
- 5
- 17
I think you can achieve what you want through the mapping
cnoremap <silent> <cr> <cr>:nohl<cr>
The above mapping should turn of highlighted searches after you have pressed enter to accept the search pattern.
Note, though, that the highlighting is turned back on with for instance n and N, i.e. when repeating the search. You can turn this off with maps, e.g.
nnoremap n n:nohl<cr>
nnoremap N N:nohl<cr>
Note: I found that the above cnoremap does not work as expected if you use the incsearch plugin, and I could not find why.
- 9,494
- 25
- 35
I have in my .vimrc
:nmap <leader>q :nohlsearch<CR>
which maps my leader (\ by default, but I remapped to ,) then q to remove the search highlighting. Not exactly what you're looking for, but it might be worthwhile.
That, and several other nice bits came from http://dougblack.io/words/a-good-vimrc.html#search, though he uses space to disable the highlight.
- 111
- 2
nnoremap <CR> :nohl<CR><C-L>? – statox Sep 24 '15 at 20:31:h CTRL-L). Does the mapping works properly? I havent tested it so I'd like to have a confirmation before I make it an answer :-) – statox Sep 24 '15 at 20:37<cr>; but the mapping works with the 2nd<cr>. – Toothrot Sep 24 '15 at 20:48<CR>you're talking about, now I have access to a Vim I just testednnoremap <CR> :nohl<CR>(without redraw) and it seems to work properly. – statox Sep 24 '15 at 21:57