Vim Tips Wiki
m (Try category inside template)
(Move categories to tip template)
Line 10: Line 10:
 
|rating=5333/1765
 
|rating=5333/1765
 
|category1=Searching
 
|category1=Searching
  +
|category2=
 
}}
 
}}
 
In normal mode, move the cursor to any word. How do you search for the next occurrence of that word?
 
In normal mode, move the cursor to any word. How do you search for the next occurrence of that word?

Revision as of 10:54, 24 April 2008

Tip 1 Printable Monobook Previous 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

More searching

After searching, press n (next) to search again, or press N to search in the reverse direction.

Vim maintains a search history – see Using command-line 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; release all keys; hold down Ctrl and press 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.

See also

References

Comments

On UK keyboards, <Shift>-<3> works just like # for searching backwards. That's because <Shift>-<3> gives # on most other countries keyboards, although it gives the pound currency symbol on UK keyboards. Touch typists should appreciate this because it is the left-hand equivalent of how you type * with <Shift>-<8>, whereas the # on UK keyboards is way over next to the return key.

Highlight matches without moving

I find it very useful to highlight the word under the cursor like *, but without jumping directly to the next match.

This way, you can quickly see the highlights in the current page without scrolling anything. Then you can move to the first (ggn), last (GN), next (n) or previous (N) match as usual.

The basic command is:

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

And I have set it as follows in my vimrc:

map <F10> :set hls<CR>:let @/="<C-r><C-w>"<CR>
map <F11> :nohls<CR>

This way, F10 highlights and F11 unhighlights.

Here is another version that uses F10 to toggle search highlighting (F11 is not used).

map <F10> :set invhls<CR>:let @/="<C-r><C-w>"<CR>/<BS>

The 'inv' prefix on a boolean setting toggles it. The trailing '/<BS>' clears the command line.

Just position the cursor to an interesting word, press F10, and all occurrences of that word are highlighted. Use commands like n and N to search up and down. When you're done, press F10 again and the highlighting is off.