Vim: execute the script I'm working on in a split screen

I'm working on a small python script, which needs frequent execution in order to debug and further develop.

Can I split the vim screen and execute my script on one part with a keystroke?


Solution 1:

Add this line to your .vimrc:

command R !./%

And then you can just type ":R" while in Vim to run the script (vim-run-current-file)

Solution 2:

Vim does not, and will never support an embedded shell like emacs or kate (if you mean that), see this stackoverflow question.

David Spillet is right, you can run your vim inside gnu screen:

$ screen vim foo.txt

But that will only give you something remotely resembling a windowmanager in a terminal - VERY useful when used over ssh or on a box with no X, but locally you can just as well open another xterm and switch between them.*

Anyway, if you can live with the fact that you won't see the file you're editing while looking at the output it produces, Jack M's tip is good, but could be shorter:

:map ;e :w<cr>:!python %<cr>

For the same purpose, I have this in my ~/.vimrc:

au BufEnter *
\if match( getline(1) , '^\#!') == 0 |
\ execute("let b:interpreter = getline(1)[2:]") |
\endif

fun! CallInterpreter()
    if exists("b:interpreter")
         exec ("!".b:interpreter." %")
    endif
endfun

map <F5> :call CallInterpreter()<CR>

This runs any file that has a shebang (#!) on the first line. It uses the interpreter to run the file, so it does not need to have execute permissions.

*(screen has some other very neat features like copy-and paste from the output, monitoring hidden windows for activity/no-activity, being able to use the session from different terminals at the same time, being able to log out leaving all programs running - it is a powerful tool).

Solution 3:

The least verbose way (which requires your script to be executable) is probably:

 :!%