visual studio 代码键绑定 - 运行 两个或更多命令与一个快捷方式
visual studio code keybindings - Running two or more commands with one shortcut
我在 VS 代码中有以下键绑定,可以在活动文档和内置终端之间切换光标的位置:
// Toggle between terminal and editor focus
{
"key": "oem_8",
"command": "workbench.action.terminal.focus"
},
{
"key": "oem_8",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
在点击快捷键将光标移动到终端之前,我首先要保存活动文件。
因此我想 运行 文件保存命令,在 google 上搜索后我认为是:workbench.action.files.save
请问我该怎么做?我尝试在 "command" 行的末尾添加上面的代码片段,但它没有用。
干杯
您需要一个宏扩展来 运行 来自一个键绑定的多个命令。
我现在使用 multi-command,现在还有其他宏扩展。
您可以将此键绑定(在您的 keybindings.json
中)与 multi-command
扩展一起使用 - 在 settings.json
:
中不需要任何东西
{
"key": "oem_8", // or whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
]
},
"when": "editorTextFocus" // if you want this, you probably do
}
如果您有更复杂的宏,您仍然可以根据需要在 settings.json
中构建它们。
运行 多个命令的另一个扩展:Commands
{
"key": "oem_8",
"command": "commands.run",
"args": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
],
"when": "editorTextFocus"
}
我做了这个扩展。太棒了。
有一种方法 运行 没有任何扩展的命令序列。它是通过使用任务。我喜欢这种方法,因为它允许定义一个命令,该命令在工作区范围内,而不是在全局范围内。这个想法来自 this 博客 post。
tasks.json
示例:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd-1",
"command": "${command:workbench.action.files.save}"
},
{
"label": "cmd-2",
"command": "${command:workbench.action.terminal.focus}"
},
{
"label": "cmd-All",
"dependsOrder": "sequence",
"dependsOn": [
"cmd-1",
"cmd-2"
],
}
]
}
然后在 keybindings.json
中,您只需将热键绑定到任务即可:
{
"key": "oem_8",
"command": "workbench.action.tasks.runTask",
"args": "cmd-All"
}
注意,在定义任务时,您可以在输入变量的帮助下将参数传递给命令。
我在 VS 代码中有以下键绑定,可以在活动文档和内置终端之间切换光标的位置:
// Toggle between terminal and editor focus
{
"key": "oem_8",
"command": "workbench.action.terminal.focus"
},
{
"key": "oem_8",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
在点击快捷键将光标移动到终端之前,我首先要保存活动文件。
因此我想 运行 文件保存命令,在 google 上搜索后我认为是:workbench.action.files.save
请问我该怎么做?我尝试在 "command" 行的末尾添加上面的代码片段,但它没有用。
干杯
您需要一个宏扩展来 运行 来自一个键绑定的多个命令。
我现在使用 multi-command,现在还有其他宏扩展。
您可以将此键绑定(在您的 keybindings.json
中)与 multi-command
扩展一起使用 - 在 settings.json
:
{
"key": "oem_8", // or whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
]
},
"when": "editorTextFocus" // if you want this, you probably do
}
如果您有更复杂的宏,您仍然可以根据需要在 settings.json
中构建它们。
运行 多个命令的另一个扩展:Commands
{
"key": "oem_8",
"command": "commands.run",
"args": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
],
"when": "editorTextFocus"
}
我做了这个扩展。太棒了。
有一种方法 运行 没有任何扩展的命令序列。它是通过使用任务。我喜欢这种方法,因为它允许定义一个命令,该命令在工作区范围内,而不是在全局范围内。这个想法来自 this 博客 post。
tasks.json
示例:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd-1",
"command": "${command:workbench.action.files.save}"
},
{
"label": "cmd-2",
"command": "${command:workbench.action.terminal.focus}"
},
{
"label": "cmd-All",
"dependsOrder": "sequence",
"dependsOn": [
"cmd-1",
"cmd-2"
],
}
]
}
然后在 keybindings.json
中,您只需将热键绑定到任务即可:
{
"key": "oem_8",
"command": "workbench.action.tasks.runTask",
"args": "cmd-All"
}
注意,在定义任务时,您可以在输入变量的帮助下将参数传递给命令。