使用更大粒度的python“编写”vim脚本

2024-06-16 13:22:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个python脚本来自动执行vim,但是pythonvim接口没有给我足够的能力来完成我需要的所有事情。我想像我的脚本是tty一样与vim通信(能够发出“visualmode”指令等)。就vim而言,我的脚本是一个运行xterm(或其他任何东西)的人。不需要用python构建自己的终端仿真器就可以做到这一点吗?在


Tags: 脚本终端指令能力vim事情xtermtty
1条回答
网友
1楼 · 发布于 2024-06-16 13:22:50

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

vim.command("normal! V")

或者

^{pr2}$

一。但是,如果你想,例如,返回一些值给一个调用函数,你必须使用

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

相关问题 更多 >