sponsor Vim development Vim logo Vim Book Ad

basic Tip #14: Highlighting all the search pattern matches

 tip karma   Rating 796/340, Viewed by 23257 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 24, 2001 18:03      complexity:   basic
author:   Yegappan      as of Vim:   5.7


To highlight all the search pattern matches in a file set the
following option:

        :set hlsearch

After this option is set, if you search for a pattern, all the
matches in the file will be highlighted in yellow.

To disable the highlighting temporarily, use the command

        :nohlsearch

This command will remove the highlighting for the current search.
The highlighting will come back for the next search.

To disable the highlighting completely, set the following option:

        :set nohlsearch

By default, the hlsearch option is turned off.

To get more help on this option, use

:help 'hlsearch'
:help :nohlsearch

 rate this tip  Life Changing Helpful Unfulfilling 

<< Incremental search | Displaying status line always >>

Additional Notes

[email protected], May 26, 2001 23:09
Another quick way to remove the highlighting of the last search without disabling highlighting is to do a search for a nonsense string

  /asdfasdf

When vim cant find the search string in the current buffer, all highlighting is removed.  It's ugly, quick, and easily remembered.
[email protected], August 19, 2001 9:25
I have found that the default hi-light color for search matches is rather flagrant (Yellow for gvim). If you set it to some pleasant color, you don't have to turn off "hlsearch" all the time, or search for an empty string.

Try this,
:hi Search guibg=LightGreen
or
:hi Search guibg=LightBlue

regards,
Harshad
Anonymous, April 25, 2003 7:28
I have this mapping in my .vimrc:

map <F1> :let &hlsearch;=!&hlsearch;<CR>

Very handy.
Anonymous, June 15, 2003 10:58
is there any way so that  while only the word on under current curser position
is highlighted ,
[email protected], October 16, 2003 22:22
add following line to the _vimrc file :
hi search guibg=LightBlue

that should take care of using pleasant colours as opposed to 'bright yellow' (?!? I mean seriously...)
[email protected], November 20, 2003 16:18
The tip:

map <F1> :let &hlsearch;=!&hlsearch;<CR>

Is good, but watch out for a invisible space at end after <CR> which makes the cursor  move right on each F1 hit. I guess this is possible because vim does not trim spaces at end of lines. (How do you keep this from calling help in insert mode?)
[email protected], June 20, 2004 9:23
A more succint way of doing the mapping above is:

map <F1> :set hlsearch!<CR>

Also, to map F1 in insert mode, use imap, as such:

imap <F1> <ESC>:set hlsearch!<CR>a

The ESC is very important, or else you'll just insert the thing into your document.  The 'a' on the end puts you back into insert mode at the same place after the command.
[email protected], November 4, 2004 8:41
I've found that I love the highlighting, until I decide to move the cursor and begin editing...So, I just added these to .vimrc:

:map j <Down>:nohlsearch<CR>
:map k <Up>:nohlsearch<CR>
:map h <Left>:nohlsearch<CR>
:map l <Right>:nohlsearch<CR>

This, of course, assumes you use the vi cursor keys h,j,k,l...you could also remap the arrow keys if you are into using things so far away from the normal typing position.

[email protected], September 22, 2005 16:28
I really like highlighting searched words, and I would like to have the option to highlight more than one search at once (in different colors)

This would look something like reading a page from Google cache, where different terms are different colors.

Another idea would be to fade the color of the older searches each time so the brightest was the latest highlighted.

I prefer the former approach, so that the words that are highlighted in blue now will be hilighted in blue after another search as well.    Easier on my brain.

Anyone know if this is supported via some plugin or script?

Thanks!

Dan
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo