Often I want to search and replace in vim like this:
:%s/<search_pattern>/<search_pattern>_foo/g
Is there a way to make this syntax more efficient so that I can reference <search_pattern> in the replace value? I’m thinking it would be something similar to back referencing by group name/number, but I can’t find any docs on this.
Use the
&character to represent the matched pattern in the replacement:Alternately you can use
\0, which is a back-reference for the whole matched pattern, and so may be easier to remember:See
:help sub-replace-specialAnd, thanks to @kev, you can force an empty pattern at the end of your search string using
\zs, to be replaced by __foo_ in the replacement:This means: replace the end of the search string with __foo_.