如何将ex命令输出重定向到当前缓冲区或文件? [英] How to redirect ex command output into current buffer or file?
问题描述
如何将ex命令的输出重定向或管道到我当前的缓冲区或文件中?
例如,我想读取所有寄存器到当前缓冲区中,在ex模式下使用:registers
:redir> name_of_registers_file
:registers
:redir END
:r name_of_registers_file
:help redir
最后一个命令非常有用,因为有很多重定向选项:变量,寄存器,
我仍然发现很奇怪,并且恼人的是,它使用END,但是因为一切可以跟随 redir
必须以非字词开头,至少不含糊。
PS AFAIK这种情况下)没有办法直接读入缓冲区:你必须先将它存储在寄存器或变量。
PPS 如果你想使用变量-maybe来封装它在一个函数,避免clobbering寄存器或全局变量 - 你将不得不将写入变量的多行字符串转换为列表。 EG :call append('。',split(variable_you_redirected_to,\\\
))
否则(如果你只是 append('。',var)
)你最终得到^ @的(nulls)而不是换行符,因为这是vimscript用来表示字符串变量中的换行符。
>:as @Bill Odom提到,使用:put = variable_you_redirected_to
比复杂的 append()
。谢谢,Bill!
PPPS
片段,以便使此内容更方便。它声明一个函数 Redir(command,target)
和命令 R
。
该命令将最后一系列非空格字符解析为重定向目标,并将其传递给函数,该函数执行将命令输出重定向到重定向目标的样板。
该命令是 R
之后,最后一个空格之前的所有内容。
p>
将vim缓冲区列表存储在buffer_list.txt中
:R ls> buffer_list.txt
Store
:R调用FunctionBeingDebugged()@>
在调用的函数发出的错误消息
这有一些限制:例如,您将无法写入文件名包含一个空格。这一点的好处是,你不必引用你的命令。我已经发布在gist.github.com,如果我最终改进它,我会尽量保持更新。
无论该代码段是否可用此处。它可以放入.vimrc文件或〜/ .vim / plugins中的文件中。
How can I redirect or pipe the output of an ex command into my current buffer or a file?
For example, I want to read the contents of all the registers into the current buffer, which in ex mode is shown using :registers
.
:redir >name_of_registers_file
:registers
:redir END
:r name_of_registers_file
:help redir
The last command is very useful, since there are lots of options for redirection: to variables, to registers, how to append, further cornucopia.
I still find it weird and annoying that it uses END that way, but since everything else that can follow redir
has to start with a non-word-character, at least it's not ambiguous.
PS AFAIK (which is pretty far in this case) there's no way to read it directly into the buffer: you have to store it in a register or a variable first. Check the help for the various options of how to do that.
PPS If you do want to do this using a variable —maybe to encapsulate it in a function and avoid clobbering registers or global variables— you'll have to convert the multiline string that gets written to the variable into a list. EG
:call append( '.', split(variable_you_redirected_to, "\n") )
Otherwise (if you just do append('.',var)
) you end up with ^@'s (nulls) instead of newlines, since that's what vimscript uses to represent newlines in String variables.
edit: as @Bill Odom mentions, using :put =variable_you_redirected_to
is a lot easier than the complicated append()
expression. Thanks, Bill!
PPPS
I've written a snippet to make this stuff more convenient. It declares a function Redir(command, target)
and a command R
.
The command parses the last series of non-space characters as a redirection target and passes that to the function, which does the boilerplate to redirect the command output to the redirection target.
The command is everything after R
and before the last space.
EG
" Store the vim buffer list in buffer_list.txt
:R ls >buffer_list.txt
" Store error messages emitted by a function being debugged
" in the 'unnamed register'
:R call FunctionBeingDebugged() @">
There are a few limitations with this: for example you won't be able to write to a filename that contains a space. The upside to this is that you don't have to quote your command. I've got it posted on gist.github.com, and I'll try to keep it updated if I end up improving it. Or you can fork it yourself</noeuphemism>!
Anyway the snippet is available here. It can be dropped into a .vimrc file or into a file in ~/.vim/plugins.
这篇关于如何将ex命令输出重定向到当前缓冲区或文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!