0

I would like a keyboard shortcut configuration that does the following:

  1. If a workspace folder is not opened, then close VS Code.
  2. If a workspace folder is opened, then close the workspace folder. A second press would trigger action #1.

How can this be done?

starball
  • 20,030
  • 7
  • 43
  • 238
Igor
  • 2,039
  • 23
  • 27

1 Answers1

0

See the "Global UI contexts" section of the when-clause context docs. There's a workspaceFolderCount when-clause that you can use. See also the sections on operators.

{
    "key": "<your-keybinding>",
    "when": "workspaceFolderCount > 0",
    "command": "workbench.action.closeFolder",
},
{
    "key": "<your-keybinding>",
    "when": "workspaceFolderCount == 0",
    "command": "workbench.action.quit", // or workbench.action.closeWindow
},

(I'm actually not sure what the difference between workbench.action.quit and workbench.action.closeWindow are).

Note: If you want to use ctrl/cmd+q, if you already have a default keybinding for ctrl/cmd+q for the workbench.action.quit command, in which case I think you can just keep that and don't need to write one again.

starball
  • 20,030
  • 7
  • 43
  • 238
  • The `workbench.action.closeFolder` command closes the entire workspace. Which may be what the OP wants. But if there are multiple workspaceFolders in a workspace, they are All closed (which is probably okay but should be pointed out). Maybe "active workspace" refers to one folder at a time... – Mark Aug 24 '23 at 21:59