13

I use RStudio for working with R programming language and find the ctrl+enter shortcut to send a line to the console extremely useful in troubleshooting my work.

Now I am using sublimetext2 and I would like to do the same thing in RStudio, send a line to the console.

Is there a way to send the existing line to the console or a SublimeREPL console?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Tahnoon Pasha
  • 5,848
  • 14
  • 49
  • 75

3 Answers3

10

I don't know about the console, but this is possible with SublimeREPL.

As long as you have a REPL and a file of the same language open at the same time, you can send a line (or a selection or file) to your open REPL via the SublimeREPL Source Buffer Keys. By default, Ctrl+, followed by l sends the current line to the REPL, but you can change the hotkey to Ctrl+Enter (in Python only, to protect other languages' default Ctrl+Enter functionality) by adding these lines to the top of your Preferences -> Key Bindings – User file:

{ "keys": ["ctrl+enter"], "command": "repl_transfer_current", "args": {"scope": "lines"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "source.python", "match_all": true }
    ]
},

Other available scopes (from Preferences -> Browse Packages -> SublimeREPL/Default (Windows).sublime-keymap) are selection, file, and block (Clojure only). If you want to send a line to your REPL but not parse it immediately, you can add "action":"view_write" to the args object, like so:

{ "keys": ["ctrl+enter"], "command": "repl_transfer_current", "args": {"scope": "lines", "action": "view_write"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "source.python", "match_all": true }
    ]
},

See the Unofficial Sublime Text 2 Docs for more information on key bindings.

In the case that the REPL is open in a different tab than your source (rather than a separate view), the source buffer hotkeys will not focus the REPL. I'm sure it's possible to implement some sort of tab-swapping toggle key, but that sounds like a problem for another question.

angerson
  • 7,342
  • 1
  • 21
  • 24
  • That is terrific. Thank you @ProtractorNinja. A quick follow on question - is it possible to get both functions. So I'd like to send the line to the console and see it before it is executed. Thanks -- Sorry by console I mean REPL – Tahnoon Pasha May 17 '13 at 01:03
  • @TahnoonPasha Yes! Add the second larger code snippet to your `Key Bindings – User` file alongside the first and adjust the `"keys"` value to whatever you want the send-but-don't-evaluate key to be, say, `ctrl+shift+enter`. Then `ctrl+enter` will send *and* evaluate the line, whereas `ctrl+shift+enter` will send and *not* evaluate the line. If you aren't interested in custom keypresses, the default send-but-don't-evaluate key combination is `ctrl+shift+,` followed by `l`. – angerson May 17 '13 at 01:11
  • Thanks @ProtractorNinja I think I must be doing something wrong. When I do the send-and-evaluate it doesn't echo in the REPL terminal, it just evaluates and returns an extra `>>>` when its done. What I was hoping was if I had e.g `import imaplib` and I did `ctrl+enter` I would see `>>>import imaplib` which would evaluate and then a new `>>>`. Is your code expected to have that effect? Thanks again for your help with this. – Tahnoon Pasha May 17 '13 at 01:32
  • @TahnoonPasha Oh! My mistake; I misunderstood your request. I don't think that what you want is possible in SublimeREPL's current state—I looked at the source code and you'd probably have to write a command chaining plugin, even though what you want *seems* like a simple demand. Hopefully someone else can prove me wrong. Sorry I can't be of more help! – angerson May 17 '13 at 02:58
  • Thanks @ProtractorNinja, still incredibly useful to be able to send it line by line. Appreciate the help. – Tahnoon Pasha May 17 '13 at 02:59
  • @ProtractorNinja, is it possible to make the cursor move to the next line in the source file upon executing Ctrl+Enter? – Richard Sep 25 '15 at 18:02
  • @Richard I'm not sure -- I stopped using Sublime Text long ago. Try looking at the keybinding docs I linked to above, and the [SublimeREPL](https://github.com/wuub/SublimeREPL) keybindings. – angerson Sep 26 '15 at 18:48
  • Okay, thanks. Do you mind if I asked what you switched to (always keeping an eye out for good tools)? – Richard Sep 27 '15 at 01:52
  • @Richard I use Vim these days. – angerson Sep 27 '15 at 16:30
4

In addition to setting up your own key bindings, you can simply install Enhanced-R:

In Sublime:

  • Cmd + Shift + P (to bring up the command palette)
  • type "Install Package"
  • Navigate to Enhanced-R

If you are using Sublime for mostly just R, then you can set the default syntax for the whole app. Or you can change it per file (Cmd + Shift + P again, then start typing Syntax Enhanced R)

Then, like you are used to in RStudio, you simply hit Cmd + enter to ship the code to the Console or R.app etc

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • With SublimeREPL I can open the R console within Sublime Text, is there a similar option for Enhanced-R? – adibender Nov 02 '13 at 22:55
  • @adibender you can use `sublimeREPL` and `Enhanced-R` simultaneously. Personally, I've done away with `sublimeREPL` because it was getting annoying to deal with the window sizing. I rather have two windows I can `CMD-tab` (or `CMD-\``) between – Ricardo Saporta Nov 02 '13 at 23:42
  • thx for the reply, but I couldn't quite get it to work. I can open R via SublimeREPL and I can send R-Code via Enhanced-R to the Rgui, but not send R-Code to SublimeREPL R-Console (via Enhanced-R short cuts). But I'll just post a question if I can't get it working after a couple more tries... – adibender Nov 03 '13 at 14:52
  • I haven't used sublimeREPL in ages, so I really dont know. Have a look at the keybindings and the preferences. It is after all, all open source :) – Ricardo Saporta Nov 03 '13 at 16:45
  • hey @RicardoSaporta can I just ask, do you know by any chance how to get sublime send the code to the macOS terminal instead of the R gui? i tried to change the enhanced R package settings to `"App" : "Terminal"`, but it still opens and uses `R.app` whenever I hit cmd+enter. cheers – Florian Oswald Mar 29 '14 at 16:04
  • @FlorianOswald, that's peculiar and shouldn't happen. Make sure you are using the latest version of ST3 & Enhanced R. If it still is occurring, submit an issue at https://github.com/randy3k/Enhanced-R -- the developer is very responsive. – Ricardo Saporta Mar 30 '14 at 19:04
  • @RicardoSaporta blimey, i was using ST2. updated to ST3 and it works! that was a quick fix. thanks! – Florian Oswald Mar 30 '14 at 22:44
1

Sending raw R code to SublimeREPL does work now:

  1. Bring up the Cmd/Ctrl+Shift+P menu
  2. Select R Application Switch
  3. Select SublimeREPL

When you have SublimeREPL active, you'll be able to send raw R to it with Cmd/Ctrl+Enter.

Note that by default, SublimeREPL won't display the code that gets sent in; it'll just show you the output. If you want to also see the code, you can change your user settings:

  1. Navigate to Preferences -> Package settings -> SublimeREPL -> Settings - User
  2. Turn on the show_transferred_text setting.

For example, if you don't have any other settings, your settings should look like this:

{
    "show_transferred_text": true
}
ellotheth
  • 4,333
  • 2
  • 17
  • 29