“脚本” vim 使用 python 具有更大的粒度

发布于 2024-12-10 18:42:03 字数 184 浏览 1 评论 0原文

我正在尝试编写一个可以自动化 vim 的 python 脚本,但是 python vim 界面并没有给我足够的能力来完成我需要的一切。我想与 vim 进行通信,就好像我的脚本是一个 tty (能够发出“可视模式”指令等)。就 vim 而言,我的脚本是人类运行 xterm(或其他)。可以在不使用 python 构建我自己的终端模拟器的情况下完成此操作吗?

I'm attempting to write a python script that can automate vim, but the python vim interface doesn't give me enough power to do everything I need. I want to communicate with vim as if my script were a tty (able to issue "visual mode" instructions etc). As far as vim is concerned, my script is a human running xterm (or whatever). Can this be done without building my own terminal emulator in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2024-12-17 18:42:03

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

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

All non-vimscript interfaces are cursed: the only way to communicate with vim (unless you want to edit/get contents of a buffer which is available using buffer object) are execute (vim.command(string) in python) and eval (vim.eval(string) in python), both requiring serializing arguments. If you want to just start visual mode use

vim.command("normal! V")

or

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

. But if you want, for example, to return some value to a caller function you will have to use

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文