在组合键上配置多个命令

Configure multiple commands on key combination

在 Visual Studio 代码中,File > Preferences > Keyboard 快捷方式菜单,我可以覆盖 keybindings.json 中的默认绑定。但是如何在一个键上添加多个绑定呢?我不想在按下 ctrl+s

时做类似保存和格式代码的操作

{ "key": "ctrl+s","command": "workbench.action.files.save,editor.action.format" }

这可行吗?

据我所知,这是目前不可能的,因为第一个匹配的键盘快捷键获胜(从下到上搜索)并且没有进一步的快捷键被评估 - 来自 docs:

When a key is pressed:

  • the rules are evaluated from bottom to top.
  • the first rule that matches, both the key and in terms of when, is accepted.
  • no more rules are processed.
  • if a rule is found and has a command set, the command is executed.

也就是说,似乎有人有同样的愿望并为此写了一个扩展 - 参见 gyuha.format-on-save
但是我没有亲自测试该扩展,所以我无法告诉您它的效果如何

使用此处所示的 when 子句,其中我将 ctrl+enter 连接到仅 .py[thon] 脚本编辑器扩展处于活动状态的情况,这类似于 ctrl+enter 文件 |喜好 | .R 脚本编辑器扩展启用的键盘快捷键。

[    
    {
        "key": "ctrl+enter",
        "command": "python.execSelectionInTerminal",
        "when": "editorTextFocus && editorLangId == 'python'"
    }
]