Repository navigation
provide rstudio-like keymaps #551
Copy link
Copy link
Closed
Description
Describe the solution you'd like
I'd love it if there was a way to quickly get the key bindings that I (and I suspect many others) have muscle-memoried from RStudio.
Caveats:
- This should only cover R-specific keybindings, such as for
devtools::load_all()orR CMD checkand not the general-purpose IDE/Editor bindings (i.e. CTRL+1 for "move to source editor".
VSCode is a different beast and has a completely different design, so it doesn't make sense for it to behave like RStudio all around.
As a heuristic, only those keybindings should be enabled which only make sense inside R projects.
If there's already a native, cross-language VSCode shortcut, that should take precedence. - Consequently, this should only trigger when
"when": "editorLangId == r || editorLangId == rmd && editorTextFocus". - Some default VSCode keybindings would have to be overwritten (see below).
- The whole thing could maybe be enabled as an option in the setting.
Here's what I have so far (using tasks as per #59):
{
"description": "Insert <-",
"key": "alt+-",
"command": "type",
"when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
"args": {
"text": "<- "
}
},
{
"description": "Insert %>%",
"key": "ctrl+shift+m",
"command": "type",
"when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
"args": {
"text": "%>% "
}
},
{
// "description": "Disable (overloaded with above)",
"key": "ctrl+shift+m",
"when": "editorLangId == r || editorLangId == rmd && editorTextFocus",
"command": "-workbench.actions.view.problems"
},
{
"description": "Load all",
"key": "cmd+shift+l",
"command": "r.runCommand",
"when": "editorLangId == r || editorLangId == rmd",
"args": "devtools::load_all()"
},
{
"description": "Test",
"key": "cmd+shift+t",
"command": "workbench.action.tasks.runTask",
"when": "editorLangId == r || editorLangId == rmd",
"args": "Test"
},
{
"description": "Document",
"key": "cmd+shift+d",
"command": "workbench.action.tasks.runTask",
"when": "editorLangId == r || editorLangId == rmd",
"args": "Document"
},
{
"description": "Check",
"key": "cmd+shift+e",
"command": "workbench.action.tasks.runTask",
"when": "editorLangId == r || editorLangId == rmd",
"args": "Check"
},Reactions are currently unavailable
Activity
maxheld83 commentedon Feb 11, 2021
Is there a better way to identify whether you're currently "in an R project", other than
"when": "editorLangId == r || editorLangId == rmd"?Maybe when there's a
DESCRIPTIONat project root?Is there already such a facility inside vscode-R?
editorLangId == r || editorLangId == rmd && editorTextFocus"doesn't quite create the RStudio experience. Whenever you're in a terminal (no way of knowing whether it's running radian at any point), or one of the non-R files (say,DESCRIPTION,README.md, ...) these keybindings won't fire.ManuelHentschel commentedon Feb 11, 2021
It's possible to set arbitrary context values from within the extension. This is done e.g. to only show help panel related navigation buttons when the active panel is actually an R help panel. See e.g.
https://github.com/Ikuyadeu/vscode-R/blob/39f5914d691a3befb04768e4df33b7f83978fd44/package.json#L709
https://github.com/Ikuyadeu/vscode-R/blob/39f5914d691a3befb04768e4df33b7f83978fd44/src/rHelpPanel.ts#L113
https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context
This could be used to e.g. set a context value
r.isRProjectthat is true whenever we identify the current project as an R project, orr.terminalIsRto indicate that the currently active terminal window is one opened by the extension.bersbersbers commentedon Mar 9, 2021
I found this issue because I fell for an unexpected key mapping collision: in RStudio, Ctrl+Shift+S runs the source, while that combination in VS Code (and many other tools, compare https://defkey.com/what-means/ctrl-shift-s) is usually used for "File - Save As". So instead of re-saving my file, I accidentally ran some R code in VS Code due to
https://github.com/Ikuyadeu/vscode-R/blob/abd2392d7ef02ea5151b26004368fdedfcc979f4/package.json#L651-L656
In general, I am not sure it is beneficial to emulate RStudio behavior at all. RStudio is only for people working (mostly) with R. VS Code is not only for people who mostly use R, but people also use JavaScript, Python, etc. - often several languages across the same project. So I think I would put more focus on being consistent within the VS Code environment rather than compatibility with RStudio. Please consider that if and when revising any keyboard shortcuts (including Ctrl+Shift+S). It's easy for me to unmap*, but new users will always fall for that.
*How? Put this in your
keybindings.json:malcook commentedon Mar 9, 2021
@bersbersbers cogently make a point I too arrived at after at first being delighted to find emacs keybinding emulation and then throwing up my hands in frustration as I found they introduced conflicts with other things I wanted, including the ability to follow along others' VS Code tutorials & recipes that were given in terms of standard bindings.
Of course, TIMTOWTDI.
Or, À chacun son goût.
Or YMMV.
For me, I'm hoping if/when notebooks allow mixing cells between different languages/kernels, someone will think through how to be consistent in bindings between them. This will be a stickier wicket. Living in emacs org-mode now with poor consistency in this regard is a bit of a pain.
(mostly lurking, but thanks for vscode-R)
andycraig commentedon Mar 13, 2021
@maxheld83 Thank you for the suggestion! I'm inclined to agree with @bersbersbers and @malcook that these shouldn't be defaults. A setting to enable RStudio-like keymaps (disabled by default) is an option although someone would need to volunteer to implement it.
We have a keybindings page in the Wiki: https://github.com/Ikuyadeu/vscode-R/wiki/Keyboard-shortcuts We could add the suggested keybindings to an RStudio-like keymaps section there? Then users who want them can do a quick copy-paste into their
keybindings.json.bersbersbers commentedon Mar 13, 2021
You could also add a default-false setting such as
config.latex-workshop.bind.altKeymap.enabledin LaTeX Workshop and scope two sets of key bindings to that. That's how they implement it:https://github.com/James-Yu/LaTeX-Workshop/blob/f6016f6dd15992894935545e25ef1a6a2b6ef411/package.json#L481-L577
I think this is a great idea in general, and it would allow maintaining one (default) set of key bindings with maximum compatibility to VS Code, and another for RStudio users that can be enabled using a single setting.
krlmlr commentedon Mar 17, 2021
The flymaps extension provides a keymap for Alt+- and Ctrl+Shift+M, a good start I think. Continuing the keymap development there would allow this extension to be installed and perhaps configured separately.
https://github.com/ShixiangWang/flymaps
andycraig commentedon Apr 8, 2021
Doing this via the flymaps extension sounds fine, and adding a setting to switch on RStudio-like keymaps sounds fine too. I'll mark this as help wanted - volunteers welcome.
ThomasSoeiro commentedon Sep 23, 2026
https://github.com/ShixiangWang/flymaps is no longer under active development at the time of writing (last commit 6 years ago).
The issue is tagged with "help wanted" since 2021 but there has been no move since then, so perhaps we can close as not planned now? Users can follow the wiki and copy the shortcuts they want.
@randy3k @eitsupi @grantmcdermott @Fred-Wu any opinion?
ThomasSoeiro commentedon Sep 27, 2026
@eitsupi @grantmcdermott @Fred-Wu I suggest converting to discussion