Philip Aarseth
Philip Aarseth

Reputation: 311

VS Code - Selection to multi cursor

Does anyone know how to turn an vscode editor selection range into a multi cursor selection?

for instance on this line: "out of this line I'd like to select this|" selection range

  1. run the key bind selection would turn into multi cursor like so "out of this line I'd like to | select this |"

multi cursor selection

Upvotes: 0

Views: 487

Answers (2)

Mark
Mark

Reputation: 182621

Assuming you are working with only a single selection:

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];

You will get the same result selecting left-to-right as right-to-left.

I made this into an extension: Convert Selection - it works on multiple selections.

convert selection demo

Here is the entire code of the extension:

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;

Upvotes: 2

rioV8
rioV8

Reputation: 28793

You can use the extension Select By v1.10

Execute the command: SelectBy: Create separate cursors for anchor and active position of the selection(s) , selectby.anchorAndActiveSeparate

It will create new cursors for every anchor and active position of all the current selections. Overlapping cursors are eliminated.

Upvotes: 2

Related Questions