1

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
  • 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 '15 at 17:20
  • @VanLaser That worked great! How about you put it as the answer so I can select it. – leeand00 Aug 19 '16 at 14:01
1

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
  • 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 '15 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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