How do I keep vim from highlighting matches after a replace?

I have set hlsearch in my ~/.vimrc. This is normally very useful, but it's annoying to have to do :noh after I do a replace like '<,'>s/$/',/ (which highlights the end of every line after I run it). How do I get vim to not highlight after replacing like this?

I'm on Ubuntu 9.04 with vim 7.2. I have the same problem on OS X 10.6 (also vim 7.2). Something that would work on both would be desireable.

UPDATE: All the answers I've received are just adding a key mapping to do :noh (or an equivalent). I really try to avoid custom key behaviors that I would really get used to, but then make me fail miserably without them (e.g. at a friend's computer, etc.). I would like vim to automatically do :noh after doing a replacement. That is, I only want hlsearch when using / or * (maybe a few other cases), but not other times.


Solution 1:

If you don't want to permanently disable, you can bind a key to toggle the highlight as suggested in this answer to a similar question on stackoverflow.

map  <F12> :set hls!<CR>
imap <F12> <ESC>:set hls!<CR>a
vmap <F12> <ESC>:set hls!<CR>gv

If you want to permanently disable the highlight:

11.1. After I searched for a text with a pattern, all the matched text
      stays highlighted. How do I turn off the highlighting
      temporarily/permanently?

The 'hlsearch' option controls whether all the matches for the last
searched pattern are highlighted or not. By default, this option is not
enabled. If this option is set in a system-wide vimrc file, then you can
turn off the search highlighting by using the following command:

    :set nohlsearch

(from the VIM FAQ)

Solution 2:

I have two different solutions, both pretty good, each with a nuanced different behavior. My favorite is option 2:

  1. I have the following 2 lines in my .vimrc:

    autocmd cursorhold * set nohlsearch
    autocmd cursormoved * set hlsearch
    

    The first one waits for a timeout (not configurable) if the cursor doesn't move and turns off hlsearch. The second one turns it back on if the cursor moves again.

    It's not a perfect solution, but it works for me (and will be close to perfect when the timeout is configurable -- for now, the timeout is tied to a builtin timer for vim.)

  2. Or you can try these lines:

    autocmd cursorhold * set nohlsearch
    noremap n :set hlsearch<cr>n
    noremap N :set hlsearch<cr>N
    noremap / :set hlsearch<cr>/
    noremap ? :set hlsearch<cr>?
    

This turns off hlsearch after a brief timeout of no cursor movement and turns it back on for the standard search commands. It doesn't cover all of the possibilities of turning hlsearch back on, but it's enough of a starter for proof of concept.

Solution 3:

I just type "/asdf" (assuming "asdf" is not in the file) to get rid of the highlighting. I know it's pretty simplistic, but it works for me.

Solution 4:

Put the following in your .vimrc file:

nnoremap <silent> <F11> :exe ":silent! normal /$^\r"<CR>

This will silently hide the highlighted search term but does not disable the :set hlsearch feature. It is activated again on the next search.