Vim 执行命令并通过 stdout 发送缓冲区 [英] Vim execute a command and send out buffer over stdout

查看:30
本文介绍了Vim 执行命令并通过 stdout 发送缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是如何以一种有趣的方式自动化 vim:

Here's how you can automate vim in an interesting way:

vim -c '0,$d | r source.txt | 1d | w | q' dest.txt

这里使用 vim ex 命令擦除 dest.txt,将 source.txt 读入缓冲区,擦除第一行(结束由于 r 的工作方式,作为一个空行),写入文件(dest.txt),然后退出.

This uses vim ex commands to erase dest.txt, read source.txt into the buffer, erase the first line (which ends up as a blank line due to the way r works), write to the file (dest.txt), and then quit.

这(据我所知)跳过了整个 vim 终端 UI 的加载,并且在概念上有点像拥有一个 vimscript 解释器.

This (as far as I can tell) skips the entire vim terminal UI from loading and is conceptually a little like having a vimscript interpreter.

现在我希望能够更进一步地滥用 vim 的功能:我想要一个脚本来查看当前编辑的打开文件的更改(如交互式自动化 shell 脚本的一部分)存在于 vim *.swp 交换文件中,通过 vim 的 recover 命令应用更改,然后获取输出.

Now I'd love to be able to take this just one little step further to abuse the capabilities of vim: I want a script to peer at the currently edited changes of an opened file (as part of an interactive automation shell script) which exist in the vim *.swp swapfiles, apply the changes through vim's recover command, and then obtain the output.

当然,使用实际文件将非常有用,例如orig_file.txt 正在另一个终端的 vim 中编辑;我的脚本可以在检测到交换文件更改的每个点执行此操作:

Of course it would be perfectly serviceable to use an actual file, e.g. orig_file.txt is being edited in vim in another terminal; my script could do this at each point that the swapfile is detected to change:

cp orig_file.txt orig_file_ephemeral.txt
cp .orig_file.txt.swp .orig_file.txt_ephemeral.txt.swp
vim -c 'recover | w | q'

此时orig_file_ephemeral.txt 应包含来自其他正在编辑的进程的 vim 缓冲区的内容,我们无需与该进程进行任何直接交互即可获得此数据.哪个很整洁.

At this point orig_file_ephemeral.txt shall contain the content of the vim buffer from the other process in which editing is taking place, and we obtained this data without requiring any direct interaction with said process. Which is neat.

当然,出于实际目的,这样做可能更有意义,只需让主要 vim 参与该过程即可.它将脚本的功能拆分为 vim 的配置,这是一个缺点,但它在概念和计算上会更直接,因为它已经具有可用于编写的缓冲区内容,并且这样做应该很简单因为我相信存在一个我们可以使用的自动命令(尽管该自动命令是否在保存交换文件之前运行还有待观察).

Of course for practical purposes it would probably make more sense to do exactly that, and just have the primary vim participate in the process. It would be splitting the functionality for the script out into the configuration of vim, which is a downside, but it would be more straightforward conceptually and computationally as it already has the buffer contents readily available for writing, and it should be straightforward to do so as I believe there exists an autocommand we can use (though whether that autocommand is run prior to saving the swapfile or not remains to be seen).

无论如何,为了完整起见,我很想知道是否存在一个 ex 命令来将内容写入 vim 的 STDOUT.或者,如果这甚至有意义.

Either way, for the sake of completeness I'm curious to know if there exists an ex command to write stuff to the STDOUT of vim. Or if this even makes any sense.

我认为这可能没有意义,因为 STDOUT 必然是实际的终端,例如它是 vim 将其 UI 和缓冲区以及所有内容的视图"发送到终端的地方.因此,例如,如果任何 vim -c 'vimscript commands' 命令产生 vim 错误,我将看到 vim 的终端输出以通过 STDOUT 显示这些错误.

I think it perhaps makes no sense as STDOUT is bound to be the actual terminal, e.g. it is where vim sends out its "view" of its UI and the buffer, and everything, to the terminal. So that for example if any of the vim -c 'vimscript commands' commands produce vim errors, I'll be seeing vim's terminal output to display these errors over STDOUT.

因此,可能只能使用文件.但也许有一些疯狂的事情,比如 !tee/dev/fd/3 我可以做吗?

Therefore it may only be practical to use a file. But maybe there's some kind of craziness like !tee /dev/fd/3 I could do?

此外,这种迂回的方法还有一个问题,就是vim在大约一秒钟的亮红色背景文本中出现Warning: Original file may have been changed错误,这是肯定是由于重命名文件.我可以通过在子目录中执行此工作同时保持文件名相同来解决这个问题.

In addition, there is a wrinkle with this roundabout approach, which is that vim presents a Warning: Original file may have been changed error in bright red background text for about a second, and this is surely due to renaming the file. I can likely work around that by doing this work inside of a sub-directory while keeping the filenames identical.

推荐答案

那是 p 命令(以及 grep 中的 p来自):

That's the p command (and where the p in grep comes from):

ex -sc '%p|q' file

有点像 cat 文件.

这篇关于Vim 执行命令并通过 stdout 发送缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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