I'm using variables for line numbers, and I want to delete a range of lines using these variables. How do I do that?

    let x = 5
    let y = 20
    " I want to delete the range of lines from x to y (i.e., 5-20)
    " I guessed at this, but it doesn't work:
    " E15: Invalid expression: ",ydelete"

Extra credit for providing reference(s) to the vim documentation that explains this. (I searched the docs for "variable" but didn't find what I needed)


1 Answer 1


Use the :execute command, e.g.,

:execute x "," y "delete"

:exe[cute] {expr1} ..
Executes the string that results from the evaluation of {expr1} as an Ex command. Multiple arguments are concatenated...
{expr1} is used as the processed command

[:help execute]

Edit: Per D Ben Knoble's comment, How do I use a variable or return value in option, command, or mapping? addresses this question.

Your Answer

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

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