Technology
 

Highlight all search pattern matches

From Vim Tips Wiki

(Redirected from VimTip14)

Tip 14 Previous Next created February 24, 2001 · complexity basic · author Yegappan · version 5.7


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 using the Search highlight group, which defaults to adding a yellow background to the current highlighting. See :help hl-Search, or type :hi Search to see what color you have it set to. You can easily change the default highlighting with, for example, :hi Search guibg=LightBlue.

To disable the highlighting temporarily, enter (this is a command, not an option):

:nohlsearch

This command (which can be abbreviated to :noh) removes the highlighting for the current search. The highlighting returns for the next search.

If you do this often, put a mapping in your vimrc, like this:

" Press Space to turn off highlighting and clear any message already displayed.
:nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>

To disable highlighting completely, even after a subsequent search, use:

:set nohlsearch

If you want to be able to enable/disable highlighting quickly, you can map a key to toggle the hlsearch option:

" Press F4 to toggle highlighting on/off.
:noremap <F4> :set hls!<CR>

Highlighting can be enabled on Vim startup, when reading the viminfo file. Add the following to your vimrc if you want Vim to start with no search highlighting:

:set viminfo^=h

[edit] References

[edit] Comments