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
:nohlsearchsuppresses the current highlight without changing thehlsearchoption.- The next time you run a search (
/,?,*,#,n,N), highlights reappear automatically. - This is different from
:set nohlsearch, which would require:set hlsearchto 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
:nohto 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:nohlsearchto your<C-l>mapping. - The abbreviated form
:nohis commonly used in mappings because it is shorter.