Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #1244 - Preview output from interpreter in new window

Created: May 25, 2006 4:09 Complexity: basic Author: Robert Retzbach Version: n/a Karma: 43/13 Imported from: Tip#1244

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 a so called "preview window" (:he 'preview)


In my case I used ruby and mapped the commands to f7.

I just select the stuff I want to run and press f7 et voila!

The output is shown in a new window!

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


Have fun!


vimrc


8<----

"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


8<----

Comments

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


8<----

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


8<----

flashdrvnk--AT--hotmail.com , May 25, 2006 12:01


Maybe you like to consider using

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

Bertram

vim at bertram dash scharpf dot de , May 26, 2006 1:46


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>

amix -a-t- amix.dk , May 29, 2006 1:37


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

Anonymous , May 30, 2006 7:30


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

flashdrvnk--AT--hotmail.com , August 24, 2006 12:57


Advertisement