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?

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
StevieD
  • 1,492
  • 1
  • 16
  • 24
  • 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 Jul 25 '20 at 16:15

2 Answers2

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.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
1

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

Tin Thai
  • 21
  • 3