将文本字符串从 python-vim 脚本发送到 tmux 窗格 [英] Send literal string from python-vim script to a tmux pane

查看:31
本文介绍了将文本字符串从 python-vim 脚本发送到 tmux 窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下方式同时使用 Vim (8.0) 和 tmux (2.3):在 tmux 会话中,我将一个窗口拆分为 2 个窗格,一个窗格在 Vim 中打开了一些文本文件,另一个窗格中有一些程序我想向其发送文本行.一个典型的用例是将行从 Python 脚本发送到在另一个窗格中运行的 IPython 会话.

I am using Vim (8.0) and tmux (2.3) together in the following way: In a tmux session I have a window split to 2 panes, one pane has some text file open in Vim, the other pane has some program to which I want to send lines of text. A typical use case is sending lines from a Python script to IPython session running in the other pane.

我是通过一个使用 python 的 Vim 脚本来实现的,代码片段示例(假设目标 tmux 窗格为 0):

I am doing this by a Vim script which uses python, code snippet example (assuming the target tmux pane is 0):

py import vim
python << endpython
cmd = "print 1+2"
vim_cmd = "silent !tmux send-keys -t 0 -l \"" + cmd + "\"" # -l for literal
vim.command(vim_cmd)
endpython

这很好用,除非 cmd 有一些必须转义的字符,比如 %#$ 等. cmd 变量是从 Vim 中打开的文本文件中的当前行读取的,因此我可以执行类似 cmd = cmd.replace('%','\%') 等,但这有两个缺点:首先,我不知道所有必须转义的 vim 字符,所以到目前为止一直在反复试验,其次,字符 " 没有正确转义 - 在 Vim 得到的字符串中," 就消失了,即使我做了 cmd = cmd.replace('"', '\"').那么,有没有一种通用的方法可以告诉 Vim 不要解释任何内容,只需获取一个原始字符串并按原样发送?如果不是,为什么 " 没有正确转义?

This works well, except for when cmd has some characters which has to be escaped, like %, #, $, etc. The cmd variable is read from the current line in the text file opened in Vim, so I can do something like cmd = cmd.replace('%', '\%') etc., but this has 2 disadvantages: first, I don't know all the vim characters which have to be escaped, so it has been trial and error up until now, and second, the characters " is not escaped properly - that is in the string Vim gets, the " just disappears, even if I do cmd = cmd.replace('"', '\"'). So, is there a general way to tell Vim to not interpret anything, just get a raw string and send it as is? If not, why is the " not escaped properly?

推荐答案

Vimscript

您正在寻找 shellescape() 函数.如果你使用 :! Vim 命令,{special} 参数需要为 1.

Vimscript

You're looking for the shellescape() function. If you use the :! Vim command, the {special} argument needs to be 1.

silent execute '!tmux send-keys -t 0 -l ' . shellescape(cmd, 1)

但是由于您对(显示)shell 输出不感兴趣,并且不需要与启动的脚本进行交互,所以我将从 :! 切换到较低级别的 system() 命令.

But as you're not interested in (displaying) the shell output, and do not need to interact with the launched script, I would switch from :! to the lower-level system() command.

call system('tmux send-keys -t 0 -l ' . shellescape(cmd))

Python

使用 Python(而不是纯 Vimscript)没有任何好处(至少在您问题中的小片段中).相反,如果您在 Vimscript 表达式中嵌入 Python cmd 变量,现在您还需要一个 Python 函数将值转义为 Vimscript 字符串(类似于 '%s'" % str(cmd).replace("'", "''"))). 或者,您可以在 Vim 变量中维护该值,并通过 vim.vars 从 Python 访问它.

Python

The use of Python (instead of pure Vimscript) doesn't have any benefits (at least in the small snippet in your question). Instead, if you embed the Python cmd variable in a Vimscript expression, now you also need a Python function to escape the value as a Vimscript string (something like '%s'" % str(cmd).replace("'", "''"))). Alternatively, you could maintain the value in a Vim variable, and access it from Python via vim.vars.

这篇关于将文本字符串从 python-vim 脚本发送到 tmux 窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆