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?

  • 2
    Maybe nnoremap <CR> :nohl<CR><C-L>?
    – statox
    Sep 24 '15 at 20:31
  • @statox: thanks! what does <c-l> do?
    – Toothrot
    Sep 24 '15 at 20:35
  • It redraws the screen (: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
  • @statox: yes. except I discovered that multiple highlights are only showed after <cr>; but the mapping works with the 2nd <cr>.
    – Toothrot
    Sep 24 '15 at 20:48
  • I not sure of what 2nd <CR> you're talking about, now I have access to a Vim I just tested nnoremap <CR> :nohl<CR> (without redraw) and it seems to work properly.
    – statox
    Sep 24 '15 at 21:57

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.


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.

  • You could remap n with :nnoremap n n:nohl<CR> Dec 29 '15 at 1:05

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.