John Y
John Y

Reputation: 1271

How to get Visual Studio code to select the correct tab when closing the current tab?

When I close a tab in Visual Studio Code, I would like it to behave like every other tabbed editor/browser I use when closing a tab.

I have already read the answer given to Is there a vscode settings which specifies which tab to open when closing a tab?, which implies that setting "workbench.editor.focusRecentEditorAfterClose": false in settings.json will give the desired behaviour — unfortunately, it does not.

Given five tabs A, B, C, D, E, and that tab C is currently selected:

There's nothing particularly wrong about closing to the right instead of the left, except for the fact that virtually every other program closes to the left. It's equivalent to putting a search box on a website and having a search for "foo bar" perform "foo or bar" rather than "foo and bar" — since Google, Bing, DuckDuckGo, eBay, Amazon, Twitter and Facebook (to name but a few major sites) all use and, it needlessly confuses people and slows them down.

I can't find a setting to configure this, nor can I find anything in the Marketplace that might fix it. Is it possible?

Upvotes: 5

Views: 1355

Answers (2)

xialu
xialu

Reputation: 304

mark's answer is insightful, but there are two unresolved cases (see comments). I found an optimization based on it. The improved operation process is as follows:

  1. install extension multi-command
  2. modify settings.json, close focusRecentEditorAfterClose
    "workbench.editor.focusRecentEditorAfterClose": false,
  1. modify settings.json, add the following configuration:
    "multiCommand.commands": [
        {
            "command": "multiCommand.closeEditorFocusLeft",
            "sequence": [
                "workbench.action.closeActiveEditor",
                "workbench.action.previousEditor",
            ]
        },
        {
            "command": "multiCommand.closeEditorOnly",
            "sequence": [
                "workbench.action.closeActiveEditor",
            ]
        },
    ],
  1. modify keybindings.json, add the following configuration:
    {
        "key": "ctrl+w",
        "command": "extension.multiCommand.execute",
        "args": {
            "command": "multiCommand.closeEditorFocusLeft"
        },
        "when": "editorFocus && !(activeEditorIsFirstInGroup || activeEditorIsLastInGroup)"
    },
    {
        "key": "ctrl+w",
        "command": "extension.multiCommand.execute",
        "args": {
            "command": "multiCommand.closeEditorOnly"
        },
        "when": "editorFocus && (activeEditorIsFirstInGroup || activeEditorIsLastInGroup)"
    },
    {
        "key": "ctrl+w",
        "command": "-workbench.action.switchWindow"
    },

Upvotes: 1

Mark
Mark

Reputation: 182621

You can create your own macro to accomplish what you want. You need a macro because you need to run two commands: close your editor and then focus the editor to the left. I don't believe there is any built-in way to do it otherwise.

So, using a macro extension like multi-command, this goes into your settings:

"multiCommand.commands": [
  {
    "command": "multiCommand.closeEditorFocusLeft",
    "sequence": [
      "workbench.action.closeActiveEditor",
      "workbench.action.previousEditor",
    ]
  }
]

and a keybinding in keybinding.json:

  {
    "key": "ctrl+w",
    "command": "-workbench.action.closeActiveEditor"
  },
  {
    "key": "ctrl+w",
    "command": "extension.multiCommand.execute",
    "args": {
      "command": "multiCommand.closeEditorFocusLeft"
    },
    // "when": "editorFocus"
  },

I assumed here that you use Ctrl+W to close the active editor, and I first disabled the default command for that and then assigned it to the macro. But you could whatever keybinding you wish. And it works.

In my testing it is required to have the setting

Workbench > Editor: Focus Recent Editor On Close

disabled. So that vscode actually tries to go right instead of to the most recently opened editor wherever that may be (and then go to the left of that).

Unfortunately, outside of an extension, I thinkthis is the only way to accomplish what you want.

Upvotes: 3

Related Questions