我如何在 vscode 中获得来自 R 的 magrittr 管道的别名

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

我想要在 vscode(R 中的管道命令)中键入 %>% 的别名。在 Rstudio 中,它被映射到 ctrl + shift + M,但如果由于任何原因这在 vscode 中不可用,我很乐意映射到其他东西,我只是不确定如何添加新别名。

我不使用 vscode,但也许宏可以使用 https://marketplace.visualstudio.com/items?itemName=geddski.macros。它在 向命令传递参数 部分中说:

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

也许这会奏效(未经测试)。将此添加到您的 settings.json:

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

这给你的 keybindings.json:

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

您只需将其添加到您的 keybindings.json 文件(请参阅 here 了解如何打开它):

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

这样你就不需要宏了


keybindings.json修改后的文件:

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

你也可以通过以下方式添加到rmd中

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

并通过以下方式到R端

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

这就是我的 settings.json 将 %>% 和 <- 添加到我的 Rmarkdowns、Rscripts 和 R 终端(包括弧度)的方式:

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

]