Vim Tips Wiki
Register
Advertisement
Tip 1244 Printable Monobook Previous Next

created 2006 · complexity basic · author Robert Retzbach · version n/a


This will come in handy for those of you who script in an interpreted language.

If you ever wanted to just run parts of your script and check the output without manipulating your code, read on.

The output of your whole script or your snippets will be shown in the "preview window" (:help preview-window).

In my case I used Ruby and mapped the commands to F7. I just select the stuff I want to run and press F7. The output is shown in a new window!

The next time I press F7 the window will be closed and replaced.

"save code, run ruby, show output in preview window
function! Ruby_eval_vsplit() range
  let src = tempname()
  let dst = tempname()
  execute ": " . a:firstline . "," . a:lastline . "w " . src
  execute ":silent ! ruby " . src . " > " . dst . " 2>&1 "
  execute ":pclose!"
  execute ":redraw!"
  execute ":vsplit"
  execute "normal \<C-W>l"
  execute ":e! " . dst
  execute ":set pvw"
  execute "normal \<C-W>h"
endfunction
vmap <silent> <F7> :call Ruby_eval_vsplit()<CR>
nmap <silent> <F7> mzggVG<F7>`z
imap <silent> <F7> <Esc><F7>a
map <silent> <S-F7> <C-W>l:bw<CR>
imap <silent> <S-F7> <Esc><S-F7>a

Comments[]

Here is a horizontal version. In this example the windows aren't recreated, which means the dimensions stay the same for the current tab.

function! Ruby_eval_vsplit() range
  let src = tempname()
  let dst = tempname()
  execute ": " . a:firstline . "," . a:lastline . "w " . src
  execute ":silent ! ruby " . src . " > " . dst . " 2>&1 "
  execute ":pedit! " . dst
endfunction

Maybe you like to consider using:

:<range>w !ruby >myoutput
:.r !ruby myscript.rb
:<range>!ruby

Here is a generic Python version of the tip. It does not require to have Vim with Python support compiled in.

"Run in the Python interpreter
function! Python_Eval_VSplit() range
  let src = tempname()
  let dst = tempname()
  execute ": " . a:firstline . "," . a:lastline . "w " . src
  execute ":!python " . src . " > " . dst
  execute ":pedit! " . dst
endfunction
au BufNewFile,BufRead *.py vmap <F7> :call Python_Eval_VSplit()<CR>

You can create a more generic version by parsing the first line of the file. If the first line is of the form:

#!/path/to/executable

Then, you can use /path/to/executable to execute the selected code. If the first line of the file doesn't match, you can consider the file's extension and choose an executable based on that:

file.pl - execute with perl
file.sh - execute with sh
file.vim - execute with vim
etc

I changed it a little more and added some comments. The biggest change is, that no shell window is opened (in windows). I hated the cmd window to popup, even if it's just for a second.

function! Ruby_eval_vsplit() range
  let src = tempname()
  let dst = "Ruby Output"
  " put current buffer's content in a temp file
  silent execute ": " . a:firstline . "," . a:lastline . "w " . src
  " open the preview window
  silent execute ":pedit! " . dst
  " change to preview window
  wincmd P
  " set options
  setlocal buftype=nofile
  setlocal noswapfile
  setlocal syntax=none
  setlocal bufhidden=delete
  " replace current buffer with ruby's output
  silent execute ":%! ruby " . src . " 2>&1 "
  " change back to the source buffer
  wincmd p
endfunction

vmap <silent> <F7> :call Ruby_eval_vsplit()<CR>
nmap <silent> <F7> mzggVG<F7>`z
imap <silent> <F7> <Esc><F7>a
map <silent> <S-F7> <C-W>l:bw<CR>
imap <silent> <S-F7> <Esc><S-F7>a

Also see this Ruby Runner plugin: http://henrik.nyh.se/2011/03/vim-ruby-runner

Advertisement