Home > Software design >  how to add keybindins in visual studio code for R terminal
how to add keybindins in visual studio code for R terminal

Time:03-27

I've succesfully added keybindings for %>% and <- when working with Visual Studio Code in R scripts or Rmarkdowns, by adding the following to my keybindings.json file:

    {
      "key": "Ctrl Shift m",
      "command": "type",
      "args": { "text": " %>% " },
      "when": "editorTextFocus && editorLangId == r"
    },
    {
      "key": "Alt -",
      "command": "type",
      "args": { "text": " <- " },
      "when": "editorTextFocus && editorLangId == r"
    },
    {
      "key": "Ctrl Shift m",
      "command": "type",
      "args": { "text": " %>% " },
      "when": "editorTextFocus && editorLangId == rmd"
    },
    {
      "key": "Alt -",
      "command": "type",
      "args": { "text": " <- " },
      "when": "editorTextFocus && editorLangId == rmd"
    },

But I can't figure how to do that for my R terminal inside Visual Studio Code. Any ideas?

CodePudding user response:

You will need to change the when part to have terminalFocus. For instance, if you want to set up Ctrl l to clear terminal while the focus is in terminal:

{
    "key": "ctrl l",
    "command": "workbench.action.terminal.clear",
    "when": "terminalFocus"
},

CodePudding user response:

@AdroMine found the way to do it. I'll just post the updated keybindings.json so that others can use it:

[
    // OTHER KEYBINDINGS,


 
    // keybindings for R scripts. 
    {
      "key": "Ctrl Shift m",
      "command": "type",
      "args": { "text": " %>% " },
      "when": "editorTextFocus && editorLangId == r"
    },
    {
      "key": "Alt -",
      "command": "type",
      "args": { "text": " <- " },
      "when": "editorTextFocus && editorLangId == r"
    },
    // keybindings for Rmarkdown
    {
      "key": "Ctrl Shift m",
      "command": "type",
      "args": { "text": " %>% " },
      "when": "editorTextFocus && editorLangId == rmd"
    },
    {
      "key": "Alt -",
      "command": "type",
      "args": { "text": " <- " },
      "when": "editorTextFocus && editorLangId == rmd"
    },
    // keybindings for R terminal (radian included)
    {
      "key": "Ctrl Shift m",
      "command": "workbench.action.terminal.sendSequence",
      "args": { "text": " %>% " },
      "when": "terminalFocus"
    },
    {
      "key": "Alt -",
      "command": "workbench.action.terminal.sendSequence",
      "args": { "text": " <- " },
      "when": "terminalFocus"
    },
    // OTHER KEYBINDINGS

]



  • Related