Search code examples
rvisual-studio-code

How do I add 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

  • 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"
        }
    ]
    

    If you are a Mac user and prefer RStudio's Cmd+Shift+m shortcut, set the above key line to "key": "Cmd+Shift+m".

    If you are new to VSCode, here is a helpful how-to video.