'How do I had an alias for magrittr pipe from R in vscode

I would like an alias for typing %>% in vscode (the pipe command in R) . In Rstudio this is mapped to ctrl + shift + M, but if this is not available in vscode for any reason I am happy to map to something else, I am just not sure how to add a new alias.



Solution 1:[1]

You only need to add this to your keybindings.json file (see here to know how to open it):

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

This way you don't need macros


keybindings.json file after modification:

// Place your key bindings in this file to override the defaults
[
    {
        "key": "Ctrl+Shift+m",
        "command": "type",
        "args": { "text": " %>% " },
        "when": "editorTextFocus && editorLangId == r"
    }
]

Solution 2:[2]

You can also add it to rmd in the following way

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

and to the R terminal in the following way

    {
      "key": "Ctrl+Shift+m",
      "command": "workbench.action.terminal.sendSequence",
      "args": { "text": " %>% " },
      "when": "terminalFocus"
    },

This is how my settings.json look to add %>% and <- to my Rmarkdowns, Rscripts and R terminal (including radian):

[
    // 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

]



Solution 3:[3]

I don't use vscode, but perhaps a macro can work using https://marketplace.visualstudio.com/items?itemName=geddski.macros. It says in the Passing Arguments to Commands section:

Many commands accept arguments, like the "type" command which lets you insert text into the editor.

Perhaps this will work (untested). Add this to your settings.json:

"macros": {
  "addPipe": [
    "cursorEnd",
      {"command": "type", "args": {"text": "%>%"}}
  ]
}

and this to your keybindings.json:

{
  "key": "ctrl+shift+M",
  "command": "macros.addPipe"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 r2evans