Clear search highlight

4.5
(34)

Vim’s search highlighting feature, set hlsearch, is a popular toggle that will instantly highlight all the matches for your current pattern match. But once you’ve found what you were looking for, you may be annoyed that the highlight persists.

If you’re like me, you might just search for an unlikely pattern by randomly mashing the keys: /akljshdakdja which will clear any present highlights because the pattern will not be found.

But, of course, Vim has a better way. The official way to clear the highlight for the last search is with the :nohlsearch command which you can abbreviate to just :noh. After completing your search, just type :noh and your highlights will disappear until the next search.

You could also add a key mapping for that if you use it often enough. I recommend just using Tim Pope’s sensible.vim plugin, which provides a nearly universally accepted set of Vim default configurations. One of the included mappings is designed just for this use case:

" Use <C-L> to clear the highlighting of :set hlsearch.
if maparg('<C-L>', 'n') ==# ''
  nnoremap <silent> <C-L> :nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L>
endif

With sensible.vim installed, you can simply press <ctrl-l> to clear the search highlight until the next search.

How useful was this tip?

Average rating 4.5 / 5. Vote count: 34

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

94 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

3 thoughts on “Clear search highlight”

  1. Good tip! It’s very handy to create a fast mapping for t his. I use the comma as my leader and comma-dot for :noh

  2. In my neovim config I have this:

    map(
    “n”,
    “”,
    [[ (&hls && v:hlsearch ? ‘:nohls’ : ‘:set hls’).”\n” redraw]],
    { silent = true, expr = true }
    )

    This way I can toggle hls as soon as I want

    1. “`vim
      map(
      “n”,
      “”,
      [[ (&hls && v:hlsearch ? ‘:nohls’ : ‘:set hls’).”\n” redraw]],
      { silent = true, expr = true }
      )
      “`

      Trying to post decent code to show my Ctrl-l properly

Leave a reply

Your email address will not be published. Required fields are marked *