views:

131

answers:

3

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 //?

+3  A: 

//<CR> means repeat the search for the last pattern with no offset.

//e<CR> means repeat the search for the last pattern, but land on the end of the match.

n is the same as /<CR> in that it uses the last pattern and the last offset, however n keeps the last direction while / always finds the next match.

See :h last-pattern and :h search-offset for a thorough explanation of these commands and their options.

jleedev
So does `//` start searching from the top of the file then?
paxdiablo
No, it searches forward from where you are.
jleedev
Offset is (generally) where you are in the file, starting from the top. `//` will repeat the search from where you are, whereas `n` will repeat the search from where the last match was.By the way, you can use `//` in substitute and global commands too. Useful if you've got a particularly tricky bit of matching to do and you want to test it out with a regular search beforehand.
Alligator
+4  A: 

One of the nice features of // is that you can use it with the s command. So if you initially search for /Foo and then decides to replace that with Bar, you can do this without repeating the pattern. Just do :%s//Bar/g

Obviously this is a lot more useful if the pattern is a bit more complex.

Brian Rasmussen
+10  A: 

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.

paxdiablo
Perfect, solid answer! Thanks!
Chetan