How to clear or disable search highlighting in Vim?
After performing a search in Vim, all occurrences of the search term are highlighted. How can I disable this highlighting? Currently, I’m searching for gibberish text that can’t be found to clear the highlights, but this isn’t ideal.
Is there a way to temporarily disable the search highlighting and then re-enable it when needed again?
To clear search highlighting in Vim, use the command :nohlsearch or its abbreviation :noh. To temporarily disable highlighting entirely, use :set nohlsearch and re-enable it later with :set hlsearch. You can also toggle highlighting on/off with :set hlsearch! or create keyboard shortcuts in your vimrc for faster access.
- Clearing Search Highlighting
- Temporarily Disabling Highlighting
- Permanent Configuration Options
- Advanced Techniques
- Common Issues and Solutions
Clearing Search Highlighting
When you perform a search in Vim using / or ?, all occurrences of the search term are highlighted by default. While this is useful for finding all instances, it can become distracting when you’re done searching.
The simplest way to clear the search highlighting is to use the :nohlsearch command or its abbreviation :noh:
:nohlsearch
:noh
These commands will clear the highlighting for the current search pattern without clearing the search history or changing any settings. The highlighting will reappear automatically if you perform another search.
You can also clear the search pattern entirely using:
:let @/ = ""
This clears the search register, which will remove both the highlighting and the stored search pattern.
Temporarily Disabling Highlighting
If you want to temporarily disable search highlighting and re-enable it later, you can use the hlsearch option:
:set nohlsearch
:set hlsearch
The first command disables highlighting, and the second command re-enables it. You can also use the toggle command:
:set hlsearch!
This command toggles the hlsearch option - if it’s ON, it turns it OFF, and vice versa.
For even faster access, you can create keyboard shortcuts in your vimrc file:
nnoremap <leader>h :set hlsearch!<CR>
nnoremap <leader>c :nohlsearch<CR>
These mappings allow you to toggle highlighting with <leader>h and clear highlighting with <leader>c.
Permanent Configuration Options
If you want to change the default behavior of search highlighting, you can modify your vimrc configuration file:
To disable highlighting by default:
set nohlsearch
To enable highlighting by default (the default setting):
set hlsearch
You can also make highlighting less obtrusive by changing the highlight group colors:
highlight Search ctermbg=gray ctermfg=black guibg=gray guifg=black
This makes the search highlighting less bright and more subtle.
Advanced Techniques
For more control over search highlighting, consider these advanced techniques:
- Automatic clearing after movement - You can set Vim to automatically clear highlighting when you move the cursor:
augroup auto_nohl
autocmd!
autocmd CursorMoved * :silent! nohlsearch
augroup END
- Conditional highlighting - You can create a function that toggles highlighting only when you’re actively searching:
function! ToggleSearchHighlight()
if &hlsearch
set nohlsearch
else
set hlsearch
endif
endfunction
nnoremap <leader>h :call ToggleSearchHighlight()<CR>
- Highlight current line only - If you want to highlight only the current search result:
augroup search_current
autocmd!
autocmd CmdlineEnter /,\? :set hlsearch
autocmd CmdlineLeave /,\? :set nohlsearch
augroup END
Common Issues and Solutions
Issue: Highlighting doesn’t clear after using :nohlsearch
Solution: Make sure you’re using the correct command. :nohlsearch clears highlighting, :set nohlsearch disables the option.
Issue: Highlighting reappears immediately after clearing
Solution: This happens when you have automatic highlighting enabled. Use :set nohlsearch to disable it entirely, or create a mapping to toggle it.
Issue: I want to keep the search pattern but remove highlighting
Solution: Use :nohlsearch which clears highlighting but preserves the search pattern.
Issue: I want to completely remove the search pattern
Solution: Use :let @/ = "" to clear the search register.
Conclusion
- Use
:nohlsearchor:nohto quickly clear search highlighting without changing settings - Use
:set nohlsearchand:set hlsearchto temporarily disable and re-enable highlighting - Create keyboard shortcuts in your vimrc for faster access to these commands
- Consider the automatic clearing technique if you want highlighting to disappear as soon as you move the cursor
- Remember that
:let @/ = ""completely clears the search pattern, not just the highlighting
The method you choose depends on your workflow and how frequently you need to clear or disable search highlighting in your Vim sessions.