首页 > 解决方案 > 在 VS Code 中一次击键执行多个命令

问题描述

下面是由多个命令组成的操作的一个示例。我想复制一行,复制它,然后,我需要返回上面的行并将其注释掉。目的是达到以下状态。

previousStatement();
// statementToBeMultipliedAndCommentedOut();
statementToBeMultipliedAndCommentedOut();
nextStatement();

今天,我通过这样的快速组合实现了这一目标。

ctrl+ c
ctrl+ v
up
ctrl+ k+ c//注释掉
down

有没有办法让组合在单个键绑定中执行这些击键?

标签: visual-studio-code

解决方案


你需要一个宏扩展,比如多命令,这样你就可以运行一系列命令。还有其他宏扩展。使用多命令:

在 settings.json 中:

"multiCommand.commands": [
  {
    "command": "multiCommand.commentDown",
    "sequence": [
      "editor.action.copyLinesDownAction",
      "cursorUp",
      "editor.action.addCommentLine",
      "cursorDown"
    ]
  }
]

这些命令可以在键盘快捷键列表中找到。在 SO 上搜索“多命令”以查看您可以使用它执行的一些操作。(我与它没有任何关系。)

在 keybindings.json 中选择一个键绑定:

{
  "key": "ctrl+shift+/",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.commentDown" },
  "when": "editorTextFocus"
},

推荐阅读