1

This following vim script calls a function that returns an array called lines:

call vimwiki#diary#generate_diary_section()

There seems to be a bug and nothing is done with the lines array—the content never gets added to the buffer. I have a two part question:

  1. How do I capture the array returned from the function? I tried let lines = call ... but that didn't work.

  2. How to I dump the lines in the array into the existing file?

1
  • The only time you use call is when you don't care about the function's return value. Since you're using the return value here you must leave it off (let foo = somefunc(...)).
    – B Layer
    Commented Jul 25, 2020 at 16:15

2 Answers 2

2

There are several ways to insert the return value of a function into a buffer, but the simplest (in my opinion) is to use the expression register:

In normal mode you can trigger the expression register with "=, and then you type an expression and hit enter (<CR>). Thiss can just be a function call, if you wish; then, press p or P to put, as usual. So, "=vimwiki#diary#generate_diary_section()<CR>p.

Alternatively, you can use the :put[!] command:

put =vimwiki#diary#generate_diary_section()

This works linewise.

As for why :call vimwiki#diary#generate_diary_section() doesn't do anything, it's because :call explicitly discards return values. You can :echo to see the value, or use :let to bind it to a variable. But if you just want to insert it directly, :put or p is the way to go.

1

Just remove call.
This answer did explain the call keyword in vim.

Your Answer

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

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