Open the output of a shell command in a split pane

vim

Consider any interpreted programming language.

I can run the currently opened script using ! interpreter %, and use the return key to return to vim after the execution is finished.

Is it somehow possible to not replace the current window with the output of the executed shell command, but open the output in a new split pane instead? I would like to still see my source code during the execution of my script.

I tried :split ! interpreter %, but it didn't work.

Is there a way? I am using vim 7.3.

Best Answer

With :!, the external command is executed in a shell, and the fleeting output isn't captured inside Vim; you just see what gets printed to the terminal, then control returns to Vim after the external command finishes.

To keep the output, you have to read it into a Vim buffer. For that, there's the :read! command. To open a new scratch buffer, combine this with :new:

:new | 0read ! <command>

If you want to pass the current buffer's filename (%) to the external command, you have to use :execute, so that it is already evaluated in the current buffer:

:execute 'new | 0read ! interpreter' expand('%')

Or, you use the fact that with a new buffer, the previous one becomes the alternate one, and use # instead of %:

:new | 0read ! interpreter #

Get rid of the scratch buffer with :bdelete!.

If you want to see the output asynchronously while it executes, you need an external multiplexer, or a plugin, as mentioned in @chaos answer.