If I have a function that takes a range, how can I echo the return value of the function?
For example, the following does not work:
:echo 2,8call MyFunction()
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
I looked into this, and I could not find a direct way of doing it. However, one possibility is to create an auxiliary function, that is:
function! Range() range abort return RangeAux(a:firstline, a:lastline) endfunction function! RangeAux(lnum1, lnum2) abort echo a:lnum1 echo a:lnum2 endfunction 1,3call Range() call RangeAux(1,3)
@Karl's answer is pretty good but you can also use
echo execute("1,2call Range()")
You can even use variables for the range:
let l1=1 let l2=2 echo execute(l1 . "," . l2 . "call Range()")
call you are not able to get the return value of the function, regardless whether
range is used or not.
Aside from @Karl's answer, you can set some variable instead of using
return. Such as
function! Range() range abort let toreturn = 'my value' let s:retval = toreturn endfunction