Vim - How to search and reaplace based on search [

2019-09-15 19:13发布

问题:

This question already has an answer here:

  • VIM - Replace based on a search regex 1 answer

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:

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.