Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #14 - Highlight all search pattern matches

Created: February 24, 2001 18:03 Complexity: basic Author: Yegappan Version: 5.7 Karma: 562/234 Imported from: Tip#14

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

Comments

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.

jsuntheimer--AT--neo.rr.com , May 26, 2001 23:09


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

harshad.joglekar--AT--wipro.com , August 19, 2001 9:25


I have this mapping in my .vimrc:

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

Very handy.


Anonymous , April 25, 2003 7:28


is there any way so that while only the word on under current curser position is highlighted ,

Anonymous , June 15, 2003 10:58


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...)

nivas--AT--vsnl.net , October 16, 2003 22:22


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?)

gene.rmspam.smith--AT--sea.rmspam.siemens.com , November 20, 2003 16:18


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.

egerlach--AT--canada.com , June 20, 2004 9:23


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.


awkay69--AT--hotmail.com , November 4, 2004 8:41


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

gdangit--AT--gmail.com , September 22, 2005 16:28


Advertisement