20

According to the comment here: https://github.com/Microsoft/vscode/issues/50671 it's possible to edit this behavior in the keybindings, but provides no instructions for how to do so.

How do I edit the VScode keybindings so that if I press CTRL+W with no open tabs, it doesn't close the editor? It's highly annoying to be trying to close a bunch of tabs and then accidentally close the entire editor.

1
  • You have verified that the shortcut is not listed within the keybindings editor?
    – Ramhound
    May 3, 2019 at 20:17

4 Answers 4

26

Go to File -> Preferences -> Keyboard Shortcuts (or press Ctrl+K Ctrl+S).

Find the "Close Window" setting with Ctrl+W as the Keybinding.

Right click and remove the key binding or edit it to another combination.

3
  • Works for me. TY Sep 9, 2020 at 22:47
  • 1
    This is not half as good or as nuanced as using the when condition.
    – psygo
    Dec 2, 2020 at 1:32
  • I agree regarding the nuance, but some people like working through the UI instead of editing a text file. My answer and harrymc's together give them options based on their comfort level. Jan 11, 2021 at 16:41
7

This is a known bug: #54583 Closing last editor with Ctrl+W closes VS Code (regression), which seems to be a reversion to a previous behavior.

The solution is found in another bug-report: #53730 User keybindings break if conditions on the default binding change, which is to edit the file ~/.config/Code/User/keybindings.json.

Somewhere in the file you should find the following code:

{
  "key": "ctrl+w",
  "command": "-workbench.action.closeWindow",
  "when": "!editorIsOpen"
}

Change the "when" condition so that this looks like:

{
  "key": "ctrl+w",
  "command": "-workbench.action.closeWindow",
  "when": "!editorIsOpen && !multipleEditorGroups"
}

Note that this is marked as fixed, so the bug might be fixed in some upcoming version of VS Code.

2
  • It workeddd!! You're the best! Thank you! Jun 22, 2020 at 18:25
  • Thanks, issue is still present in VS Code Jun 28, 2022 at 20:20
1

I was also having the same issue. Adding this in keybindings.json file helps:

{
      "key": "cmd+w",
      "command": "-workbench.action.closeWindow",
      "when": "!editorIsOpen && !multipleEditorGroups"
}

This will close active editor by cmd+w . But will not close the vscode window when no editor is open.

0

When you use the recent version, 1.43.1, in the keybindings.json user setup:

  1. Press Ctrl + K, Ctrl + S
  2. Type: view close editor to filter the shortcuts
  3. Find item View: close editor with the keybinding Ctrl + W and right-click it.
  4. Select the item Change when expression in the context menu
  5. Type: !editorIsOpen and press Enter
  6. Restart VS Code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .