Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim, run command and redirect stdout to specific buffer

Tags:

vim

Say I open two buffers side by side and enter the source code into buffer 1. I want to run the compiler (or any command line program) and see its output (stdout) in buffer 2.

How do I feed the current or specific buffer as stdin to this command line program? If this it not possible, I can save source code to the file and specfy it as parameter to compiler; but anyway I want to see output in buffer 2.

like image 946
George Sovetov Avatar asked Apr 16 '16 13:04

George Sovetov


2 Answers

If you look at :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.

And for +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.

So:

:2b +r!date

Would open buffer 2, and read in the output of the date command.

like image 89
muru Avatar answered Nov 19 '22 06:11

muru


You can use :r! command to execute shell command and read its output to current buffer.

like image 4
dz902 Avatar answered Nov 19 '22 06:11

dz902