2

I was wondering how it is that I would place VIMScript variable values in my regex range and also in the replacement portion of my regex?

function! GoogleFormsStep(startnum, endnum)

    let x = startnum
    let line = 1

    while x <= endnum

      line, line s/$/x/g " What do I do here?

      let x = x + 1
      let line = line + 1
    endwhile

endfunction
2
  • 1
    A visual selection over the desired lines, followed by :'<,'>s/$/\=line('.')/ (where '<,'> are automatically inserted) would seem to accomplish what's in your code (if it helps somewhere).
    – VanLaser
    Aug 18, 2015 at 17:20
  • @VanLaser That worked great! How about you put it as the answer so I can select it.
    – leeand00
    Aug 19, 2016 at 14:01

1 Answer 1

3

Use :execute and concatenation:

let startline = 2
let endline = 56
let searchpattern = "foo"
let replacepattern = "bar"

execute startline . "," . endline . "s/" . searchpattern ."/" . replacepattern . "/g"

Alternative:

let cmd = startline . "," . endline . "s/" . searchpattern ."/" . replacepattern . "/g"
execute cmd
1
  • Ah I didn't think this would involve concatenation, I guess execute is a bit like eval in Javascript for ':' commands. Thank you.
    – leeand00
    Aug 18, 2015 at 17:11

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.