Reman
Reman

Reputation: 8109

How to print the output of python code in Vim buffer?

Example (in vim function):

python3 << endpython
import vim,sys,locale
locale.setlocale(locale.LC_ALL, vim.eval("Locale"))
for i in range(1,10,2):
    print(locale.format('%.2f', i))
endpython

The print command, prints the numbers as an echo message.
I would like to print the numbers at the top of the current buffer.
How can I do this?

Upvotes: 1

Views: 1089

Answers (2)

Roland Puntaier
Roland Puntaier

Reputation: 3511

If you have this Redir, you can recover already printed text with:

:Redir messages

Example:

:py3 print('print to stdout')
:Redir messages
/print to stdout

It will be at the end of the buffer that opens.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172570

You can access the current buffer and append after line 0 (= inserting at the top) via:

vim.current.buffer.append("example text", 0)

See :help python-buffer.

Upvotes: 3

Related Questions