I've only just found out about Vimscript's existence as of today, so forgive me if this question is more basic than it seems, but I've been struggling to find an answer to this question for the better part of a day.
I'm working on a file in vim where I have to do a large number of repeated actions. For instance, my file looks something like this:
firstname
secondname
thirdname
fourthname
fifthname
sixthname
...
The list keeps going, and what I'd like to do is append a fixed string to the end of each line such that each line ends with an increasing number, i.e.:
firstname = _first1
secondname = _first2
thirdname = _first3
fourthname = _first4
fifthname = _first5
sixthname = _first6
...
I know that I could write a short bash script to resolve this issue, but I've been enticed by the prospect of using vimscript to do it all within vim, and that's a skill I'd like to acquire. Thus far I've worked out a function as such to run in vim's command mode:
:let i=1 | g/name/s//\=i/ | let i=i+1
Which seems promising, as the result is most of the way to what I want:
first1
second2
third3
fourth4
fifth5
sixth6
...
But no matter how I've tried to modify it, I can't get it to print a string ( = _first) followed by the incremented number. For instance this...
:let i=1 | g/name/s//name = _first\=i/ | let i=i+1
Produces this.
firstname = _first=i
secondname = _first=i
thirdname = _first=i
fourthname = _first=i
fifthname = _first=i
sixthname = _first=i
...
and this...
:let i=1 | execute 'g/name/s//name = _first' . i . '/' | let i=i+1
Produces this.
firstname = _first1
secondname = _first1
thirdname = _first1
fourthname = _first1
fifthname = _first1
sixthname = _first1
...
I've tried other combinations, but at this point I'm pretty much stabbing in the dark. Everything I try seems to either get an incrementing variable with no string, or a string with a static variable. How could I do this with vimscript?
Edit:
I'm giving the correct answer to Ingo Karkat since it led directly to me finding how to resolve my issue. For posterity, here is the code I wound up using:
:let i=1 | 1156,1271g/$/s//\=' = _first' . i/ | let i=i+1
producing:
firstname = _first1
secondname = _first2
thirdname = _first3
fourthname = _first4
fifthname = _first5
sixthname = _first6
...
The only minor differences in my code (project specific) were that it operated only on lines 1156 through 1271, and it appended to the end of the line without replacing any text.