Vim Tips Wiki
m (Highlighting all the search pattern matches moved to Highlight all search pattern matches: Page moved by JohnBot to improve title)
Tag: rollback
(40 intermediate revisions by 15 users not shown)
Line 1: Line 1:
  +
{{TipImported
__NOTOC__
 
{{Tip
 
 
|id=14
 
|id=14
  +
|previous=12
|title=Highlighting all the search pattern matches
 
  +
|next=15
|created=February 24, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=Yegappan
 
|author=Yegappan
|version=5.7
+
|version=6.0
 
|rating=562/234
 
|rating=562/234
 
|category1=Searching
|text=
 
  +
|category2=
 
}}
 
}}
  +
When searching, it is often helpful to highlight all search hits (in a program, for example, that allows you to quickly see all occurrences of a variable). This tip shows how search highlighting is controlled, and has a method to highlight searches ''without'' moving.
   
To highlight all search matches in a file, set the following option:
+
==Highlighting search matches==
  +
To highlight all search matches, set the following option:
 
  +
<pre>
:set hlsearch
+
:set hlsearch
  +
</pre>
   
  +
With the defaults, setting this option causes all text matching the current search to be highlighted using the <code>Search</code> highlight group, which adds a yellow background to the current highlighting. See {{help|hl-Search}}, or type <code>:hi Search</code> to see what color you have it set to. You can easily change the default highlighting with, for example, <code>:hi Search guibg=LightBlue</code>.
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):
 
To disable the highlighting temporarily, enter (this is a command, not an option):
  +
<pre>
 
:nohlsearch
  +
</pre>
   
 
This command (which can be abbreviated to <code>:noh</code>) removes the highlighting for the current search. The highlighting returns for the next search.
:nohlsearch
 
   
  +
If you do this often, put a mapping in your [[vimrc]], like this:
This command removes the highlighting for the current search. The highlighting returns for the next search.
 
  +
<pre>
 
" Press Space to turn off highlighting and clear any message already displayed.
 
:nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>
  +
</pre>
   
To disable highlighting completely, set the following option (this is the default):
+
To disable highlighting completely, even after a subsequent search, use:
  +
<pre>
 
:set nohlsearch
  +
</pre>
   
  +
If you want to be able to enable/disable highlighting quickly, you can map a key to [[Managing boolean options|toggle the hlsearch option]]:
:set nohlsearch
 
  +
<pre>
  +
" Press F4 to toggle highlighting on/off, and show current value.
 
:noremap <F4> :set hlsearch! hlsearch?<CR>
  +
</pre>
   
  +
Or, press return to temporarily get out of the highlighted search.
==Mappings==
 
  +
<pre>
Here are some alternative mappings that you could use in your vimrc file to simplify changing search highlights.
 
  +
:nnoremap <CR> :nohlsearch<CR><CR>
  +
</pre>
   
  +
  +
  +
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:
 
<pre>
 
<pre>
  +
:set viminfo^=h
" Press Space to turn off highlighting and blank message.
 
  +
</pre>
:noremap <silent> <Space> :silent noh<bar>:echo ""<CR>
 
   
  +
==Highlight matches without moving==
" Press Ctrl-N to turn off highlighting.
 
  +
It can be useful to highlight the word under the cursor like <code>*</code>, but ''without'' jumping to the next match. Then you can see the search highlights on the current screen, without any scrolling. Move to the first match (<code>ggn</code>), last (<code>GN</code>), next (<code>n</code>) or previous (<code>N</code>) as usual.
:noremap <silent> <C-N> :silent noh<CR>
 
   
  +
One procedure would be to type <code>*``</code> ([[VimTip1|search]] for next occurrence of word, then jump back to the original position). Following is another procedure that won't flicker the screen when the search would require scrolling.
" Toggle highlighting on/off.
 
  +
:noremap <F4> :set hls!<CR>
 
  +
The technique is to assign the wanted pattern to the search register (<code>@/</code>), and to set the <code>'hlsearch'</code> option (abbreviated as <code>'hls'</code>). For example, these commands highlight every whole word matching "the":
  +
<pre>
  +
:let @/='\<the\>'
  +
:set hls
 
</pre>
 
</pre>
  +
  +
The following map uses the above technique so that pressing F8 will highlight all occurrences of the current word:
  +
<pre>
  +
:nnoremap <F8> :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
  +
</pre>
  +
  +
Here is how to do the same for visually selected text:
  +
<pre>
  +
set guioptions+=a
  +
function! MakePattern(text)
  +
let pat = escape(a:text, '\')
  +
let pat = substitute(pat, '\_s\+$', '\\s\\*', '')
  +
let pat = substitute(pat, '^\_s\+', '\\s\\*', '')
  +
let pat = substitute(pat, '\_s\+', '\\_s\\+', 'g')
  +
return '\\V' . escape(pat, '\"')
  +
endfunction
  +
vnoremap <silent> <F8> :<C-U>let @/="<C-R>=MakePattern(@*)<CR>"<CR>:set hls<CR>
  +
</pre>
  +
  +
The script starts by adding the <code>'a'</code> flag to <code>'guioptions'</code> so that visually selecting text automatically places the text in the clipboard (register <code>*</code>) – that only works on some systems ({{help|"*}}). The function replaces all whitespace strings (space, tab, newline) with a pattern that finds any amount of whitespace. For example, selecting "foo bar" and pressing F8 will highlight each of the two occurrences in:
  +
<pre>
  +
"foo bar" and "foo
  +
bar"
  +
</pre>
  +
  +
Another approach is to use the following to map the Enter key (<code><CR></code>) so that pressing Enter toggles highlighting for the current word on and off:
  +
<pre>
  +
let g:highlighting = 0
  +
function! Highlighting()
  +
if g:highlighting == 1 && @/ =~ '^\\<'.expand('<cword>').'\\>$'
  +
let g:highlighting = 0
  +
return ":silent nohlsearch\<CR>"
  +
endif
  +
let @/ = '\<'.expand('<cword>').'\>'
 
let g:highlighting = 1
  +
return ":silent set hlsearch\<CR>"
  +
endfunction
  +
nnoremap <silent> <expr> <CR> Highlighting()
  +
</pre>
  +
  +
After sourcing the above, move the cursor to a word then press Enter to highlight all occurrences of that word (the cursor is not moved). Press Enter again to clear the highlighting. It works by setting the search register so pressing <code>n</code> or <code>N</code> will search forwards or backwards for the word. The <code><expr></code> mapping (see {{help|:map-<expr>}}) means that the text returned by <code>Highlighting()</code> is executed as a command. That is necessary because commands that affect the current search highlighting are ignored when executed inside a function. The global variable <code>g:highlighting</code> is used to track whether highlighting is active as there appears to be no way to detect that using Vim script. The operator <code>=~</code> is used to check whether the contents of the search register (<code>@/</code>) matches the pattern to search for the exact current word; it uses a regular expression for the match so that the current setting of <code>'ignorecase'</code> is used (when set, highlighting a word like "example" will also highlight "Example").
  +
  +
==See also==
  +
*[[VimTip1|Searching]]
  +
*[[VimTip171|Search for visually selected text]]
  +
*[[VimTip1572|Highlight multiple words]]
  +
*[[VimTip572|Auto highlight current word when idle]]
   
 
==References==
 
==References==
*{{help|'hlsearch'}} Option to control search highlighting.
+
*{{help|'hlsearch'}} option to control search highlighting
*{{help|:nohlsearch}} Temporarily remove search highlighting.
+
*{{help|:nohlsearch}} command to temporarily remove search highlighting
  +
*{{help|'viminfo'}} include <code>h</code> to disable the effect of <code>'hlsearch'</code>
*{{help|:match}} You can highlight more than the search pattern.
 
  +
*{{help|'highlight'}} <code>l:Search</code> means the <code>Search</code> highlight group is used for matches
 
*{{help|:match}} you can highlight more than the search pattern
   
 
==Comments==
 
==Comments==
  +
I like this no-flicker find/highlight a lot. I used this one but wished it put my cword into search history so /<Up> would recall it.
A hack to temporarily remove highlighting is to search for nonexistent text:
 
  +
/xxxxx
 
  +
:nnoremap <F8> :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>
  +
  +
So I discovered and tried histadd and it worked!?!
   
  +
:nnoremap <F8> :let curwd='\\\<<C-R>=expand("<cword>")<CR>\\\>'<CR> :let @/=curwd<CR>:call histadd("search", curwd)<CR>:set hls<CR>
----
 
You can use a less prominent highlight (for gvim):
 
:hi Search guibg=LightGreen
 
or
 
:hi Search guibg=LightBlue
 
   
  +
Thank you so much for the hint.
----
 
[[Category:Searching]]
 

Revision as of 15:14, 22 August 2013

Tip 14 Printable Monobook Previous Next

created 2001 · complexity basic · author Yegappan · version 6.0


When searching, it is often helpful to highlight all search hits (in a program, for example, that allows you to quickly see all occurrences of a variable). This tip shows how search highlighting is controlled, and has a method to highlight searches without moving.

Highlighting search matches

To highlight all search matches, set the following option:

:set hlsearch

With the defaults, setting this option causes all text matching the current search to be highlighted using the Search highlight group, which adds 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, and show current value.
:noremap <F4> :set hlsearch! hlsearch?<CR>

Or, press return to temporarily get out of the highlighted search.

:nnoremap <CR> :nohlsearch<CR><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

Highlight matches without moving

It can be useful to highlight the word under the cursor like *, but without jumping to the next match. Then you can see the search highlights on the current screen, without any scrolling. Move to the first match (ggn), last (GN), next (n) or previous (N) as usual.

One procedure would be to type *`` (search for next occurrence of word, then jump back to the original position). Following is another procedure that won't flicker the screen when the search would require scrolling.

The technique is to assign the wanted pattern to the search register (@/), and to set the 'hlsearch' option (abbreviated as 'hls'). For example, these commands highlight every whole word matching "the":

:let @/='\<the\>'
:set hls

The following map uses the above technique so that pressing F8 will highlight all occurrences of the current word:

:nnoremap <F8> :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>

Here is how to do the same for visually selected text:

set guioptions+=a
function! MakePattern(text)
  let pat = escape(a:text, '\')
  let pat = substitute(pat, '\_s\+$', '\\s\\*', '')
  let pat = substitute(pat, '^\_s\+', '\\s\\*', '')
  let pat = substitute(pat, '\_s\+',  '\\_s\\+', 'g')
  return '\\V' . escape(pat, '\"')
endfunction
vnoremap <silent> <F8> :<C-U>let @/="<C-R>=MakePattern(@*)<CR>"<CR>:set hls<CR>

The script starts by adding the 'a' flag to 'guioptions' so that visually selecting text automatically places the text in the clipboard (register *) – that only works on some systems (:help "*). The function replaces all whitespace strings (space, tab, newline) with a pattern that finds any amount of whitespace. For example, selecting "foo bar" and pressing F8 will highlight each of the two occurrences in:

"foo    bar" and "foo
  bar"

Another approach is to use the following to map the Enter key (<CR>) so that pressing Enter toggles highlighting for the current word on and off:

let g:highlighting = 0
function! Highlighting()
  if g:highlighting == 1 && @/ =~ '^\\<'.expand('<cword>').'\\>$'
    let g:highlighting = 0
    return ":silent nohlsearch\<CR>"
  endif
  let @/ = '\<'.expand('<cword>').'\>'
  let g:highlighting = 1
  return ":silent set hlsearch\<CR>"
endfunction
nnoremap <silent> <expr> <CR> Highlighting()

After sourcing the above, move the cursor to a word then press Enter to highlight all occurrences of that word (the cursor is not moved). Press Enter again to clear the highlighting. It works by setting the search register so pressing n or N will search forwards or backwards for the word. The <expr> mapping (see :help :map-<expr>) means that the text returned by Highlighting() is executed as a command. That is necessary because commands that affect the current search highlighting are ignored when executed inside a function. The global variable g:highlighting is used to track whether highlighting is active as there appears to be no way to detect that using Vim script. The operator =~ is used to check whether the contents of the search register (@/) matches the pattern to search for the exact current word; it uses a regular expression for the match so that the current setting of 'ignorecase' is used (when set, highlighting a word like "example" will also highlight "Example").

See also

References

Comments

I like this no-flicker find/highlight a lot. I used this one but wished it put my cword into search history so /<Up> would recall it.

nnoremap <F8> :let @/='\<<C-R>=expand("<cword>")<CR>\>'<CR>:set hls<CR>

So I discovered and tried histadd and it worked!?!

nnoremap <F8> :let curwd='\\\<<C-R>=expand("<cword>")<CR>\\\>'<CR> :let @/=curwd<CR>:call histadd("search", curwd)<CR>:set hls<CR>

Thank you so much for the hint.