Search and replace without consuming what you are looking for

Say I have lines like this:

'alpha' 123 
'beta' 678
'alpha' 998
'gamma' 212

And using the search and replace regular expression in VIM, include it in the following:

'alpha' 123 : alpha
'beta' 678 : beta
'alpha' 998 : alpha
'gamma' 212 : gamma

Basically, a search will not replace what it is looking for, but instead just uses it for something else. In my head, this should work:

:g/'\(.*\)'/s/$/: \1/g

But this was not done. How do I not consume what I am looking for, but save it for use?

+5
source share
4 answers

You can do this directly in the substitute command using \zs, which is really useful for creating things like zero-width matches (which doesn't allow Consume).

:%s/'\(.*\)'.*\zs/ : \1

\zs . " ". , , .* , \zs , , .

: .

:h \zs

+1

g ; . , :

%s/'\(.*\)'.*/& : \1/

& , .

+5

:

:%s/\v'(\w*)'.*/\0 : \1

(, )

0

: ( vim, .)

1- :

qqyi'A : <esc>pjq

then x@q (x - )

0

All Articles