Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to duplicate text in each line and add equal sign and prefix to it

Tags:

vim

This is my text:

xxx
yyy
zzz

I would like it to be this text instead:

xxx = C.xxx
yyy = C.yyy
zzz = C.zzz

Is this possible to do in Vim?

like image 848
Darius Kucinskas Avatar asked Aug 17 '11 19:08

Darius Kucinskas


3 Answers

You can also use the simpler form:

:%s/.*/& = C.&
like image 143
sidyll Avatar answered Nov 14 '22 08:11

sidyll


Type this:

:%s/\(.*\)/\1 = c.\1/g

Breakdown:

  • % - work on all lines
  • \(.*\) - capture all the characters in a group ("group 1")
  • s/PATTERN/REPLACEMENT/g - do a string substitution
  • \1 in the replace pattern - refer to the matched group
like image 27
orip Avatar answered Nov 14 '22 08:11

orip


Select the text, then press : and type

s/\(.*\)/\1 = C.\1/
like image 1
Karoly Horvath Avatar answered Nov 14 '22 06:11

Karoly Horvath