Highlight all search pattern matches
From Vim Tips Wiki
Tip 14 Previous Tip • Next Tip
Created: February 24, 2001 Complexity: basic Author: Yegappan Minimum version: 5.7 Karma: 562/234 Imported from: Tip#14
To highlight all search matches in a file, set the following option:
:set hlsearch
When this option is set, if you search for a pattern, all matches in the file will be highlighted in yellow.
To disable the highlighting temporarily, enter (this is a command, not an option):
:nohlsearch
This command removes the highlighting for the current search. The highlighting returns for the next search.
To disable highlighting completely, set the following option:
:set nohlsearch
[edit] Mappings
Here are some alternative mappings that you could use in your vimrc to simplify changing search highlights.
" Press Space to turn off highlighting and blank message. :noremap <silent> <Space> :silent noh<bar>:echo ""<CR> " Press Ctrl-N to turn off highlighting. :noremap <silent> <C-N> :silent noh<CR> " Toggle highlighting on/off. :noremap <F4> :set hls!<CR>
[edit] References
- :help 'hlsearch' Option to control search highlighting.
- :help :nohlsearch Command to temporarily remove search highlighting.
- :help :match You can highlight more than the search pattern.
[edit] Comments
With the following in vimrc, you can press Enter to clear search highlighting.
" Clear search highlight by hitting Enter. " The <tt>/<BS></tt> is a trick to clear the command line. " The final <CR> restores the standard behaviour so " pressing Enter moves to the next line. :nnoremap <CR> :nohlsearch<CR>/<BS><CR>
A hack to temporarily remove highlighting is to search for nonexistent text:
/xxxxx
You can use a less prominent highlight (for gvim):
:hi Search guibg=LightGreen or :hi Search guibg=LightBlue
Or you can use modify the colours for vim:
:hi Search ctermbg=LightGreen ctermfg=red term=bold cterm=bold
