Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

8 Answers 8

up vote 3 down vote accepted

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)

share|improve this answer
add comment

I don't know about vim directly, but you can split the screen in this way generally with the screen utility. ctrl+a,S (note, that is a capital S) will split the display and you can switch between the two with ctrl+a,tab (if the window is empty, use ctrl+a,n to move to another open window or ctrl+a,c to create a new one).

You can split the display into more then two too, and change the size of the splits with ctrl-a,:resize . I use screen this way often, and while it isn't the single-key solution you ask for it can be very convenient.

share|improve this answer
    
Are you sure it's working for Vim? It does not seem to respond in my vim (I use the slpit command to split between files). –  Adam Matan Aug 10 '09 at 14:48
    
These are GNU screen, not vim, commands. –  innaM Aug 10 '09 at 17:36
    
Yep. I'm talking about running vim within screen. –  David Spillett Aug 10 '09 at 20:51
    
a vim split is something that happens within the realm of the vim window (or the terminal that it's running in). –  hasenj Aug 11 '09 at 20:48
add comment

It might not be split-screen, but this works rather well for me:

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

This maps ;e to write out the current file, and execute it using python (it assumes that python is on your PATH).

share|improve this answer
add comment

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).

share|improve this answer
    
This autocommand didn't work for me until I put the first line (with the \if) on the same line as the au BufEnter but I removed the \ –  Jason Axelson Sep 14 '10 at 23:52
    
Vim might not support a shell natively, but I just wanted to mention this interesting shell plugin: vim.org/scripts/script.php?script_id=2771 –  Mr A Jul 11 '13 at 16:58
add comment

The Bexec vim plugin does exactly what you want. Keyboard shortcuts included.

share|improve this answer
add comment

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

 :!%
share|improve this answer
add comment
map <F5> :w<CR>:exe ":!python " . getreg("%") . "" <CR> 

Is what I used with please (F5 runs the current script).

For turning vim into a powerful python IDE i recommend this tutorial:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.