将ex命令重定向到vim中的STDOUT [英] Redirect ex command to STDOUT in vim

查看:156
本文介绍了将ex命令重定向到vim中的STDOUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写命令以将vim的突出显示信息转储到STDOUT.我可以像这样成功地写入文件:

I'm trying to craft a command to dump vim's highlighting information to STDOUT. I can write successfully to a file like this:

vim +'redir >outfile' +'hi' +'redir END' +'q'

此后,outfile完全包含我想要的内容.但是我宁愿输出到STDOUT以便通过管道传递给将突出显示信息转换为CSS的命令.

After this, outfile contains exactly what I want. But I'd rather output to STDOUT to pipe to a command which converts that highlighting info to CSS.

此方法尝试将命令重定向到寄存器,写入当前缓冲区,然后将该输出发送到tee.

This approach tries to redirect the command to a register, write to the current buffer, then send that output to tee.

vim -E +'redir @a' +'silent! hi' +'redir END' +'put a' +'w !tee' +'q!' > outfile

这非常接近,但是会输出一个前导的寻呼消息(多出255行...),并在最后输出一些ANSI转义废话.设置nomore不会为我压缩消息.我希望将第一个命令中发送到outfile的确切输出发送到STDOUT.

This is pretty close, but outputs a leading paging message (255 more lines...) and some ANSI escape crap at the end. Setting nomore did not squelch the message for me. I'm looking to send the exact output sent to outfile in the first command to STDOUT.

推荐答案

Vim支持输出到stderr,我们可以将其重定向到stdout来解决您的问题.但是请小心,此功能仅用于调试目的,因此具有一些优势.这是您如何执行此操作的简短概述.

Vim supports outputting to stderr, which we can redirect to stdout to solve your problem. Careful though, this feature is intended only for debugging purposes, so it has a few edges. Here's a short sketch of how you could do this.

首先,我们将以 silent batch模式运行Vim,并通过-e -s标志(

First, we will be running Vim in silent or batch mode, which is enabled with the -e -s flags (:h -s-ex). We disable swap files -n because they will be a bother if we need to kill Vim when it gets stuck.

我们将-S用作脚本文件,而不是将Ex命令作为命令行参数传递.创建具有以下内容的文件hi.vim:

Instead of passing Ex commands as command line arguments we source a script file with -S. Create a file hi.vim with the following contents:

verbose highlight

要使Vim将:highlight消息输出到stderr,必须使用

:verbose .让我们看看目前为止:

:verbose is necessary to make Vim output the :highlight message to stderr. Let's see what we have so far:

$ vim -n -e -s -S hi.vim

请不要运行此命令,否则您将陷入无头Vim的黑暗之中!

Don't run this yet or you'll get stuck in the darkness of a headless Vim!

添加:quit命令并将stderr重定向到stdout:

Add a :quit command and redirect stderr to stdout:

$ vim -n -e -s -S hi.vim +quit 2>&1

Voilà!现在,将这些混乱内容按您的意愿发送到任何文件或实用程序中.

Voilà! Now pipe this mess into any file or utility at your heart's desire.

关于此主题,有一篇非常详尽的Wiki文章," Vim作为vimscript的系统解释器".

There's a very thorough wiki article on this topic, "Vim as a system interpreter for vimscript".

要点:由于Vim如何与终端环境交互,因此无法在批处理模式输出中写入正确的unix LF行尾.而是写出看起来像CRLF行尾的行尾.

Finer points: Due to how Vim interacts with the terminal environment it can't write proper unix LF line endings in batch mode output. Instead it writes line endings which look like CRLF line endings.

考虑添加一个过滤器以摆脱这些过滤器:

Consider adding a filter to get rid of those:

$ vim -n -e -s -S hi.vim +quit 2>&1 | tr -d '\r' | my-css-util


这回答了有关如何将ex命令重定向到vim中的STDOUT"的一般问题,但是由于-e -s标志引起的这种约束,它不一定能解决您的:hi问题:


This answers the general question of how to "Redirect ex command to STDOUT in vim", but it doesn't necessarily work for your :hi problem, because of this constraint caused by the -e -s flags:

'term'和$ TERM不使用.

'term' and $TERM are not used.

这篇关于将ex命令重定向到vim中的STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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