Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a shortcut to toggle the Outline view in VSCode

I've successfully created a key shortcut for outline.focus which displays the Outline panel. I'm looking for toggle behaviour.

Existing:

{
    "key": "ctrl+alt+o",
    "command": "outline.focus",
    "when": "editorTextFocus"
}

Is it possible??

like image 912
SteinTheRuler Avatar asked Jul 26 '19 15:07

SteinTheRuler


People also ask

How do I create a custom shortcut in VS Code?

All keyboard shortcuts in VS Code can be customized via the keybindings.json file. To configure keyboard shortcuts through the JSON file, open Keyboard Shortcuts editor and select the Open Keyboard Shortcuts (JSON) button on the right of the editor title bar.

What is Ctrl Shift P in VS Code?

You can define a keyboard shortcut for any task. From the Command Palette (Ctrl+Shift+P), select Preferences: Open Keyboard Shortcuts File, bind the desired shortcut to the workbench.


3 Answers

This will switch focus to the outline view and collapse all opened tree items in the outline view.

[ Handy option: "Follow Cursor" option in the Options View (click the ... for the options).]

{
  "key": "ctrl+alt+o",
  "command": "list.collapseAll",   // Ctrl+LeftArrow also does this
                                   // Space will open the current tree item
                                   // left/right arrows will open/close item
  "when": "outline.active"
},

{
  "key": "ctrl+alt+o",
  "command": "outline.focus",
  "when": "editorTextFocus"
},

So clicking Ctrl+Alt+O once shifts focus to the Outline view and clicking Ctrl+Alt+O a second time collapses all opened items. There is no list.expandAll command for some reason so you can't toggle those.

These could be combined into a single macro command if you wish, so there would be just one key combo to hit to focus the Outline view and collapse all. Let me know if you want to see that.

like image 174
Mark Avatar answered Oct 22 '22 12:10

Mark


With this you should be able to toggle between the outline view and the editor group to the right.

{
    "key": "ctrl+alt+o",
     "command": "workbench.action.focusRightGroup", 
     "when": "outline.active"
},
{
     "key": "ctrl+alt+o",
     "command": "outline.focus",
     "when": "editorTextFocus"
,

You can also substitute any of the other focus editor group type of commands, i.e. first group: workbench.action.focusFirstEditorGroup.

However, you can already do this using the outline focus binding above and then hitting the default editor group binding without defining the toggle.

like image 1
niid Avatar answered Oct 22 '22 10:10

niid


In addition of outline focus, you also have, with VSCode 1.47, June 2020, the ability to add basic keybinding for focusing on an element in outline panel (PR 91799 for issue 90732)

It will be with the setting list.selectAndPreserveFocus

See:

"Select and keep focus in a list view"

There is a new command, list.selectAndPreserveFocus, which lets you select an item from a list, while keeping focus in that list.
This can be helpful if you want to select multiple files from a list, such as the File Explorer, without having focus go to the file editor.

The command is not bound to any keyboard shortcut by default, but you can add your own keybinding:

{
  "key"    : "ctrl+o",
  "command": "list.selectAndPreserveFocus"
}
like image 1
VonC Avatar answered Oct 22 '22 10:10

VonC