Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim replacement issue

Tags:

vim

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?

like image 429
user1726366 Avatar asked Oct 19 '12 07:10

user1726366


People also ask

How do you get out of replace in vim?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match.

Why is vim opening in replace mode?

Seems to be an issue with utf-8 ambiguous characters and Windows cmd console. Flag t_u7 is set by default and so vim will request cursor position and get a bad reply from the ssh client. Workaround: Adding set t_u7= or set ambw=double to your vimrc should fix the problem.

How do you replace words in vim?

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.

How do I turn on line numbers in vim?

Vim can display line numbers in the left margin: Press ESC key. At the : prompt type the following command to run on line numbers: set number. To turn off line numbering, type the following command at the : prompt set nonumber.


1 Answers

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).

like image 171
January Avatar answered Sep 18 '22 18:09

January