开发者

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

开发者 https://www.devze.com 2023-01-20 13:02 出处:网络
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

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消