【问题标题】:VS Code keyboard shortcut when multiple lines selected选择多行时的 VS Code 键盘快捷键
【发布时间】:2020-10-02 10:27:43
【问题描述】:

我试图在选择一行时为键盘快捷键设置不同的命令,而在选择多行时设置不同的命令。我试图在默认键绑定中找到示例,但没有成功。有什么办法可以做到这一点?我应该在键绑定文件中使用什么when 参数,也许还有其他方法?

【问题讨论】:

    标签: visual-studio-code key-bindings


    【解决方案1】:

    编辑器中有2个when关于选择的上下文

    • editorHasSelection
    • editorHasMultipleSelections

    如果您正在寻找选择是单个选择但多行的情况,那么您不走运,上下文不知道选择了什么。


    编辑

    我已经编写了一个扩展来修复键绑定上下文中的这个差距。

    使用Extra Context 并定义extraContext:editorSelectionHasMultipleLineswhen 子句一起使用。

    一个例子:

    settings.json:

      "multiCommand.commands": [
        {
          "command": "multiCommand.terminalSingleLine",
          "sequence": [
            { "command": "workbench.action.terminal.sendSequence",
              "args": { "text": "echo Single Line\u000D" }
            }
          ]
        },
        {
          "command": "multiCommand.terminalMultipleLine",
          "sequence": [
            { "command": "workbench.action.terminal.sendSequence",
              "args": { "text": "echo Multiple Lines\u000D" }
            }
          ]
        }
      ]
    

    keybindings.json:

      {
        "key": "ctrl+k f5", // or any other key combo
        "command": "multiCommand.terminalSingleLine",
        "when": "editorTextFocus && !extraContext:editorSelectionHasMultipleLines"
      },
      {
        "key": "ctrl+k f5",
        "command": "multiCommand.terminalMultipleLine",
        "when": "editorTextFocus && extraContext:editorSelectionHasMultipleLines"
      }
    

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多