Vim - How to search and reaplace based on search [

2019-09-15 19:08发布

This question already has an answer here:

What i would like to do :

Every time i find something based on s[w|l].*[0-9]\.\* replace the end of that string\search .* with %s/\.\*/\\\\\.\.\*/g

Already tried with standard search and replace, but couldn't do it.

Thanks

1条回答
来,给爷笑一个
2楼-- · 2019-09-15 19:31

Just wrap your search pattern in escaped parentheses \(\), and use a backreference \1 in the replacement string:

:%s/\(s[w|l].*[0-9]\)\.\*/\1\\\\.*/g

You can have several \(\) subexpressions in your search string, and can use them both in the search string itself and in the replacement string via \1, \2, ... . Subexpressions are simply numbered by order of occurence in the search string.

查看更多
登录 后发表回答