How do I clear search highlighting in Vim?
Answer
:noh
Explanation
The :noh (short for :nohlsearch) command clears the highlighting from the last search pattern. When you search with /pattern or *, Vim highlights all matches in the file. These highlights persist until you run :noh or search for something else.
How it works
:nohturns off the current search highlighting- It does not disable the
hlsearchoption — the next time you search, highlights will appear again - It only affects the display, not the search register — you can still press
nto jump to the next match
Example
You search for /error and every occurrence of "error" in the file is highlighted in yellow. After reviewing the results, the highlights are distracting. Type :noh<CR> and all the highlights disappear.
The next time you press n or /, highlighting will come back for the new or continued search.
Tips
- Many users map this to a key for quick access. A popular mapping:
nnoremap <Esc><Esc> :nohlsearch<CR>
- Another common mapping uses
<leader>:
nnoremap <leader><space> :nohlsearch<CR>
- Use
:set nohlsearchto disable search highlighting permanently (not recommended — you lose a useful visual cue) - Use
:set hlsearchto re-enable it if you turned it off - The full command is
:nohlsearchbut:nohis the shortest unambiguous abbreviation