使用插件配置vscode快捷键支持多command命令

作者:matrix 发布时间:2022年9月12日 分类:零零星星

配置快捷键运行多条命令,目前没有看到官方的解决方案,使用三方插件支持

测试环境:

Mac vscode 默认语言

multi-command插件

https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command

先安装multi-command~

打开配置页

打开vscode命令面板按F1或者command + shift + p

搜索 open keyboard shortcuts,选择Open Keyboard Shortcuts (JSON)

图片5545-vscode cmd-k快捷键清空信息

我之前有次修改过cmd-k快捷键,目的是清空code-runner插件运行的命令行信息(清屏)。
vscode默认的cmd-k执行场景有限,当初是把触发条件when修改为终端或者编辑器获取焦点就行。

...
{
"key": "cmd+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus && terminalProcessSupported || editorFocus"
}
...

但是现在需要terminaloutput都执行清空,也就是执行两条命令。我尝试了多次都失败了,最后也只好乖乖安装插件搞定。

自定义快捷键

按照上面操作,正常打开keybindings.json文件,
或者自己打开配置文件默认路径:

~/Library/Application Support/Code/User/keybindings.json

我需要执行多个消息清空的命令,按下面配置即可

// 将键绑定放在此文件中以覆盖默认值auto[]
[
//...
{
"key": "cmd+k",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.terminal.clear",
"workbench.output.action.clearOutput",
]
},
"when": "terminalFocus && terminalProcessSupported || editorFocus"
},
//...
]

说明:
command参数执行插件入口命令extension.multiCommand.execute
sequence参数配置需要执行的命令(按序执行)
when参数控制触发条件

配置完成之后,会立即生效~ 这样就可以了

找到自己的command

你自定义的快捷键可能会用到vscode的其他命令,可以使用下面方法找到完整command命令

  • 打开vscode命令面板按F1或者command + shift + p

  • 搜索命令关键字

  • 点击右侧设置按钮

图片5558-使用插件配置vscode快捷键支持多command命令

  • 右键Copy Command ID

图片5558-使用插件配置vscode快捷键支持多command命令2

参考:

when参数可用context:
https://code.visualstudio.com/api/references/when-clause-contexts

不使用插件的方法:
https://dae.me/blog/2603/vs-code-bind-one-key-to-multiple-commands-without-an-extension/