Search code examples
visual-studio-code

In VS Code, how can I toggle maximization of the current workbench part / editor group?


I want to define a single and universal keyboard binding that allows me to toggle the maximization of the current workbench part, regardless of the type of part.

At a minimum, this would include the following parts could be:

  • Any single editor group
  • The panel itself

By maximization, note that I mean in both height and width so that the window takes over the full application window with the exception of the side bar (i.e. if the side bar is already visible, it remains visible at all times).

By toggling I mean of course going back and forth between the original layout (i.e. remembering it), and the maximization result.


My thinking about this problem

There are several command IDs that I believe could help here but none of them seem to do what I want, e.g.

  • workbench.action.toggleEditorWidths
  • workbench.action.maximizeEditor
  • workbench.action.toggleMaximizePanel
  • workbench.action.toggleEditorVisibility

I suspect that

  1. there are more commands that could help here
  2. there is probably a way to accomplish this with the right boolean logic of when clauses and joint execution of command IDs (?)

 Dow


Solution

  • With respect specifically to toggle maximize the current editor group, see https://stackoverflow.com/a/53926099/836330 and the new command it mentions:

    "View: Toggle Maximize Editor Group
    

    Try this in your keybindings.json (chose your desired keybinding):

     {
        "key": "alt+m",
        "command": "workbench.action.toggleMaximizedPanel",
        "when": "terminalFocus"
      },
      {
        "key": "alt+m",
        "command": "workbench.action.closePanel",
        "when": "editorFocus && panelIsOpen"
      },
      {
        "key": "alt+m",
        "command": "workbench.action.togglePanel",
        "when": "editorFocus && !panelIsOpen"
      },
    

    It works well - has no effect on the SideBar or ActivityBar - except for one thing. The workbench.action.togglePanel command in the third keybinding will appear to toggle the maximized editor but that command shifts focus to the opened panel. Which might not be what your expect.

    If you don't want that focus shift to the panel when un-maximizing the editor I think you will have to add a macro to the mix. Disable the second and third keybindings and add this to your settings.json:

    "multiCommand.commands": [
    
      {
        "command": "multiCommand.toggleEditorWthFocus",
        "sequence": [
          "workbench.action.togglePanel",
          "workbench.action.focusActiveEditorGroup",
          "workbench.action.toggleEditorWidths"  // will toggle other editor groups width
            // but other editor group width goes to a built-in minimum, not 0
        ]
      }
    ],
    

    using the multi-command macro extension.

    and use these as your two keybindings:

      {
        "key": "alt+m",
        "command": "workbench.action.toggleMaximizedPanel",
        "when": "terminalFocus"
      },
    
      //{
      //  "key": "alt+m",
      //  "command": "workbench.action.closePanel",
      //  "when": "editorFocus && panelIsOpen"
      //},
      // {
      //   "key": "alt+m",
      //   "command": "workbench.action.togglePanel",
      //   "when": "editorFocus && !panelIsOpen"
      // },
    
    
    {
      "key": "alt+m",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.toggleEditorWthFocus" },
      "when": "editorTextFocus && !panelIsOpen"
    },
    

    toggle views demo

    I know you said you want the SideBar unchanged, but if you go the macro route I think it would be pretty easy to toggle that as well.

    The comments above note that if you have more than one editor group, unfocused editor groups' width will be minimized, but it goes to some built-in minimum and not to 0 width. I don't think there is a way to toggle other editor groups on/off other than by closing other editor groups which can be done but you probably don't want that.

    Demo with split:

    toggle views with split