Computational Mathematics

Adventures in Computing, Statistics, and R

R with Vim on Mac OS X

4 Comments »

The built-in script editor for the Mac OS X R GUI actually isn’t bad. In fact it is much better than its Window’s counterpart. In particular, it has:

  • Syntax highlighting
  • Auto-completion
  • Bracket matching
  • Auto-indent
  • Block code execution (with blocked command history)

However, when coding in R and pretty much any other language, Vim has always been my goto text editor of choice. Once you get past the fairly steep learning curve, nothing comes close to it in terms of coding efficiency and navigation except perhaps Emacs, but let’s not go there.

Michael Bojanowski wrote a blog post on using R with Vim on an Ubuntu machine. One commenter asked about getting it to work on Mac OS X.

First of all, I highly recommend the MacVim port of Vim (not to be confused with macvim.org). You can get the latest version (7.3.53 at time of post) at github:

https://github.com/b4winckler/macvim/downloads

The Vim-R-plugin that Michal mentioned works on OS X with the Screen plugin as well. However, a simpler method is to use one of the following two OS X specific scripts that rely on AppleScript.

R.vim
R.vim uses AppleScript to send selected lines of code to an R buffer. Simply hit <F3> to run the selected lines in R. ┬áIf R is not open, it will automatically open a new session. Note that it opens R.app instead of R64.app so if you need to run the 64-bit version you’ll have to tinker with the script a bit.

R-MacOSX
R-MacOSX is also pretty straight forward, although I couldn’t get it working properly. It defaults to <Cmd-E> to run selected lines and <Shift-Cmd-E> to source the entire file.

In either case, key mappings are customizable but personally, I have no trouble with <F3> to execute code using R.vim.  Neither are as full-featured as Vim-R-plugin but I find them sufficient as all I really need is to send blocks of code to R.

Happy Vimming!

4 Responses

  1. Great that you figured that one out!

    A side note: In my post I described the Vim+R setup on Ubuntu, not Windows… ;)

  2. [...] R with Vim on Mac OS X « Computational Mathematics [...]

  3. [...] here how to set it up on [...]

Leave a Reply