1

I want to replace the lines :

https://www.dailymotion.com/video/whatever_1 # The Serie - S2 E4 - Grin and Bear It
https://www.dailymotion.com/video/whatever_2 # The Serie - S2 E3 - Betty's Wait Problem
https://www.dailymotion.com/video/whatever_3 # The Serie - S1 E21 - Secretaries Day
...

by these lines :

https://www.dailymotion.com/video/whatever_1 # The Serie - S2 E4 - whatever_79
https://www.dailymotion.com/video/whatever_2 # The Serie - S2 E3 - whatever_78
https://www.dailymotion.com/video/whatever_3 # The Serie - S1 E21 - whatever_77
...

So I tried different combinations of the following vim command :

:let i=79 | g/^http/s/\(S[0-9]\+ E[0-9]\+\).*$/\1 - whatever_\=i/ | let i=i-1

but none has worked and I got this instead :

https://www.dailymotion.com/video/whatever_1 # The Serie - S2 E4 - whatever_=i
https://www.dailymotion.com/video/whatever_2 # The Serie - S2 E3 - whatever_=i
https://www.dailymotion.com/video/whatever_3 # The Serie - S1 E21 - whatever_=i

Can you help me ?

1
  • There's more info on \= here. And just for fun...you could have repaired the lines all ending in '=i' by doing a Ctrl+V select of the last column (cursor on firstline then <c-v>}k$) followed by :s//79/ | norm! gvojg<c-v><c-x> (For <c-v>, <c-x> here you should enter actual Ctrl+V/Ctrl+X chars).
    – B Layer
    Commented Feb 21, 2020 at 4:10

1 Answer 1

2

If you want to use \=, it needs to be at the start of the replacement, and the whole replacement expression becomes an expression.

You can use the submatch() function to access your backreferences, since \1 will not work from a \= replacement expression.

After you do that, all you need is " - whatever_".i to produce the replacement using a string concatenation.

Putting it all together:

:let i=79 | g/^http/s/\(S[0-9]\+ E[0-9]\+\).*$/\=submatch(1)." - whatever_".i/ | let i=i-1

It's easier to do that if you don't need the backreference. In this particular case, you can easily avoid the backreference by using \zs, which marks the start of the match, the part that actually gets replaced:

:let i=79 | g/^http/s/S[0-9]\+ E[0-9]\+\zs.*$/\=" - whatever_".i/ | let i=i-1
5
  • @fibranden I do need the \1, is there a function replacement for \1 which can be called after \= ?
    – SebMa
    Commented Feb 7, 2020 at 21:03
  • 1
    @SebMa yes: see :help submatch(
    – D. Ben Knoble
    Commented Feb 7, 2020 at 21:28
  • @D.BenKnoble Incorporated the reference to submatch(). Also expanded on \zs to explain it a little better. In this exact case here, you really don't need to use backreferences... But it's possible you do elsewhere, where you might want to reorder things, so good to include that information too.
    – filbranden
    Commented Feb 7, 2020 at 21:49
  • @filbranden Can you please also incorporate the submatch(1) solution in a second g//s/// vim command ?
    – SebMa
    Commented Feb 8, 2020 at 0:06
  • 1
    @SebMa done! Please take another look.
    – filbranden
    Commented Feb 8, 2020 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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