Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the search command `//` work in Vim?

After searching for something, if you hit //, you seem to get the next result. How is this different from n? How should you use it? What does //e match, and what other options are there for //?

like image 633
Chetan Avatar asked Oct 21 '10 05:10

Chetan


1 Answers

The search command is of the following format:

/pattern/offset<cr>

If the pattern part is left out, the search looks for the last pattern that was searched for. If the offset is left out, no offset is applied. The offset is basically what to do to the cursor once you've found your pattern item.

Most vi users are familiar with the variation without an offset, /pax<cr> and the repeat last search, /<cr>, which is equivalent to n.

In your specific examples, //<cr> is the same as /<cr> and it means repeat the last search and apply no offset.

On the other hand, //e<cr> means to repeat the last search and move the cursor to the end of the found item. The offsets are:

[num]         [num] lines downwards, in column 1
+[num]        [num] lines downwards, in column 1
-[num]        [num] lines upwards, in column 1
e[+num]       [num] characters to the right of the end of the match
e[-num]       [num] characters to the left of the end of the match
s[+num]       [num] characters to the right of the start of the match
s[-num]       [num] characters to the left of the start of the match
b[+num]       [num] identical to s[+num] above (mnemonic: begin)
b[-num]       [num] identical to s[-num] above (mnemonic: begin)
;{pattern}    perform another search, see |//;|

A plus or minus without a num uses 1.

like image 156
paxdiablo Avatar answered Oct 16 '22 21:10

paxdiablo