Vim Tips Wiki
Advertisement
Tip 1 Printable Monobook Next

created 2001 · complexity basic · version 6.0


This tip shows how to search using Vim. The see also section links to other useful searching tips.

Basic searching

In normal mode you can search forwards by pressing / then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press n to search forwards for the next occurrence, or N to search backwards.

Search backwards by pressing ? then typing your search pattern. Pressing n searches in the same direction (backwards), while N searches in the opposite direction (forwards).

Searching for the current word

In normal mode, move the cursor to any word. Press * to search forwards for the next occurrence of that 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 appropriate 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

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 Ctrl-r then Ctrl-w to copy the current word to the command line. You can now edit the search pattern and press Enter. Use Ctrl-r Ctrl-w for a <cword>, or Ctrl-r Ctrl-a for a <cWORD>.

After searching, press Ctrl-o to jump back to your previous position (then Ctrl-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.

You can highlight all search matches (and quickly turn highlighting off), and you can use a mapping to highlight all occurrences of the current word without moving (see here).

Case sensitivity

By default, searching is case sensitive (searching for "the" will not find "The").

With the following in your vimrc (or entered via a toggle mapping), searching is not case sensitive:

set ignorecase

Now the command /the will find "the" or "The" or "THE" etc. You can use \c to force a pattern to be case insensitive, or \C to force a pattern to be case sensitive. For example, the search /the\c is always case insensitive, and /the\C is always case sensitive, regardless of the 'ignorecase' option.

If 'ignorecase' is on, you may also want:

set smartcase

When 'ignorecase' and 'smartcase' are both on, if a pattern contains an uppercase letter, it is case sensitive, otherwise, it is not. For example, /The would find only "The", while /the would find "the" or "The" etc.

The 'smartcase' option only applies to search patterns that you type; it does not apply to * or # or gd. If you press * to search for a word, you can make 'smartcase' apply by pressing / then up arrow then Enter (to repeat the search from history).

When programming, there is generally no reason to want 'smartcase' to apply when you press *. For other situations, use:

nnoremap * /\<<C-R>=expand('<cword>')<CR>\><CR>
nnoremap # ?\<<C-R>=expand('<cword>')<CR>\><CR>

With these mappings, if 'smartcase' is on and you press * while on the word "The", you will only find "The" (case sensitive), but if you press * while on the word "the", the search will not be case sensitive.

The mapping for * uses / to start a search; the pattern begins with \< and ends with \> so only whole words are found; <C-R>= inserts the expression register to evaluate expand('<cword>') which inserts the current word (similar to Ctrl-R Ctrl-W but avoiding an error when used on a blank line).

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 by pressing Esc. 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.


Name change?

I have been irritated at how we don't have many basic how to tips, so I have reworked this in an attempt to show how to search while being fairly brief. I am also irritated that Google searches do not rate our wiki very highly. As an experiment, I have put "how to search using Vim" in the lead and I plan to try Google in a couple of weeks. There is a problem with the meta name="description" in the html source – I'll worry about that later.

I am also thinking of renaming this to "Searching" (currently, "Searching" and "Search" are redirects to "The super star"). I've been reluctant to muck about with this well-known tip, but I think renaming it (and having "The super star" redirect to "Searching") would help its Google rank, as well as make more sense to newcomers. Any thoughts? JohnBeckett 08:48, December 16, 2009 (UTC)

Advertisement