Vim Tips Wiki
Tip 1 Printable Monobook Next

created 2001 · complexity basic · version 6.0

This tip shows how to search using Vim, including use of * (the super star) to search for the current word. Search options are described, and the see also section links to other useful searching tips.

Basic searching[]

In normal mode you can search forwards by pressing / (or <kDivide>) 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. Type ggn to jump to the first match, or GN to jump to the last.

Note: ggn skips first match if it is at row 1 column 1. Type Gn to jump to the real first match. For this, 'wrapscan' must be on (default).

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 * (also <kMultiply>, <S-LeftMouse>) or # (also <S-RightMouse>) 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 highlight matches).

A search can include an offset to position the cursor. For example, searching with /green positions the cursor at the beginning of the next "green", while /green/e positions the cursor at the end of the next "green". :help search-offset

A search pattern can include characters with codes specified in decimal or hex. For example, searching with /\%d65 or with /\%x41 both find 'A' (character decimal 65 = hex 41), and searching with /\%d8211 or with /\%u2013 both find '' (a Unicode en dash). :help /\%d

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.

Other search options[]

By default, the 'wrapscan' option is on, which means that when "search next" reaches end of file, it wraps around to the beginning, and when "search previous" reaches the beginning, it wraps around to the end.

These examples show how to set 'wrapscan' (abbreviated as 'ws'):

:set nowrapscan        " do not wrap around
:set wrapscan          " wrap around
:set wrapscan!         " toggle wrap around on/off
:set ws! ws?           " toggle and show value

By default, search hits may occur in lines at the top or bottom of the window. The 'scrolloff' option controls whether context lines will be visible above and below the line containing the search hit.

If your text is folded, you probably want folds to automatically open to reveal search hits. To achieve that, the 'foldopen' option should include "search" (check by entering :set fdo?). :help 'foldopen' Conversely, this option can be used to Search only in unfolded text.

See also[]

Searching in multiple files
Finding a file from its name



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