Say I have lines like this:
'alpha' 123
'beta' 678
'alpha' 998
'gamma' 212
And using the search & replace regex in VIM, turn it into this:
'alpha' 123 : alpha
'beta' 678 : beta
'alpha' 998 : alpha
'gamma' 212 : gamma
Basically, the search won't be replacing what it's searching for, but instead just using it for something else. In my head, this should work:
:g/'\(.*\)'/s/$/: \1/g
But that didn't do it. How do I not consume what I'm searching for but retain it for use?
The initial g
for the match does not capture for replacement; it is only used for grouping for the search. Instead use this, which is a little simpler too:
%s/'\(.*\)'.*/& : \1/
The &
replaces everything that was matched.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With