如何在vscode中为来自R的magrittr管道添加别名

zi8p0yeb  于 2023-03-05  发布在  Vscode
关注(0)|答案(3)|浏览(149)

我想在vscode中输入%>%时使用别名(R中的管道命令)。在Rstudio中,它被Map为ctrl + shift + M,但如果由于某种原因在vscode中不可用,我很乐意Map为其他东西,我只是不知道如何添加新的别名。

mm5n2pyu

mm5n2pyu1#

你只需要把它添加到你的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"
    }
]

如果您是Mac用户并且喜欢RStudio的Cmd+Shift+m快捷方式,请将上面的键行设置为"key": "Cmd+Shift+m"
如果您不熟悉VSCode,here是一个有用的操作视频。

iszxjhcz

iszxjhcz2#

您还可以通过以下方式将其添加到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

]
pinkon5k

pinkon5k3#

我不使用vscode,但也许宏可以使用https://marketplace.visualstudio.com/items?itemName=geddski.macros工作,它在向命令传递参数部分中说:
许多命令都接受参数,比如"type"命令,它允许您在编辑器中插入文本。
也许这会起作用(未经测试)。将此添加到您的settings.json

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

然后将这个输入到keybindings.json

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

相关问题