Home > OS >  VSCode: How to Focus to an Editor
VSCode: How to Focus to an Editor

Time:06-23

In my Visual Studio code extension, I currently call workbench.action.moveEditorToNextGroup which moves the editor in code. However, if I call any extra commands, it will use the previous editor group rather than the one I moved it to.

How can I focus to a certain editor such as the one I move to?

CodePudding user response:

It looks like workbench.action.focusFirstEditorGroup should do the tick.

Alternatively,

  • workbench.action.focusFirstEditorGroup
  • workbench.action.focusSecondEditorGrou

...

May also be what you are looking for.

See docs for full list

CodePudding user response:

A more powerful way to move editors is to use the moveActiveEditor command.

 const moveTabBetweenGroupArgs = {to: "right", by: "group"};
 await vscode.commands.executeCommand('moveActiveEditor', moveTabBetweenGroupArgs);

The best documentation is located here: moveActiveEditor explanation.

If I call that repeatedly, it uses the editor I just moved. So, for instance, if I move an editor to the right group, and then trigger the command again, that same editor is moved to the next right group.

The command will create a group to the right if there isn't already one there.

Unfortunately, with these options:

// const moveTabBetweenGroupArgs = {to: "position", by: "group", value: 2};  // 'value' is 1-based

you should also be able to move an editor to a second group - even if it doesn't exist - but this form will NOT create the second group and silently fails. I consider that a bug and will file an issue.

You can also use the moveActiveEditor command to move editors within groups easily with code like this:

const moveTabInGroupArgs = {to: "position", by: "tab", value: targetIndex   1};  // 'value' is 1-based
await vscode.commands.executeCommand('moveActiveEditor', moveTabInGroupArgs);

which will move the ACTIVE editor to the value column within th3e current group. The value is 1-based, so the first column in a group is 1, not 0.

  • Related