Vim Tips Wiki
(→‎Highlight matches without moving: changed to use <cword> (avoids error E348); added method for doing the same in visual mode)
Line 84: Line 84:
 
</pre>
 
</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 match "foo bar" and "foo
+
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" (separated by a newline). If you want to keep your visual selection, append 'gv' to that mapping.
 
bar" (separated by a newline). If you want to keep your visual selection, append 'gv' to that mapping.
   

Revision as of 08:00, 2 December 2008

Tip 1 Printable Monobook Next

created February 24, 2001 · complexity basic · author Scott+Yegappan · version 5.7


In normal mode, move the cursor to any word. How do you search for the next occurrence of that word?

Press the * key to search forwards for the current word, or press # to search backwards.

Using * or # searches for the exact word at the cursor (searching for rain would not find rainbow).

Use g* or g# if you don't want to search for the exact word.

Using the mouse

With the proper settings, you can search for an exact word using the mouse.

Shift-LeftClick a word to search forwards, or Shift-RightClick to search backwards.

This needs a GUI version of Vim (gvim), or a console Vim that accepts a mouse. You may need the following line in your vimrc to enable mouse searches:

:set mousemodel=extend

In gvim, click the Edit menu, then Global Settings, then the "tear off" bar at the top. That will show a floating Global Settings menu with useful Toggle Pattern Highlight and Toggle Ignore-case commands.

More searching

After searching, press n for the next match, or N to search in the reverse direction.

Vim maintains a search history. Type / or ? and use the arrow up/down keys to recall previous search patterns. You can edit a pattern, and press Enter to search for something different.

Suppose the cursor is on a word, and you want to search for a similar word.

Press / then <C-r><C-w> to copy the current word to the command line. You can now edit the search pattern and press Enter. Use <C-r><C-w> for a <cword>, or <C-r><C-a> for a <cWORD>.

<C-r><C-w> is Control-R then Control-W (hold down Ctrl and press r then w).

After searching, use <C-o> to jump back to your previous position (then <C-i> will jump forwards).

After searching, an empty search pattern will repeat the last search. This works with /, :s and :g.

So, after searching for a word, use :%s//new/g to change all occurrences to 'new', or :g/ to list all lines containing the word. See Substitute last search.

You can enter a count before a search. For example 3/pattern will search for the third occurrence of pattern, and 3* will search for the third occurrence of the current word.

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 (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 <F10> :set invhls<CR>:exec "let @/='\\<".expand("<cword>")."\\>'"<CR>/<BS>

The 'inv' prefix on a boolean setting toggles it. The trailing /<BS> clears 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" (separated by a newline). If you want to keep your visual selection, append 'gv' to that mapping.

Show the next match while entering a search

To move the cursor to the matched string, while typing the search pattern, set the following option in your vimrc:

:set incsearch

Complete the search by pressing Enter, or cancel the search by pressing Escape. When typing the search pattern, press Ctrl-L (:help c_CTRL-L) to insert the next character from the match or press Ctrl-R Ctrl-W (:help c_CTRL-R_CTRL-F) to complete the current matching word.

See also

References

Comments

On UK keyboards, <Shift-3> produces ₤ but works just like # for searching backwards.