【问题标题】:"scripting" vim using python with greater granularity“脚本”vim 使用更细粒度的 python
【发布时间】:2011-10-23 17:34:56
【问题描述】:

我正在尝试编写一个可以自动化 vim 的 python 脚本,但是 python vim 接口并没有给我足够的能力来做我需要的一切。我想与 vim 通信,就好像我的脚本是一个 tty(能够发出“可视模式”指令等)。就 vim 而言,我的脚本是一个人类运行 xterm (或其他)。不用在 python 中构建我自己的终端仿真器就可以做到这一点吗?

【问题讨论】:

  • 你不能从那个 python 界面做类似:exec "normal V2jx" 的事情吗?

标签: python linux vim scripting tty


【解决方案1】:

所有非 vimscript 接口都被诅咒:与 vim 通信的唯一方法(除非你想编辑/获取使用缓冲区对象可用的缓冲区内容)是execute(python 中的vim.command(string))和@ 987654323@(python 中的vim.eval(string)),都需要序列化参数。如果您只想启动可视模式,请使用

vim.command("normal! V")

vim.eval("feedkeys('V')")

。但是,例如,如果您想向调用者函数返回一些值,则必须使用

import json
# Some code that puts result into variable r
# This won't work if r contains non-unicode strings,
# you will have to write your own serializer in this case.
# As far as I know, it will also fail if some characters in the string
# can be represented only using surrogate pairs.
vim.command("let reply=".json.dumps(r))
# Now in the caller function variable l:reply is defined

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2012-02-20
    相关资源
    最近更新 更多