Vim repeats pattern in replace command

I have the following text:

Lang1
Lang2
Lang3

Now I want to replace all the text in each line with a pattern like:

{ DisplayName: DispName(Lang1), Value: Lang1 },
{ DisplayName: DispName(Lang1), Value: Lang2 },
{ DisplayName: DispName(Lang1), Value: Lang3 },

Now, how can I achieve this by substituting the vim command, I can get: :%s/*$/\{\ DisplayName\:\ DispName\(Lang1\)\,\ Value\:\ Lang1\}\,/to replace each line Lang1, and then replace the individual 1s with 2, 3, 4, etc. But I do not want for this. I want one alternative team to select a pattern and repeat it. How to achieve this?

+4
source share
1 answer

You can use:

:%s/.*/{ DisplayName: DispName(&), Value: &},/

Here &is the back link to the full string using .*. This will replace the given content with this content:

{ DisplayName: DispName(Lang1), Value: Lang1},
{ DisplayName: DispName(Lang2), Value: Lang2},
{ DisplayName: DispName(Lang3), Value: Lang3},

, .*:

^Lang[0-9]*$
+5

Source: https://habr.com/ru/post/1681478/


All Articles