【问题标题】:How to print the output of python code in Vim buffer?如何在 Vim 缓冲区中打印 python 代码的输出?
【发布时间】:2016-02-08 13:52:33
【问题描述】:

示例(在 vim 函数中):

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

打印命令,将数字打印为回显消息。
我想打印当前缓冲区顶部的数字。
我怎样才能做到这一点?

【问题讨论】:

    标签: python python-3.x vim printing


    【解决方案1】:

    您可以通过以下方式访问当前缓冲区并在第 0 行之后追加(= 在顶部插入):

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

    :help python-buffer

    【讨论】:

    • 非常感谢英戈
    • 顶部的“0”数字与列表相反,但没有它也可以正常工作。最好仅将“0”用于第一项。 :)
    • 如果你从一个空缓冲区开始,0 确实是可选的。感谢您接受我的回答!
    【解决方案2】:

    如果你有这个Redir, 您可以使用以下方法恢复已打印的文本:

    :Redir messages

    例子:

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

    它将位于打开的缓冲区的末尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 2012-08-12
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多