5

Here is an example that I am working with. It is intended to add a boilerplate to a file, and then comment out those lines using Vim BlockComment() plugin function. The goal is to mark the line number before I read from the file and after I have completed reading from the file, so that I can comment out the range of lines just inserted.

However, I am having a time figuring out what the syntax should be to indicate that range. The commented line below is my attempt to call a function with a given range using variables. The commented part has a syntax error, but, if I provide a hard-coded range as shown below, the script works. How do we put my range in as a variable in this case?

function! AddBoilerPlate()
    let s:beginLine = line(".")
    r /Users/danieljbrieckjr/myBolierPlate.txt
    exe "normal! joDate Created: " . strftime("%B %d, %Y")
    exe "normal! oLast Modified: " . strftime("%B %d, %Y")

    let s:endLine = line(".")

"--------------------------------------------------
"     s:beginLine, s:endLine call Comment()
"-------------------------------------------------- 

    1,3 call Comment()

endfunction
ib.
  • 27,830
  • 11
  • 80
  • 100
MrDaniel
  • 583
  • 1
  • 7
  • 19

1 Answers1

12

One can prepare a string containing the target command, and then use :execute to run it:

:exe s:beginLine.','.s:endLine 'call Comment()'
ib.
  • 27,830
  • 11
  • 80
  • 100
  • What if you need the return value of Comment()? – Stephen Talley Nov 23 '20 at 22:23
  • @StephenTalley: As far as I know, it is not possible. The simplest workaround is to create a helper function that takes the range line numbers as regular arguments, and call that the regular way. – ib. Nov 25 '20 at 23:02