sponsor Vim development Vim logo Vim Book Ad

EvalSelection.vim : evaluate selected code (vim, bash, ruby, python, perl, (mz)scheme, tcl, R, SPSS)

 script karma  Rating 19/7, Downloaded by 5521  Comments, bugs, improvements  Vim wiki

created by
Tom Link
 
script type
utility
 
description
Evaluate source code selected in a visual region. The result can be displayed
in an interaction log, appended to the text, replace the text, or saved in the
@e register. This is useful for performing small text manipulation tasks or
calculations that aren't worth the trouble of creating a new file or switching
to a different program, but which are too big for being handled in the command
line.

The key bindings follow this scheme:

    <Leader>e{LANGUAGE}{MODE} :: work on the visual region
    <Leader>E{LANGUAGE}{MODE} :: work on the contents of the e-register
    <Leader>x{MODE}           :: work on the visual region (derives LANGUAGE
                                 from the buffer's 'filetype')

LANGUAGE being one of:

    v :: vim (comments at the end of line after vim code could cause problems)
    r :: ruby (requires compiled-in ruby support)
    s :: sh (using system())
    e :: calculator, evaluate formula (using either ruby, python, perl, tcl,
         or vim)

I haven't actually tested the following three bindings because I use
neither of them. Anyway, as I happen to use a Vim with +tcl, +python and
+perl support compiled in, I added them to the standard bindings:

    y :: python (requires compiled-in python support)
    p :: perl (requires compiled-in perl support)
    t :: tcl (requires compiled-in tcl support)
    z :: mzscheme (requires compiled-in mzscheme support)

If ruby-support is compiled in, the following languages are available too
(but see |EvalSelection-Interaction| below):
    
    c :: scheme
        set g:evalSelectionSchemeInterpreter to "Gauche" or "Chicken"
    l :: lisp
        set g:evalSelectionLispInterpreter to "CLisp"
    o :: ocaml
        set g:evalSelectionOCamlInterpreter to "OCaml" or "OCamlClean"
    R :: R
        set g:evalSelectionRInterpreter "R", "RClean", or "RDCOM"
    S :: SPSS
        limited support (no return values), the (one-way) communication with
        SPSS is done via OLE automation

MODE being one of:

    x :: just evaluate and put the result in the e-register
    e :: the same as x
    p :: echo/print the result on the command line
    a :: append the result to visual region
    r :: replace visual region with the result
    l :: insert the result in a temporary interaction log


Commands:

    EvalSelectionCmdLine LANG :: Simulate a command line for LANG; due to the
    primitive handling of input/output, no input commands (like read, get kind
    of stuff) can be used; all output is redirected to the log. Exit with ^D +
    Enter, i.e. enter the ^D character.

    EvalSelectionSetup{LANG}  :: Set up interaction with external interpreter
    (but see below)

    EvalSelectionQuit INTERPRETER :: Quit interaction with an
    external interpreter
 
install details
Requires multvals.vim (vimscript #171), +ruby (for interaction with an interpreter)

If you have +ruby support compiled in and if you want to talk to an external interpreter, make sure that g:evalSelectionRubyDir points to the directory where EvalSelection.rb resides. Default values are:

    Win32: $VIM."/vimfiles/ruby/"
    Else:  "~/.vim/ruby/"


Also available via git
http://github.com/tomtom/vimtlib/tree/master
 

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
EvalSelection.vba.gz 0.16 2007-06-08 7.0 Tom Link - MzScheme support (thanks to Mark Smithfield)
- Catch errors on EvalSelectionQuit (you'll have to manually kill zombie
processes)
EvalSelection.vba.gz 0.15 2007-05-14 7.0 Tom Link - Escape backslashes in EvalSelectionTalk()
- SPSS: Insert a space before variable names (like SPSS itself)
EvalSelection.zip 0.14 2005-06-14 6.0 Tom Link Fixed some menu-related problems; <LocalLeader>r shortkey for SPSS  andR (work similarly to ctrl-r in the spss editor); display a more usefulerror message when communication via OLE goes wrong; possibility to savethe interaction log; post setup & tear down hooks for externalinterpreters
EvalSelection.zip 0.13 2005-02-16 6.0 Tom Link R: set working directory and load .Rdata if available, word completion, catch errors, build objects menu; SPSS: show data window, build menu; g:evalSelectionLogCommands defaults to 1; revamped log
EvalSelection.zip 0.10 2005-02-13 6.0 Tom Link capture interaction with R via R(D)COM; "RDCOM" uses a 2nd instance of gvim as a pager (it doesn't start RCmdr any more); "RDCOM Commander" uses RCmdr; "RDCOM Clean" and "RDCOM Commander Clean" modes; take care of R functions with void results (like data, help.search ...)
EvalSelection.zip 0.9 2005-02-12 6.0 Tom Link support for interaction with R and SPSS via win32 COM/OLE; calculator shortcuts
EvalSelection.zip 0.8 2004-05-06 6.0 Tom Link improved interaction with external interpreters (still not fully usable but it's getting better); reunified EvalSelection.vim and EvalSelectionRuby.vim
EvalSelection.zip 0.7 2004-05-03 6.0 Tom Link Small improvements with external interpreters (e.g., multi-line commands in R); moved code depending on +ruby to EvalSelectionRuby.vim (thanks to Grant  Bowman for pointing out this problem); saved all files in unix  format; added python, perl, and tcl (but I can't tell if they work)
EvalSelection.vim 0.6 2004-03-09 6.0 Tom Link Use of redir; EvalSelectionCmdLine (CLI-like interaction); interaction with external interpreter (lisp, scheme, R, ocaml; experimental & requires +ruby support); separated logs; removed bc support
EvalSelection.vim 0.382 2004-01-31 6.0 Tom Link l-Mode for logging
ip used for rating: 142.132.191.50

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.
   
Vim at Github