How to execute file I'm editing in Vi(m)
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.
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.
Vim has !
("bang") command which executes shell command directly from VIM window. Moreover it allows launching sequence of commands that are connected with pipe and read stdout.
For example:
! node %
is equivalent to opening command prompt window and launching commands:
cd my_current_directory
node my_current_file
See "Vim tips: Working with external commands" for details.