1

I am apparently not getting a return value from execute():

function! Example()
    return 5
endfunction

:echo execute("call Example()")

Just results in a blank line. The 5 is not printed. Why is this?

3

From the documentation of execute, i.e. :h execute():

execute({command} [, {silent}])
Execute {command} and capture its output.
If {command} is a String, returns {command} output.
If {command} is a List, returns concatenated outputs.

execute() returns the output of whatever is executed, not the return value of any function that may (or may not) be executed.

As an example,

echo execute('echo "test"')

will echo "test", while

echo execute('call SomeFunc(...)')

will only output whatever is echoed inside the function SomeFunc. The return value is not relevant here.

2

this is because call Example() alone wouldn't print anything. You need :echo execute("echo Example()") for execute() to print anything.

0

If your goal is just to print the return value of the function, you don't need the execute or the call:

:echo Example()

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.