I'm using MacVim, and I want to write codes for R in MacVim.
I want to have a hotkey which can:

  • Send a line where the cursor is in MacVim to RStudio, execute the code in RStudio, and back a cursor to MacVim (line by line execution)
  • If Visual Mode is used in MacVim, copy the chunk selected in MacVim and send it to RStudio (copy lines and paste on RStudio), execute the lines and back a cursor to MacVim (chunk execution)

Is it possible to realize this hotkey using what is prepared in Vim?

  • 1
    You're asking us to program your fancy idea. How about just using "*y and copying it to RStudio yourself.
    – hgiesel
    Jun 11 '16 at 8:30
  • That is what I'm doing now, so I want to speed up my coding. I know there is VimMode in RStudio, but it would be better for me to use MacVim itself. I've found similar thing can be done in GVIM (latest comments in this discussion). Same in MacVim?
    – user51966
    Jun 11 '16 at 8:33
  • 1
    It may be doable by calling some Applescript within vim which does all the steps you want to (activate RStudio, selecting textwindow, pasting, selecting some menu item from GUI, etc.). But that is terribly ugly and very slow, if you've ever seen that. Also is there no way to execute R from the terminal?
    – hgiesel
    Jun 11 '16 at 8:39
  • 1
    You can do that. If you know the command line to access rstudio (or the R compiler itself) and pass arguments for it, you can. An example with shell : system('expression') will do. You can insert the line into this expression or create a function which takes the lines and pass it to compiler or any other executable. Give dome example lines and the command line for executing in R. I am not aware whether you use MacVim within RStudio, like vrapper in eclipse. Give more details.
    – SibiCoder
    Jun 11 '16 at 8:40
  • @SibiCoder I think Nvim-R does the similar thing (passing a line from Vim to R run in Terminal). I currently run MacVim and RStudio independently (just open them one by one), not MacVim in RStudio.
    – user51966
    Jun 11 '16 at 8:46

I don't know if you have Rscript installed, but you should have. If you don't install R using this tutorial.

Just run Terminal.app and run Rscript and if says "command not found", you need to install R using the terminal.

If Rscript now works, you can run R code directly from vim. First copy the R code from vim (using y) to the unnamed register ("), which is the register vim uses, if you don't define a register. Now use the cmdline mode (with :) and type:

echo system("read arg; Rscript -e $arg", @")

The script should run, and the output is displayed on your screen.

  • The questioner would like to "[s]end a line where the cursor is in MacVim to RStudio".
    – SabreWolfy
    Jun 11 '16 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.