Wikia

Vim Tips Wiki

Send buffer to interpreter in a new xterm

Talk0
1,613pages on
this wiki
Revision as of 23:34, September 29, 2008 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 944 Printable Monobook Previous Next

created June 4, 2005 · complexity intermediate · author Thomas Rowe · version 5.7


Without saving the current buffer you want to send its contents to an interpreter (python, scheme, whatever), and you want that interpreter to run in a freshly spawned xterm. A new xterm window is useful because running the shell within Vim does not allow simultaneously inspection of program output and the code buffer, and Vim's shell is weak in several respects.

Put this script in your path and make it executable:

#!/bin/sh
# This is for use from .vimrc files. It sends vim buffers to the specified
# interpreter.
if [ "$#" -lt 1 ]
then
  echo "ERROR: not enough args."
  echo "VIMRC USAGE: viminterpreter <interpretercommand>"
  exit 1
fi
TMP=`mktemp`
VM="$1" #command to run on the current buffer
cp /dev/stdin $TMP
xterm -e "\"$VM\" $TMP && rm $TMP && read"

Add the following to vimrc:

map ,p :w !viminterpreter python <CR>
map ,g :w !viminterpreter gosh <CR>

Now you can edit scheme code in an unsaved vim buffer and hit ",g" to see what it does. Or ",p" for python code.

CommentsEdit

Edit the last line in the script to read:

xterm -e "\"$VM\" $TMP; rm $TMP; read"

The original doesn't let you see the errors if your script fails.


Following should help:

:map ,p :!xterm -e "python \"<the_current_buffer>\"" &

Note that & at the end of the line.


Around Wikia's network

Random Wiki