vimtricks.wiki Concise Vim tricks, one at a time.

How do I clear search highlights without disabling hlsearch permanently?

Answer

:nohlsearch

Explanation

After a search in Vim, matched text is highlighted as long as hlsearch is enabled. While you could toggle the option off with :set nohlsearch, that permanently disables highlights for future searches. The :nohlsearch command (abbreviated :noh) temporarily clears highlights until the next search — the perfect balance between visibility when you need it and a clean buffer the rest of the time.

How it works

  • :nohlsearch suppresses the current highlight without changing the hlsearch option.
  • The next time you run a search (/, ?, *, #, n, N), highlights reappear automatically.
  • This is different from :set nohlsearch, which would require :set hlsearch to restore the feature.

Example

Search:   /function
Result:   every occurrence of "function" is highlighted

Run:      :noh
Result:   highlights disappear immediately

Next search:  /class
Result:   "class" matches are highlighted again

Tips

  • Map :noh to a convenient key to clear highlights with a single keystroke:
nnoremap <leader><space> :nohlsearch<CR>
  • You can also press <C-l> to redraw the screen, though this only clears highlights if you add :nohlsearch to your <C-l> mapping.
  • The abbreviated form :noh is commonly used in mappings because it is shorter.

Next

How do I visually select a double-quoted string including the quotes themselves?