Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Once you use sub-replace-special, introduced with \=, everything has to be a Vim expression; you cannot prepend / append literal replacement text. Therefore, your static text has to be encoded as Strings, and concatenated with .:

:let i=1 | g/name/s//\='_first' . i/ | let i=i+1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.