Searching


Up to now we have reviewed many ways to navigate through a document, from simple cursor movements, to paging, to text objects. Vim also provides the ability to search a buffer for specific text or a pattern for desired text, then quickly navigating through matching text.

Searching forward

The first search operator we will look at it /, which searches the buffer forward from the current cursor location. Starting from the following buffer:

Initial Conditions
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
COMMANDTop1:1
/comp

Initiate the search by typing /, followed by the text you want to search for, then finally hit enter to start the search.

Search forward
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
NORMAL60%3:26
 

Vim found the text, and moved the cursor forward to the start of the first match. Since there are often multiple matches, Vim provides a shortcut command for repeating a search. To repeat the search, hit n.

Repeat the previous searchn
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
NORMAL80%4:27
 

Repeating the search moves the cursor to the location of the next matching text. The cursor is now at the last match, what happens if we repeat the search once more?

Repeat againn
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
NORMAL60%3:26
search hit BOTTOM, continuing at TOP

When Vim is not able to find matching text, it "wraps around" to the top of the buffer and continues the search. In this example there are only two matches in the document, so the cursor returns to the first match.

Searching backward

Searching backward uses the ? operator, but otherwise operates the same as searching forward:

Search backward
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
NORMAL60%3:14
 

The n also works when searching backward, and like most operators this shortcut accepts a count. To move the cursor to the "second next match", precede the operator with a count of 2:

Repeat search with count2n
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
NORMALTop1:17
 

As expected, this moves the cursor to the start of the first match. Now, suppose we moved the cursor too far, and wanted to jump to the second match. Vim has a second shortcut for repeating a search, but in the opposite direction: N.

Repeat in the opposite directionN
1.·Beautiful·is·better·than·ugly.
2.·Explicit·is·better·than·implicit.
3.·Simple·is·better·than·complex.
4.·Complex·is·better·than·complicated.
NORMAL40%2:16
 

Search for word under cursor

Vim provides two additional shortcuts, & and #, for searching in the forward and backward directions, respectively. These shortcuts initiate a search and populate the search pattern with the word that is currently under the cursor, so that the search executes by simply hitting <CR>.

Here is a quick summary of the search-related operators:

CommandAction
/{pattern}search forward for the Nth occurrence of {pattern}
?{pattern}search backward for the Nth previous occurrence of {pattern}
&repeat last :s
#search backward for the Nth occurrence of the ident under the cursor
nrepeat the latest '/' or '?' N times
Nrepeat the latest '/' or '?' N times in opposite direction

This section has introduced the basics of searching a buffer with simple patterns. Vim extends the searching function with powerful patterns that can pinpoint specific text, and also supports the ability to search and replace text that has been matched.