tags:

views:

939

answers:

4

How to execute file that I'm editing in VI(M) and get output in split window (like in SciTE)?

Of course I could execute it like that:

:!scriptname

But is it posible to avoid writing script name and how to get output in split window instead just bottom of the screen?

+9  A: 

There is the make command. It runs the command set in the makeprg option. Use % as a placeholder for the current file name. For example, if you were editing a python script:

:set makeprg=python\ %

Yes, you need to escape the space. After this you can simply run:

:make

If you wish, you can set the autowrite option and it will save automatically before running the makeprg:

:set autowrite

This solves the execute part. Don't know any way of getting that output into a split window that doesn't involve redirection to file.

Martinho Fernandes
Thanks (+1). I knew about this command but I thought it's is not customizable.
Oleksandr Bolotov
Actually, your solution does get the output to a split window. Use<pre>:copen</pre>to open up the "error list" produced by running <code>:make</code>.in its own window. Unfortunately, to get the output to be formatted properly, some finagling of the <code>errorformat</code> option is necessary. Otherwise output will be presumed to be of the format gcc puts out.
Conspicuous Compiler
You're damn sure about "finagling" errorformat. It look like some perl code I've seen...
Martinho Fernandes
+10  A: 

To access the current buffer's filename, use %. To get it into a variable you can use the expand() function. To open a new window with a new buffer, use :new or :vnew. To pipe the output from a command into the current buffer, use :.! . Putting it all together:

:let f=expand("%")|vnew|execute '.!ruby "' . f . '"'

obviously replacing ruby with whatever command you want. I used execute so I could surround the filename with quotation marks, so it'll work if the filename has spaces in it.

Brian Carper
+1  A: 

I use a slightly more intrusive mechanism through maps:

map ;e :w<CR>:exe ":!python " . getreg("%") . "" <CR>

Just makes it so I don't have to save, then go. Just go.

Jack M.
If you set the autowrite option, you can run :make and it... auto saves before.
Martinho Fernandes
A: 

For Shell script I've used

:set makeprg=%

:make

Timur Fanshteyn