[R] R and ViM

François Pinard pinard at iro.umontreal.ca
Wed Apr 19 06:28:00 CEST 2006


[Michael Graber]

>[...] I'd like to be able to use R together with ViM.  [...]  My 
>question now is, whether there are already people out there knowing how 
>to do this in a similar easy way as with Emacs [...]

I've been an Emacs user for a very long time, and then, switched to Vim.
See http://pinard.progiciels-bpi.ca/opinions/editors.html, if you feel 
curious, for a few personal thoughts on Emacs.

For R, I tried sticking to a mere interactive shell, taking advantage of 
the "GNU readline" interface built into R, with Vim as an external 
editor.  For sending R code from Vim to R, one merely selects the code 
to send within Vim using the mouse, and paste it directly with the mouse 
in the interactive shell window running R.  Simple and comfortable!  :-)

Emacs offers ESS, which has many interesting features.  However, despite 
quite attractive, it did not fully seduce me: a bit because I try to 
avoid returning to Emacs keystroke habits, a bit because ESS is heavy 
weighted compared to Vim + R-in-a-shell solution, a bit because ESS adds 
distracting idiosyncrasies, like scrolling differently or opening extra 
windows at times.  R already offers enough options I could customize if 
I want to read help in a browser or a pager, and at good speed.  (Of 
course, if you use an heavy browser, you feel it; but "links -g" is OK!)

An ESS nicety that my current setup does not really replace is the 
automatic highlighting or R output.  One of the advantages of this 
output highlighting is visually spotting R requests and replies.  As 
a compromise, I'm using this bit of a kludge in my Rprofile file:

if (interactive()) {
    local({
        options(editor='vim -c "set ft=r"')
        if (Sys.getenv('TERM') %in% c('rxvt', 'xterm')) {
            onglet = 2
            options(prompt=paste(sep='',
                                 formatC('', width=80-onglet), '\033[;30;45m',
                                 formatC('', width=onglet), '\033[0m\n',
                                 options('prompt')))
        }
    })
}

The "set ft=r" bit ensures proper highlighting and coloration within 
Vim, whenever edit() or fix() are used.  Here "vim" could be replaced by 
"gvim" or "gvim -f", say.  (In my Vim configuration, "vim" uses the GUI 
automatically if started within X; or uses the console mode otherwise.)

Then, the R prompt is modified to visually mark each request-reply 
interaction with a white separating line holding a small violet marker 
at the right.  It works nicely for me in almost all circumstances (there 
are a few, uncommon exceptions).  Usual scrolling of the shell window 
allows me to quickly find R commands and replies, even if much less 
colourful than with ESS.  I'm ready to pay that price for simplicity.

A last trick which is convenient in my case.  My X window manager allows 
customization of keystrokes.  (I'm using Openbox, but surely many other 
window manages offer that possibility too.)  For all 26 of 
Ctrl-Alt-Letter, the same small "openbox-helper" (Python) script of mine 
is called with the Letter given as an option, which may launch 
applications in turn.  This is how Ctrl-Alt-R opens a shell window 
running R, and Ctrl-Alt-M opens a shell window running Maxima.  In both 
these shells, Ctrl-D closes the application and the window.  This is 
convenient for quick mathematical jobs, and quite in the spirit of Vim 
(fast and easy start/exit, instead of long running like Emacs).

-- 
François Pinard   http://pinard.progiciels-bpi.ca




More information about the R-help mailing list