Vim Tips Wiki
(need the ":echo" to clear messages like "search hit BOTTOM")
(rough merge in of "Highlight matches" from VimTip1)
Line 47: Line 47:
 
:set viminfo^=h
 
:set viminfo^=h
 
</pre>
 
</pre>
  +
  +
==Highlight matches without moving==
  +
:'''Section moved here from [[VimTip1]]. Will merge material into this tip.'''
  +
It can be useful to highlight the word under the cursor like <tt>*</tt>, 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 (<tt>ggn</tt>), last (<tt>GN</tt>), next (<tt>n</tt>) or previous (<tt>N</tt>) match as usual.
  +
  +
The basic command is (type Ctrl-r then Ctrl-w to insert the current word):
  +
<pre>
  +
:let @/="<C-r><C-w>"<CR>
  +
</pre>
  +
  +
The following map uses F10 to highlight all occurrences of the current word, and F11 to unhighlight (put in your vimrc):
  +
<pre>
  +
nnoremap <F10> :set hls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>
  +
nnoremap <F11> :nohls<CR>
  +
</pre>
  +
It is functionally the same as doing <C-r><C-w> manually, but using "<cword>" avoids errors if the cursor is not over anything.
  +
Here is another version that uses F10 to toggle search highlighting (F11 is not used).
  +
<pre>
  +
nnoremap <silent> <F10> :set invhls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>
  +
</pre>
  +
  +
The 'inv' prefix on a boolean setting toggles it. <silent> doesn't echo the command on the command line
  +
  +
Position the cursor on an interesting word, then press F10 to highlight all occurrences of that word. Use commands like <tt>n</tt> and <tt>N</tt> to search up and down. When you're done, press F10 again to toggle highlighting off.
  +
  +
Here is how to do the same for visually selected text:
  +
<pre>
  +
function! <SID>FidgetWhitespace(pat)
  +
let pat = substitute(a:pat,'\_s\+$','\\s\\*', '')
  +
let pat = substitute(pat, '^\_s\+', '\\s\\*', '')
  +
return substitute(pat, '\_s\+', '\\_s\\+','g')
  +
endfunction
  +
vmap <silent><M-8> :<C-U>let @/="\\V<C-R>=escape(<SID>FidgetWhitespace(escape(@*,'\')),'\"')<CR>"<CR>
  +
</pre>
  +
  +
Replace <M-8> with the key of your choice. The function FidgetWhitespace() allows changes in whitespace for potential matches, so using the mapping while "foo bar" is selected will also highlight:
  +
<pre>
  +
"foo bar" and "foo
  +
bar"
  +
</pre>
  +
If you want to keep your visual selection, append 'gv' to that mapping.
   
 
==References==
 
==References==

Revision as of 04:24, 16 December 2009

Tip 14 Printable Monobook 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

Highlight matches without moving

Section moved here from VimTip1. Will merge material into this tip.

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 (ggn), last (GN), next (n) or previous (N) match as usual.

The basic command is (type Ctrl-r then Ctrl-w to insert the current word):

:let @/="<C-r><C-w>"<CR>

The following map uses F10 to highlight all occurrences of the current word, and F11 to unhighlight (put in your vimrc):

nnoremap <F10> :set hls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>
nnoremap <F11> :nohls<CR>

It is functionally the same as doing <C-r><C-w> manually, but using "<cword>" avoids errors if the cursor is not over anything. Here is another version that uses F10 to toggle search highlighting (F11 is not used).

nnoremap <silent> <F10> :set invhls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>

The 'inv' prefix on a boolean setting toggles it. <silent> doesn't echo the command on the command line

Position the cursor on an interesting word, then press F10 to highlight all occurrences of that word. Use commands like n and N to search up and down. When you're done, press F10 again to toggle highlighting off.

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

function! <SID>FidgetWhitespace(pat)
    let pat = substitute(a:pat,'\_s\+$','\\s\\*', '')
    let pat = substitute(pat, '^\_s\+', '\\s\\*', '')
    return    substitute(pat,  '\_s\+', '\\_s\\+','g')
endfunction
vmap <silent><M-8> :<C-U>let @/="\\V<C-R>=escape(<SID>FidgetWhitespace(escape(@*,'\')),'\"')<CR>"<CR>

Replace <M-8> with the key of your choice. The function FidgetWhitespace() allows changes in whitespace for potential matches, so using the mapping while "foo bar" is selected will also highlight:

"foo   bar" and "foo
bar"

If you want to keep your visual selection, append 'gv' to that mapping.

References

Comments