如何在没有扩展名的情况下使用单个 VS Code 键绑定运行多个命令?

Mar*_*ark 4 visual-studio-code vscode-extensions

是否可以在 VS Code 中从单个键绑定/快捷方式运行多个命令,就像宏操作一样,但没有扩展名?

我知道有一些扩展可以做到这一点,但是有没有内置的方法可以做到这一点?就像是:

{
  "command": "<run a few commands in sequence>"
  "key": "alt+r",
  "args": [  // some keybinding
    "editor.action.clipboardCopyAction",   // command ids from the Keyboard Shortcuts editors
    "workbench.action.files.newUntitledFile",  
    "editor.action.clipboardPasteAction",
  ]
}
Run Code Online (Sandbox Code Playgroud)

这些选项是否支持每个命令之间有延迟?

Mar*_*ark 14

现已在 Stable Build v1.77 中提供,有一个新的内置命令

  • runCommands // 按顺序运行一个或多个命令

它可以运行单个命令或多个命令 - 就像宏扩展一样。请参阅测试:运行多个命令的新命令 - runCommands。以下是几个键绑定示例(放入您的keybindings.json):

{
  "command": "runCommands",
  "key": "alt+r",                // whatever keybinding you want
  "args": {
    "commands": [

        "editor.action.clipboardCopyAction",

        "workbench.action.files.newUntitledFile",  
        "editor.action.clipboardPasteAction",

        // prompt for save immediately?
        "workbench.action.files.saveAs"
    ]
  },
  "when": "editorTextFocus"  // can use context keys
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将复制选定的文本,打开一个新文件,将复制的文本粘贴到其中,然后提示保存该文件。

{
  "command": "runCommands",
  "key": "alt+r",                // whatever keybinding you want
  "args": {
    "commands": [

      "editor.action.copyLinesDownAction",
      "cursorUp",
      "editor.action.addCommentLine",
      "cursorDown"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将复制当前行,对其进行注释,然后在下面添加相同的行(未注释)。

{
  "command": "runCommands",
  "key": "alt+r",                // whatever keybinding you want
  "args": {
    "commands": [

      {                                            // use commands that take args
        "command": "editor.actions.findWithArgs",
        "args": {
          "searchString": "trouble",
          // "regexp": true,
        }
      },
      "editor.action.selectAllMatches",
      "editor.action.commentLine"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

trouble上面的代码查找文件中的所有匹配项并注释这些行。

这个新runCommands命令目前无法替换现有的宏扩展,包括我最喜欢的多命令,因为其中一些有一个delayinterval选项可以在运行命令之间添加一些时间。我发现在某些情况下有必要具备这种能力,但它不是runCommands.

但在大多数情况下,runCommands命令之间不需要间隔。


我已经提交了一个问题,建议这个“宏”以原子方式操作 - 也就是说,当撤消Ctrl+Z它时,所有步骤/命令都应该立即撤消,而不是一次只撤消一个命令。如果想看到这种行为,请在 github 问题上投票:考虑将 runCommands 设置为“原子”


对于扩展开发人员来说,runCommands可以像这样运行命令:

const commandArray =  [
  "editor.action.copyLinesDownAction",
  "cursorUp",
  "editor.action.addCommentLine",
  "cursorDown"
];

await vscode.commands.executeCommand('runCommands', { commands: commandArray });
Run Code Online (Sandbox Code Playgroud)

或使用带参数的命令:

const commandArray = [
  { 
    "command": "workbench.action.files.newUntitledFile",
      "args": {
        "languageId": "typescript",
       }
  },
  {
    "command": "type",
      "args": {
        "text": "/* add some text here */"
      }
  }
];

await vscode.commands.executeCommand('runCommands', { commands: commandArray });
Run Code Online (Sandbox Code Playgroud)