我在 VS Code 中有以下键绑定,可以在活动文档和内置终端之间切换光标的位置:
// Toggle between terminal and editor focus
{
"key": "oem_8",
"command": "workbench.action.terminal.focus"
},
{
"key": "oem_8",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
在我点击快捷键将光标移动到终端之前,我首先必须保存活动文件。
因此我想运行文件保存命令,在谷歌搜索后我相信它是
workbench.action.files.save
我该怎么做?我试过在“命令行”行的末尾添加上面的代码片段,但没有成功。
更新:为 vscode v1.77 发布,更多内容在run multiple commands like a macro.
你可以这样做:
{
"command": "runCommands",
"key": "alt+r", // whatever keybinding
"args": {
"commands": [
// commands to run in sequence
"workbench.action.files.save",
"workbench.action.terminal.focus"
]
}
}
该命令是内置的,因此您的用例不需要扩展。但是请参阅上面的链接,某些用例可能需要宏扩展。
上一个答案:
你需要一个宏扩展来从一个键绑定运行多个命令。
我现在使用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
中构建它们。
有一种方法可以在没有任何扩展的情况下运行一系列命令。它是通过使用任务。我喜欢这种方法,因为它允许定义一个命令,该命令在工作区范围内,而不是在全局范围内。这个想法来自this 博客文章。
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"
}
注意,在定义任务时,您可以在输入变量的帮助下将参数传递给命令。
运行多个命令的另一个扩展:Commands
{
"key": "oem_8",
"command": "commands.run",
"args": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
],
"when": "editorTextFocus"
}
我做了这个扩展。太棒了