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

问题描述 投票:0回答:3

我想要一个用于在 VScode 中输入

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

r visual-studio-code
3个回答
38
投票

您只需将其添加到您的

keybindings.json
文件中(请参阅此处了解如何打开它):

{
  "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 新手,此处是一个有用的操作视频。


14
投票

您也可以通过以下方式添加到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 添加 %>% 和 <- 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

]




0
投票

我不使用 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"
}
© www.soinside.com 2019 - 2024. All rights reserved.