sponsor Vim development Vim logo Vim Book Ad

basic Tip #467: vim windows displaying output inside vim window

 tip karma   Rating 11/8, Viewed by 1958 

created:   April 30, 2003 21:45      complexity:   basic
author:   char      as of Vim:   5.7

In vim for *nix or cygwin, the command

:!cmd

displays the output inside vim windows,
but for vim windows, the output is displayed in the console window.
To make the output displayed inside vim or gvim window,
can use

:echo system("command")

references:
:help system()
:help echo

 rate this tip  Life Changing Helpful Unfulfilling 

<<Insert one character only | display date-and-time on status line >>

Additional Notes

wnatter@nortelnetworks_cba.com, May 2, 2003 10:21
If you want, you can have the output of the command sent into your file by using the '!' mechanism a little further:

:<range>!<command>

This will send the lines in the range to the standard input of your command, and replace them with the standard output of the command (see help for details).  Please remove _cba in my address to send me a note.
Anonymous, May 4, 2003 9:16
hmm this is a good tip, but does anyone know how to make it expand % into the current buffer?

Most of the times I am running an external command it involves %
For example, after recovering a swap file:     (very common on windows due to frequent crashes :-)   )

:cp % %.rec

and then

!diff % %.rec

which of course spawns the annoying DOS box to give you the result of the diff.

I tried
echo system("diff expand("%") expand("%.rec")")
echo system('diff expand("%") expand("%.rec")')
echo system("diff expand('%') expand('%.rec')")

But in all cases the shell doesn't get the expanded buffer name.

I even got fancy and tried:

execute "echo system('diff " . expand('%') . " " . expand('%.rec') . "')"

but that seemed to return no output at all, even by checking :messages    :(

Help, anyone?


But none of these successfully expand the % such that the shell receives the actual buffer name  :(
Anonymous, May 7, 2003 22:32
You don't use vimdiff ?
Using the script DirDiff.vim, you get some commands (diffsplit, diffget, diffput)
to work visually with two or  more files with colour marked differences, each in a separate
vim-window.

To read output of system commands into the actual file at filepos use ':r !<command>'.
<command> may be any shell-command, '%' is substituted with current file name,
e.g. ':r !ls -l %.bak' reads in the output of 'ls -l test.cpp.bak' while editing test.cpp.

Works fine on WIndows NT4/2000.

Thomas
jaldri1 at gl dot umbc dot edu, June 11, 2003 9:22
Try this.

    :echo system("dir ".expand("%"))

John

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo