user1726366
user1726366

Reputation: 2416

vim replacement issue

I have some lines like below:

aaa
bbb
ccc
ddd

I want them to be changed like this:

aaa=$aaa
bbb=$bbb
ccc=$ccc
ddd=$ddd

so I use the following command to do it in vim, but I got an error

:s/\(\^*\)/\1=\$\1/

and I realized the \1 here could not be used twice, then how should I do this?

Upvotes: 5

Views: 91

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172598

When matching the entire contents of the line, you neither need the ^ anchor, nor the capture via \(...\). In the replacement, you can use \0 or shorter &. (Also, you don't need to escape the $ there.)

:%s/.*/&=$&

Upvotes: 4

January
January

Reputation: 17090

The back reference \1 can be used as many times as you wish, but you have another problem. Your regex should look like that:

:%s/^\(.*\)/\1=\$\1/

Explanation: the % tells vim to replace on all lines; ^ as a mark for the beginning of line should be the first character in your regex and should not be escaped. The .* means "any character any number of times". However, the original expression \(\^*\) will look for any number of repeats of the literal character ^ (including none).

Upvotes: 4

Related Questions