如何将ex命令输出重定向到当前缓冲区或文件?

Ama*_*ain 43 vim redirect buffer command

如何将ex命令的输出重定向或管道输入到当前缓冲区或文件中?

例如,我想将所有寄存器的内容读入当前缓冲区,在ex模式下显示使用:registers.

int*_*ted 36

:redir >name_of_registers_file
:registers
:redir END
:r name_of_registers_file
:help redir
Run Code Online (Sandbox Code Playgroud)

最后一个命令非常有用,因为有很多重定向选项:变量,寄存器,如何追加,进一步的聚宝盆.

我仍然觉得它很奇怪并且很烦人,因为它使用了END,但是由于其他所有可以遵循的东西都redir必须以非单词字符开头,至少它并不含糊.

PS AFAIK(在这种情况下相当远)没有办法直接将它读入缓冲区:你必须先将它存储在寄存器或变量中.检查帮助以了解如何执行此操作的各种选项.

PPS如果你想使用变量-may来将它封装在函数中并避免破坏寄存器或全局变量 - 你必须将写入变量的多行字符串转换为列表.例如

:call append( '.', split(variable_you_redirected_to, "\n") )
Run Code Online (Sandbox Code Playgroud)

否则(如果你这样做append('.',var))最终会得到^ @(nulls)而不是换行符,因为vimscript用来表示String变量中的换行符.

编辑:正如@Bill Odom所提到的,使用:put =variable_you_redirected_to比复杂的append()表达容易得多.比尔,谢谢!


购买力平价

我写了一个片段,让这些东西更方便.它声明了一个函数Redir(command, target)和一个命令R.

该命令将最后一系列非空格字符解析为重定向目标,并将其传递给函数,该函数执行样板操作以将命令输出重定向到重定向目标.

该命令是R最后一个空格之前和之后的所有内容.

例如

" 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() @">
Run Code Online (Sandbox Code Playgroud)

这有一些限制:例如,您将无法写入包含空格的文件名.这样做的好处是你不必引用你的命令.我已将它发布在gist.github.com上,如果我最终改进它,我会尽力保持更新.或者你可以自己分叉</ noeuphemism>!

无论如何,这里有代码片段.它可以放入.vimrc文件或〜/ .vim/plugins中的文件中.


Bil*_*dom 26

@intuited是对的; 该redir命令是你想要的.像这样的单行将插入:输出registers到当前缓冲区:

redir => m | silent registers | redir END | put=m
Run Code Online (Sandbox Code Playgroud)

然而,这并不是你想要经常打字的东西,而且它并不完全适合关键地图.我发现自己经常这样做,所以我写了一个函数和一些命令来使它更容易.作为奖励,我现在可以将命令输出发送到新窗口或新选项卡,就像将其插入当前缓冲区一样简单.这是代码(最后有​​一些命令示例):

" redir_messages.vim
"
" Inspired by the TabMessage function/command combo found
" at <http://www.jukie.net/~bart/conf/vimrc>.
"

function! RedirMessages(msgcmd, destcmd)
"
" Captures the output generated by executing a:msgcmd, then places this
" output in the current buffer.
"
" If the a:destcmd parameter is not empty, a:destcmd is executed
" before the output is put into the buffer. This can be used to open a
" new window, new tab, etc., before :put'ing the output into the
" destination buffer.
"
" Examples:
"
"   " Insert the output of :registers into the current buffer.
"   call RedirMessages('registers', '')
"
"   " Output :registers into the buffer of a new window.
"   call RedirMessages('registers', 'new')
"
"   " Output :registers into a new vertically-split window.
"   call RedirMessages('registers', 'vnew')
"
"   " Output :registers to a new tab.
"   call RedirMessages('registers', 'tabnew')
"
" Commands for common cases are defined immediately after the
" function; see below.
"
    " Redirect messages to a variable.
    "
    redir => message

    " Execute the specified Ex command, capturing any messages
    " that it generates into the message variable.
    "
    silent execute a:msgcmd

    " Turn off redirection.
    "
    redir END

    " If a destination-generating command was specified, execute it to
    " open the destination. (This is usually something like :tabnew or
    " :new, but can be any Ex command.)
    "
    " If no command is provided, output will be placed in the current
    " buffer.
    "
    if strlen(a:destcmd) " destcmd is not an empty string
        silent execute a:destcmd
    endif

    " Place the messages in the destination buffer.
    "
    silent put=message

endfunction

" Create commands to make RedirMessages() easier to use interactively.
" Here are some examples of their use:
"
"   :BufMessage registers
"   :WinMessage ls
"   :TabMessage echo "Key mappings for Control+A:" | map <C-A>
"
command! -nargs=+ -complete=command BufMessage call RedirMessages(<q-args>, ''       )
command! -nargs=+ -complete=command WinMessage call RedirMessages(<q-args>, 'new'    )
command! -nargs=+ -complete=command TabMessage call RedirMessages(<q-args>, 'tabnew' )

" end redir_messages.vim
Run Code Online (Sandbox Code Playgroud)

  • 很好,我没有意识到你可以只使用 `:put =var`。 (2认同)

Ash*_*ppa 18

:redir @a
:registers
:redir END
"ap
Run Code Online (Sandbox Code Playgroud)

:redir @a 此处的所有消息重定向到名为的寄存器a.您可以使用要捕获其输出的命令(:registers在您的情况下)来执行此操作.:redir END结束重定向."ap装置,"a使用名为寄存器ap把所选择的寄存器的内容到当前缓冲区.

捕获前命令输出Vim的技巧维基了解更多信息:-)


Nat*_*ver 7

只要您可以保存当前缓冲区并且可以将消息附加到当前文件的末尾,就没有必要使用临时变量。

来自 Vim 文档:

:redi[r] >> {file}      Redirect messages to file {file}.  Append if {file}
                        already exists.  {not in Vi}
Run Code Online (Sandbox Code Playgroud)

http://vimdoc.sourceforge.net/htmldoc/various.html#:redir

因此,要将消息附加:registers到当前文件的底部,请执行以下操作:

:write | redir >> % | silent registers | redir END | edit
Run Code Online (Sandbox Code Playgroud)
  1. :write 文件,以便任何更改都不会丢失
  2. 开始将输出重定向到%当前文件的名称。
  3. 静默运行:registers命令。
  4. END 重定向到文件。
  5. :edit 文件以查看新更改。

您还可以强制截断当前文件并将其替换为输出消息:

:redir! > % | silent registers | redir END | edit!
Run Code Online (Sandbox Code Playgroud)

但这可能不是您想要做的。


Fra*_*Niu 7

此外,:h :redir

...要获得一个命令的输出|execute()| 可以使用功能。

例如

put = execute('scriptnames')
put = execute('filter /vimfiles/ scriptnames')
Run Code Online (Sandbox Code Playgroud)


小智 7

不幸的是,我没有代表,所以我不能将此作为评论添加到“Francis Niu”。

execute() 函数看起来是完成此操作的最简单方法,它是在vim 8中添加的。

列在 vim 的 github 上的新/扩展功能中。 版本8.txt