sponsor Vim development Vim logo Vim Book Ad

PyScratch : allows processing of Python/Ruby/Perl/Lua commands directly in Vim buffer

 script karma  Rating 10/4, Downloaded by 523  Comments, bugs, improvements  Vim wiki

created by
Herbert Sitz
script type
PyScratch is a simple plugin that lets you use any Vim buffer as a scratchpad for running and viewing the output of Python, Ruby, Perl, and Lua commands.  (Note the name 'PyScratch' is no longer accurate and will probably be changed.)  This plugin can also be found at github:

You can watch a screencast of PyScratch at:
NOTE:  You can get tab completion of your Python commands by using any of the
available methods of completion in Vim.  I've tried the Pydiction
tab-completion plugin and recommend that anyone using PyScratch install Pydiction
( http://www.vim.org/scripts/script.php?script_id=850 )
NOTE2:  Also works with RUBY, PERL, and LUA.
The name PyScratch is now misleading, since code has been modified to work
with Ruby, Perl, and/or Lua.  The global variable g:script_language can
be set to 'python,'ruby','perl', or 'lua', and controls which interpreter
is used to process '>>>' commands and code blocks.  Support for any
interpreter you use must be compiled into your Vim.  Note also that usng Perl
is a little different from the other languages, because Vim does not
support Perl's 'print' or 'say' commands.  Instead 'VIM::Msg' is used
to specify printed output.   See :help perl for more.

The PyScratch plugin consists of a single vimscript file that a Vim user can
install by placing it in their 'plugin' directory.

PyScratch requires a Vim executable compiled with Python support. It has been
tested with Python26 but version should not matter.

Over last few years several different plugins have been designed to send
commands in a Vim buffer out to a shell for processing (typically scripting the
'Gnu Screen utility), where the output can be viewed.  This works well, but for
languages where Vim has built-in support some people might prefer executing the
commands and getting output right in the Vim buffer.  All this takes is
redirecting the output from a supported language's command to the user's buffer
instead of to a message buffer.  This technique is possible only for the
languages where Vim offers compile-in support, which include Python, Perl,
Ruby, Lua, MZScheme (anything else?)  This plugin is specific to Python but it
should be relatively easy to extend to other supported languages.

When the PyScratch plugin is loaded you can process Python commands and get
Python output in your Vim buffer in several different ways:

(1)  Prepend a '>>>' user prompt as first characters in a line, then press
<enter>.  The command will be evaluated and results will be shown beginning in
the next line.  Commands that provide no output have the command itself
mirrored as output as confirmation that they have been processed.  You will
typically want to issue Python print commands, and these can be abbreviated
with 'p' instead of 'print'.  Some examples:

>>> print 8 + 16
>>> p 8 + 16
>>> import sys
( import sys )
>>> for i in range(5): print i
>>> etc. etc.

(2)  You can execute a block of lines at one time by visually selecting them
and pressing <ctrl_enter>.  The block should be a clean block of code with no
user prompt chars (i.e, '>>>') at left margin.  Output will be returned below
the block of lines.  Note that this method uses <ctrl-enter> rather than the
simple <enter> of the single-line-prompt method.

(3)  It's not part of the plugin, but an entire file of Python can be executed
with the Vim command:   pyfile <filename>.  The plugin does not capture any
output from this command, however functions defined in the file can may then be
accessed in the Vim buffer using single-line-prompt Python commands or the
block-select Python processing.

The plugin has a couple of lines defining a syntax region for error messages
and a highlight command that displays them in red.  This may be overwritten by
a syntax file that clears all syntax settings so if you want to use these you
may want to move the syntax and the highlight command to a syntax and color
file, respectively.

This is just a Sunday afternoon hack and there are lots of little tweaks and
improvements that could be added.   I'd be happy to hear comments or
suggestions. . .
install details
Put the pyscratch.vim file in your .vim/plugin or vimfiles/plugin directory.  Functionality will then be available in any Vim buffer.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
PyScratch-015.zip 0.15 2010-11-20 7.0 Herbert Sitz Added support for Ruby, Perl, and Lua
PyScratch-0-11.zip 0.11 2010-11-16 7.0 Herbert Sitz Fixed major bug that interfered with entering <CR> when in insert mode.
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
SourceForge.net Logo