开发者

Smart search/replace in Vim

开发者 https://www.devze.com 2023-01-04 00:06 出处:网络
I have a file with the following expressions: something[0] 开发者_如何转开发 Where instead of 0 there could be different numbers. I want to replace all these occurances with

I have a file with the following expressions:

something[0]

开发者_如何转开发

Where instead of 0 there could be different numbers. I want to replace all these occurances with

somethingElse0

Where the number should be the same as in the expression I replaced. How do I do that?


Use pattern grouping like this:

:%s/something\[\(\d\+\)\]/somethingElse\1/g


You should use a combination of match groups and character classes to accomplish this. Your example, if you mean replacing Something(numbers-here) with SomethingElse(same-numbers), is solved with the following command:

:%s/Something\(\d\+\)/SomethingElse\1/g

This finds all of the places in the file that have Something(a-bunch-of-digits), and replaces the Something with SomethingElse. The + means one or more digits; you could replace that with * if you wanted zero or more digits.

0

精彩评论

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