How to not close the editor window when closing the last tab in VS Code
Asked Answered
D

5

12

If there's only one tab on an editor in VS Code, the default behavior is to close the editor right then and there.

Personally, and I think many others agree with me, this behavior is very annoying since the programmer would rather preserve the layout he or she created for the project's workspace.

How do I change this behavior?

I've found discussions on this topic, but all of the solutions do not seem to be the ideal one of simply leaving the editor open but empty. Most of them simply delete the related shortcuts, which seems to me like cutting your arm off just because you broke it.

Anyway, here are some useful links:

Denomination answered 2/12, 2020 at 12:9 Comment(1)
The current answers do NOT fix the issue as I see it. Simple test: in terminal type "code myfile", this will open code with one tab open. Click the 'x' on the tab to close that tab. Code will exit. Not annoying with a pretend example, but it's counter-intuitive when you want to edit a second file but haven't pre-prepped code to know that before you launched it. I'd like an option to turn the 'auto-exit on last tab close'.Bithia
D
1

I think @VonC's answer almost had it. In the end, this other answer by @harrymc did it for me:

{
  "key": "cmd+w",
  "command": "-workbench.action.closeWindow",
  "when": "!editorIsOpen && !multipleEditorGroups"
}
Denomination answered 11/6, 2024 at 14:32 Comment(0)
S
15

If what you are looking for is to preserve empty groups as well as empty editor windows, there is an option in the preferences that gets you almost all of the way:

"workbench.editor.closeEmptyGroups": false

This has the effect that an editor group will stay open even if the last tab within it is closed.

Saccharase answered 15/2, 2021 at 17:50 Comment(2)
This didn't work for me. If type "code" on the command line, then File -> Preferences -> Settings (so now I have ONE tab open), click the "x" to close the TAB, and the entire editor quits. It would make sense to keep the editor open and close the "Code" window when you hit the "x" in the top-right corner of the editor window.Bithia
I have window.closeWhenEmpty and workbench.editor.closeEmptyGroups toggled off, but it does not workDelaine
I
10

The behavior of the shortcut ctrl+w is as-designed closing the application

Actually, VSCode 1.57 (May 2021) will change that:

Removed Cmd+W / Ctrl+W as keybinding to close window when no editor is opened

We have gotten feedback that users are frustrated when the window suddenly closes after having closed the last editor via rapid Cmd+W / Ctrl+W usages.

A quick poll in the team also revealed that many had the keybinding for closing a window unassigned, so we went ahead and removed Cmd+W / Ctrl+W as keybinding to close window when no editor is opened.

You can easily bring the keybinding back by configuring it as follows:

{
 "key": "cmd+w", // use "ctrl+w" on windows
 "command": "workbench.action.closeWindow",
 "when": "!editorIsOpen && !multipleEditorGroups"
}

Note: on all platforms there is a dedicated assigned keybinding to close the window:

  • macOS: Cmd+Shift+W
  • Linux: Alt+F4
  • Windows: Alt+F4
Illuminative answered 28/5, 2021 at 20:1 Comment(0)
D
1

I think @VonC's answer almost had it. In the end, this other answer by @harrymc did it for me:

{
  "key": "cmd+w",
  "command": "-workbench.action.closeWindow",
  "when": "!editorIsOpen && !multipleEditorGroups"
}
Denomination answered 11/6, 2024 at 14:32 Comment(0)
L
0

but all of the solutions do not seem to be the ideal one of simply leaving the editor open but empty

Reddit has an answer providing you the ideal solution of leaving the editor open, but empty: close all tabs but leave editors open.

In brief, they suggest to use in rapid succession the "close all editors" and the "three column editor layout".

They suggest to create a macro like:

"macros": {
    "closeAllTabs": [
        "workbench.action.closeAllEditors",
        "workbench.action.editorLayoutThreeColumns"
    ]
}

then bind it to a key combination. macrosRe is advised.

The behavior of the shortcut ctrl+w is as-designed closing the application, see #49023. It is advisable to remove the shortcut and cut the arm, since it is design to work like that. However, for version 1.43.1 a new comment was posted this year on a thread you linked. You might want to have a look at it as well.

Lucchesi answered 9/12, 2020 at 14:41 Comment(5)
closeAllEditors closes every tab, but I wanted to close only the tabs in a certain group, only "clean" a certain grouping of tabs. There is a closeEditorsInGroup but it doesn't seem to do anything for me.Denomination
I prefer the multi-command extension, which is really well built and slightly more popular.Denomination
There was a known bug on that topic: #10438, but it is closed as not reproducible. Remember that the command closes the active groupLucchesi
Ok, the bug is unfortunate, but I can't really close the bounty with this answer if the best we can get is closing everything to keep the layout. That's too much of a destructive operation.Denomination
Maybe you are missing the point: Microsoft states that the command should work just fine. Those who reported it as a bug were dismissed. Please double check your code, maybe there is something wrong there.Lucchesi
A
0

I found this question when researching how to do the opposite: I do want the window to close when I close the last editor, to match the behavior of other text editing apps on macOS.

None of the configurations shown here so far worked for me, including the one that uses "when": "!editorIsOpen && !multipleEditorGroups" predicate, so I played around with it a bit, and this seems to do exactly what I want:

    {
        "key": "cmd+w",
        "command": "workbench.action.closeWindow",
        "when": "groupEditorsCount == 1 && !multipleEditorGroups && workbenchState == empty"
    }
Amora answered 10/7, 2024 at 4:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.