Skip to content

R with Vim

2010 December 2
tags: ,
by Michał

For all those who think that Vim is The Editor for text files, and simultaneously think that R is The EnvironmentForStatisticalAnalysisAndGraphics.

After trying out various options for intergrating Vim with R I settled on the following configuration:

  1. Use Vim-R-pluginfor editing R code files, R documentation files (*.Rd) as well as the Sweave files. Apart from syntax highlighting the plugin allows to open an R console in a separate window and operate it with keyboard shortcuts from Vim (no need for frequent alt-tabbing etc.). Among other things you can:
    • Execute individual code lines, visually selected portions, or whole R code files in the R console.
    • Putting a cursor on a function name in the code file and: display its R help page, or display function arguments (through args()).
    • Put a cursor on any R object in the code file and perform frequently used functions: str, summary, plot, print, names…
    • List the content of the R Workspace
    • Clean the R Workspace
  2. I use Sweave quite extensively. For Sweave files the Vim-R-plugin provides the same keyboard mappings as for the R code files as well as nicely highlights both the LaTeX code and the R code in the code chunks. As my Sweave files have mostly LaTeX code with rather short R code snippets I would like to take advantage of another Vim plugin: the LaTeX-Suite. By default Vim will not load the Latex-suite for Sweave files, which is a HUGE disadvantage.

Vim and R using Vim-R-Plugin in action

Here is a way how to use both plugins simultaneously for Sweave files. The instruction applies to Ubuntu (so probably any Linux-like system). On Windows the ~/.vim directory corresponds to the ‘vimfiles’ directory, which most likely is something like ‘c:Program FilesVimvimfiles’. So:

  1. Install Vim-R-plugin normally.
  2. Install Latex-suite normally.
  3. In ~/.vim/ftplugin remove the symbolic link ‘rnoweb.vim’ and replace it with a normal text file with the following content:
runtime! ftplugin/r.vim
runtime! ftplugin/tex_latexSuite.vim

This will essentially load both plugins one after another. QED.


See here how to set it up on Mac

25 Responses leave one →
  1. December 3, 2010

    I couldn’t get it to work on Mac OS X. Do you know if anyone’s had any luck with it on non-Linux platforms?

  2. January 11, 2011

    It works fine for me on Windows. As per the installation notes for Windows, I had to install Python. And I also had to install the conque shell plugin to get the R console as a buffer.

  3. Dan permalink
    January 15, 2011

    I find that indenting does not handle Sweave commands (<>= etc). What it does is to use the same indenting as in the line above. That is no good, if the above line is indented, as for example would be the case for an item. The problem is that spaces at the start of Sweave commands causes “R CMD Sweave” ignores the commands. (Mac osx; mvim 7.3).

    • January 16, 2011

      Hmm…strange. vim-r-plugin on my ubuntu when I insert <>= properly puts it in the beginning of the new line irrespective of the current LaTeX indentation.

      • Dan permalink
        January 16, 2011

        In case it’s relevant to anyone on the thread at the moment (or who comes here later), I’m using “vim-latex-1.8.23-20101027-r1112“ and “vim-r-plugin-101217“ in my tests that reveal the problem with indenting “<>=“ chunks.

        • January 17, 2011

          Even stranger… as I have vim-r-plugin 101121 (so earlier than yours). I’m not sure where to check the latex-suite version though.

      • Dan permalink
        January 17, 2011

        The author of vim-r sent me a test version, and it works on my test files without any issues. Software development being a pains-taking process, I imagine it may be a while before the test version is ready for release on

  4. mhnatiuk permalink
    July 1, 2011

    totally off the topic: I love the name of a folder on the first screen

  5. ejjunju permalink
    August 23, 2011

    The instructions for installation on windows are not clear to me!!

  6. ejjunju permalink
    August 23, 2011

    Foe instance when you say install install Vim-R-plugin normally, what does this mean? also when you say “remove the symbolic link ‘rnoweb.vim’ and replace it with a normal text file with the following content”, does this apply to windows? what name should i give the text file? By the way my folder doent even include a symbolic link!! it is actually empty.

    I am new to vim and only installed it because i thought that i could use it with Sweave and R. I am looking for nice detailed instructions for windows. also, when you say to replace text

    • August 23, 2011

      Did you look at vim-r-plugin page on ( Windows instructions are there. I think the recent versions of the package do not create any symbolic links, so you should be able to skip that step. All the other points I made apply to both Windows and Unix, and, probably, Mac.

      I hope that helps.

  7. okugami permalink
    September 18, 2011

    Hi, folks,

    Following are the most useful vim plugin for R user,
    It’s worth looking: Any way, Google it to find more information:

    1) To improve finding functions or keywords in multiple files,
    R plugin name: EasyGrep

    2) Improved version of working with multiple R script files, I found to be ctag to be too painful. This plugin works like IDE, allows to book map of files or multiple buffers

    R plugin name: NERDTree

    3) Learn and watch tmux demo on youtube
    It tells you how to split windows virtical, horizontal etc. It’s worth learning of tmux with r-plugin.


    • September 19, 2011

      Hi. Thanks for the plugin suggestions. EasyGrep looks especially promising for my needs.

  8. March 1, 2012

    Thanks for this, having latex-suite and vim-r-plugin play nicely together has been fantastic.

    Question: I see you’re running R and vim in seperate terminal sessions — on my Ubuntu box I can get vim-R-plugin to display a split screen (with screen plugin) but I prefer your setup. Gvim will launch a new terminal running R when I hit rf, but running vim from the terminal does not. How did you set that up?

    • March 1, 2012

      Thanks. Yes, I also find separate terminals much more convenient. I have the following lines in my .vimrc:

      au FileType r let vimrplugin_nosingler = 0
      au FileType r let vimrplugin_vimpager = "no"
      au FileType r let vimrplugin_by_vim_instance = 0
      au FileType r let vimrplugin_screenplugin = 0

      They set vimrplugin_* variables when you open an R file. I do not remember right now what each of the variables mean, but I’m sure you can look it up in the documentation.

    • March 1, 2012

      Oh, and the crucial one:

      let vimrplugin_term_cmd = “gnome-terminal –title R -e”

      that will make rf open a new gnome terminal….

      I think that’s it. Let me know if it works.

  9. September 3, 2013

    I’ve had nothing but problems with vim-R on Fedora. Tmux and Screen do not work well with it. It’s a shame.

    • September 4, 2013

      I’m sorry to hear that. It works for me on Ubuntu. I hope they resolve some of the problems in future releases.

      • September 18, 2013

        Im actually pleased to say I’ve had marginal success with it now 🙂

Trackbacks and Pingbacks

  1. R with Vim on Mac OS X « Computational Mathematics
  2. R Studio « Brokering the Closure

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS