在Vim中,运行命令并将stdout重定向到特定缓冲区

时间:2016-04-16 13:20:45

标签: vim

假设我并排打开两个缓冲区并将源代码输入缓冲区1.我想运行编译器(或任何命令行程序)并在缓冲区2中查看其输出(stdout)。

如何将当前或特定缓冲区作为stdin提供给此命令行程序?如果这不可能,我可以将源代码保存到文件中,并将其指定为编译器的参数;但无论如何我想在缓冲区2中看到输出。

2 个答案:

答案 0 :(得分:3)

如果你看:h :b

:[N]b[uffer][!] [+cmd] [N]              :b :bu :buf :buffer E86
                Edit buffer [N] from the buffer list.  If [N] is not given,
                the current buffer remains being edited.  See :buffer-! for
                [!].  This will also edit a buffer that is not in the buffer
                list, without setting the 'buflisted' flag.
                Also see +cmd.

对于+cmd

                                                        +cmd [+cmd]
The [+cmd] argument can be used to position the cursor in the newly opened
file, or execute any other command:
        +               Start at the last line.
        +{num}          Start at line {num}.
        +/{pat}         Start at first line containing {pat}.
        +{command}      Execute {command} after opening the new file.
                        {command} is any Ex command.

所以:

:2b +r!date

将打开缓冲区2,并读入date命令的输出。

答案 1 :(得分:1)

您可以使用:r! command执行shell命令并将其输出读取到当前缓冲区。