I'm trying to define a command that can take a range and pass it to a function. This is what I thought I should have:
function! PrintGivenRange() range
echo "firstline ".a:firstline." lastline ".a:lastline
" Do some more things
endfunction
command! -range PassRange call PrintGivenRange()
However, it doesn't work that way, it seems to only pass the first line.
e.g.
:1,5PassRange "outputs firstline 1 lastline 1
:1,5call PrintGivenRange() "outputs firstline 1 lastline 5
" if you select lines in visual mode, same thing for both
I've read :help command-range
already but still haven't been able to figure this out. Am I supposed to pass the range in the prefix to the call
? How do I fix this?
You need to explicitly pass the range, try with:
command! -range PassRange <line1>,<line2>call PrintGivenRange()
If you want use the range of currently selected line in visual mode, you can use the below:
command! -range PassRange '<,'> call PrintGivenRange()
'<
: the first line of the current selected Visual area in the current buffer.'>
: the last line of the current selected Visual area in the current buffer.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With