Vim Tips Wiki
Advertisement
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>

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

If you want to clear the search pattern for current editing session, without turning syntax highlight off (i.e. so that you can search another pattern and see it highlighted immediately, w/o extra step of enabling hlsearch:

" clear search and clear the message - i.e. cls
:noremap <F6> :let @/=''<bar>:echo<CR>


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"


See also

References

Comments

Advertisement