Running interpreters from Vim
Updated Oct 11 2009 with gnuplot commands. Be very, very careful copying the single quotes, double quotes, and backticks.
I haven't forgotten about the web, but now that I'm back in school I am spending most of my time in Octave and R. Rather than start the interpreters and input one command at a time I prefer to write scripts as would normally be done in languages like Python or Perl.
To make this process easier I've put together some Vim shortcuts for sending the current buffer's full file path to various interpreters.
Put this in your vimrc:
"Send the current buffer's complete path to various interpreters
" \[letter] - non-interactive execution
" \[shift+letter] - execute, then run interactive shell
" Note: %:p in the expressions below gives the full file path to the current buffer
noremap <leader>p :!clear && python %:p \| less<CR><CR>
noremap <leader>P :!clear && python -i %:p<CR>
noremap <leader>o :!clear && octave -q %:p \| less<CR><CR>
noremap <leader>O :!clear && octave -q --persist --eval "source('%:p')"<CR>
noremap <leader>r :!clear && R -q --no-save --no-restore-data < %:p \| less<CR><CR>
noremap <leader>R :!clear && R -q --save --no-restore-data < %:p && R -q --no-save<CR>
" You'll probably want to change some of the gnuplot font and size options
" This outputs to the full file path of the script, with the script's extension replaced with .png
noremap <leader>g :!clear && gnuplot -e "set terminal png truecolor size 800,600 font '/Library/Fonts/Arial.ttf'; set output '$(expr %:p : '\(.*\)\..*').png'" %:p<CR><CR>
noremap <leader>G :!clear && gnuplot %:p -<CR>
Now in Vim you can use \p (note: <leader> is mapped to backslash by default) to execute the current buffer's file using Python. If you instead use \P the file will be executed and then an interactive prompt will be presented, allowing you to continue interacting with the objects created by the script that are left after execution. Similarly, \o, \O, \r, \R, \g and \G work with Octave, R, and Gnuplot.
A few caveats:
- Your buffer needs to be written to a file first.
- The working directory used by these interpreters is the directory in which Vim was started. So if your file has relative includes like "source('my_function.m')" they may break if you're not editing in the correct/same directory.
- This works best for short scripts that don't take long to run.
- You might want to replace python with ipython in the interactive case \P.