REditorSupport / vscode-R

R Extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=REditorSupport.r
MIT License
1.07k stars 128 forks source link

How to allocate a keyboard shortcut to Restart R? #1481

Closed francisbarton closed 8 months ago

francisbarton commented 9 months ago

I can see from the wiki that rstudioapi::restartSession() is implemented in VS Code, and indeed I can run this from the R console no problem.

But what I would like to be able to do is allocate a keybinding for this command, as I can in RStudio.

VS Code doesn't seem to support custom commands for keybindings (?? let me know if I am wrong about this) - you can only set a keybinding for a command that already exists within the command palette.

I don't think rstudioapi::restartSession() exists within the command palette - I can't find it by searching there, anyway.

I would like it if this command was available in the command palette [or if some equivalent way of being able to set up a keyboard shortcut to restart R could be provided or pointed out to me.]

The Julia language extension has the Julia: Restart Language Server command available. I can't see an R equivalent.

image

Thank you!

Edit: apparently it requires the command to be added with registerCommand - so this issue is now basically a feature request for that!

thedbcooper commented 8 months ago

Does the "Kill active terminal instance" command accomplish this for you in the mean time?

conig commented 8 months ago

You can just add the command to your keybindings.json.

E.g.,

  {
    "description": "Restart RSession",
    "key": "ctrl+alt+shift+s",
    "command": "r.runCommand",
    "when": "editorTextFocus && (editorLangId == r || editorLangId == rmd)",
    "args": "rstudioapi::restartSession()"
  },
francisbarton commented 8 months ago

Thanks so much @conig. I've got various custom keybindings already set up but I had no idea that you could assign an arbitrary R command using r.runCommand - that's excellent and solves the problem perfectly.

edit: except it doesn't, because my preferred keybinding shift+alt+r just prints a capital R at the console instead of executing the command.

I'm actually finding @thedbcooper's idea works fine, one keybinding for kill and another one that launches a fresh R terminal, very easy.