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

9 Answers 9

up vote 4 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

Put this small snippet in your .vimrc to execute the current file with one keystroke (like F5) and display the result in a new split-pane buffer.

:! is okay but you need to switch to your terminal to see the result.

While you can do that with ctrl-z and bring vim back with fg it still means you need to switch context a lot.

The way this snippet works is by first guessing the executable based on the filetype and then running it with the current file as its argument.

Next a handy utility method takes the output and dumps it into a new buffer.

It's not perfect, but really fast for common workflows.

Here's the snippet copied below:

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""" RUN CURRENT FILE """""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Execute current file
nnoremap <F5> :call ExecuteFile()<CR>

" Will attempt to execute the current file based on the `&filetype`
" You need to manually map the filetypes you use most commonly to the
" correct shell command.
function! ExecuteFile()
  let filetype_to_command = {
  \   'javascript': 'node',
  \   'coffee': 'coffee',
  \   'python': 'python',
  \   'html': 'open',
  \   'sh': 'sh'
  \ }
  let cmd = get(filetype_to_command, &filetype, &filetype)
  call RunShellCommand(cmd." %s")
endfunction

" Enter any shell command and have the output appear in a new buffer
" For example, to word count the current file:
"
"   :Shell wc %s
"
" Thanks to: http://vim.wikia.com/wiki/Display_output_of_shell_commands_in_new_window
command! -complete=shellcmd -nargs=+ Shell call RunShellCommand(<q-args>)
function! RunShellCommand(cmdline)
  echo a:cmdline
  let expanded_cmdline = a:cmdline
  for part in split(a:cmdline, ' ')
     if part[0] =~ '\v[%#<]'
        let expanded_part = fnameescape(expand(part))
        let expanded_cmdline = substitute(expanded_cmdline, part, expanded_part, '')
     endif
  endfor
  botright new
  setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
  call setline(1, 'You entered:    ' . a:cmdline)
  call setline(2, 'Expanded Form:  ' .expanded_cmdline)
  call setline(3,substitute(getline(2),'.','=','g'))
  execute '$read !'. expanded_cmdline
  setlocal nomodifiable
  1
endfunction
share|improve this answer
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

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

 :!%
share|improve this answer

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

share|improve this answer

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 Mikkél Jul 11 '13 at 16:58
    
:map ;e :w<cr>:!python %<cr> wants to echo stuff to my vim buffer to "confirm" (?) that the command ran. How do I get it to simply execute the command silently? :map <silent> ... does not appear to work. –  Johnny Utahh May 18 at 0:43

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

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). –  hasen Aug 11 '09 at 20:48

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.