Reputation: 7633
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:
Upvotes: 13
Views: 6853
Reputation: 4701
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"
}
Upvotes: 0
Reputation: 7633
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"
}
Upvotes: 1
Reputation: 313
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.
Upvotes: 17
Reputation: 1329572
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
Upvotes: 10
Reputation: 3520
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.
Upvotes: 0