0

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

2 Answers 2

2

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; 
Sign up to request clarification or add additional context in comments.

5 Comments

Is there a vanilla (without extensions) way of running this on a key bind ? if not, do you have one you'd recommend?
Since you tagged vscode-extensions I thought you were building one. It is very easy to do so. I could package this into an extension if you want.
Thanks a lot, but I don't think that will be necessary. It seems rioV8s extension suggestion has exactly what I need.
Well, I have already published the extension Convert Selection. Haven't written the readme yet. Default keybinding is Alt+s, command is convert-selection.surround: Surround selection(s) with multi-cursors.
Nice, I downloaded it. What's also nice is it always makes the first multi cursor the active one, so when you click escape it always uses the first one.
2

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.

2 Comments

is there any way to force active be the first of the two regardless of which way you drag?
@PhilipAarseth I have updated the extension to sort the new selections, so if you first select at the bottom after Esc you are left with the first cursor (lowest line and char)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.