【问题标题】:VS Code - Selection to multi cursorVS Code - 选择多光标
【发布时间】:2022-01-05 13:52:37
【问题描述】:

有谁知道如何将 vscode 编辑器选择范围变成多光标选择?

例如在这一行: “在这条线之外,我想选择这个|”

  1. 运行键绑定 选择会像这样变成多光标 “在这一行之外,我想|选择这个|

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    您可以使用扩展名Select By v1.10

    执行命令:SelectBy:为选择的锚点和活动位置创建单独的光标selectby.anchorAndActiveSeparate

    它将为所有当前选择的每个锚点和活动位置创建新光标。消除了重叠的光标。

    【讨论】:

    • 有没有办法强制激活成为两者中的第一个,不管你用哪种方式拖动?
    【解决方案2】:

    假设您只使用一个选择:

    const selection = vscode.window.activeTextEditor.selections[0];
    
    const newSelection1 = new vscode.Selection(selection.start, selection.start);
    const newSelection2 = new vscode.Selection(selection.end, selection.end);
    
    vscode.window.activeTextEditor.selections = [newSelection1, newSelection2];
    

    选择从左到右和从右到左会得到相同的结果。

    我把它做成了一个扩展:Convert Selection - 它适用于多个选择。

    这是扩展的完整代码:

    const vscode = require('vscode');
    
    
    /**
     * @param {vscode.ExtensionContext} context
     */
    function activate(context) {
    
        let disposable = vscode.commands.registerCommand('convert-selection.surround', function () {
    
          const editor = vscode.window.activeTextEditor;
          const selections = editor.selections;
          const newSelections = [];
        
          for (const selection of selections) {
            const newSelection1 = new vscode.Selection(selection.start, selection.start);
            const newSelection2 = new vscode.Selection(selection.end, selection.end);
            newSelections.push(newSelection1, newSelection2);
          }
    
          editor.selections = newSelections;
        });
    
        context.subscriptions.push(disposable);
    }
    
    exports.activate = activate;
    

    【讨论】:

    • 是否有一种香草(不带扩展)的方式在键绑定上运行它?如果没有,你有推荐的吗?
    • 自从你标记了 vscode-extensions 我以为你正在构建一个。这样做很容易。如果你愿意,我可以把它打包成一个扩展。
    • 非常感谢,但我认为这没有必要。看来 rioV8s 扩展建议正是我所需要的。
    • 好吧,我已经发布了扩展Convert Selection。还没有写自述文件。默认键绑定是Alt+s,命令是convert-selection.surroundSurround selection(s) with multi-cursors
    • 不错,我下载了。还有一个好处是它总是让第一个多光标成为活动光标,所以当你点击转义时它总是使用第一个。
    猜你喜欢
    • 2019-06-03
    • 2018-06-23
    • 2019-04-22
    • 1970-01-01
    • 2022-08-13
    • 2019-04-16
    • 2017-04-16
    • 2022-12-29
    • 1970-01-01
    相关资源
    最近更新 更多