Search code examples
rvisual-studio-codekeyboard-shortcuts

Keyboard shortcut for R assignment operator (<-) in VSCode in editor and terminal


How would I add a keyboard shortcut for the basic assignment operator (<-) that works both in the editor AND the terminal?

Getting it to work in the editor is straightforward (content goes into keybindings.json):

{
  "key": "alt+-",
  "command": "type",
  "when": "editorLangId == r && editorTextFocus || editorLangId == rmd && editorTextFocus",
  // if you want using quarto, try this
  // "when": "editorLangId =~ /r|rmd|qmd/ && editorTextFocus",
  "args": {"text": " <- "}
}

But I'm struggling with understanding what the when clause would need to look like for the terminal.

Things I tried based on the official doc on when clauses:

  • using terminalFocus
{
    "key": "alt+-",
    "command": "type",
    // "when": "editorLangId == r && editorTextFocus || editorLangId == rmd && editorTextFocus || terminalFocus",
    "args": {"text": " <- "}
}
{
    "key": "alt+-",
    "command": "type",
    // "when": "editorLangId == r && editorTextFocus || editorLangId == rmd && editorTextFocus || editorLangId == shellscript && terminalFocus",
    "args": {"text": " <- "}
}

Solution

  • You can't use the type command to write to the terminal. Try this instead:

    {
      "key": "alt+-",  // or whatever keybinding you want
      "command": "workbench.action.terminal.sendSequence",
      "args": {
          "text": " <- "
      },
      "when": "terminalFocus && !terminalTextSelected"
    }
    

    See send text to terminal docs