'In VS Code file searching, can I expand (or collapse) all results?

In the Search pane of the program, after I hit Enter, all files are listed, with some expanded to show results in a file, and others collapsed. I'm wondering firstly what determines the expansion of any given file, and secondly what I can do to expand all of them at once.

This question seems closest to mine, but it's about a different IDE, and the key commands it suggests for Windows had no apparent effect: Automatically expand all in Eclipse Search results



Solution 1:[1]

See this setting:

Search: Collapse Results in the Settings UI or

search.collapseResults: alwaysExpand in your settings.json file

The options are auto,alwaysCollapse, and alwaysExpand. auto is the default.

auto: Files with less than 10 results are expanded. Others are collapsed.

So you want the alwaysExpand option.

You can also toggle any file expanded/collapsed with the Space key or just expand any collapsed file with RightArrow.

Collapse with LeftArrow and collapse all with Ctrl+LeftArrow. Oddly, there is no expandAll binding or command.


And see https://stackoverflow.com/a/67307225/836330 for a command to collapse all the results that you can set to a keybinding:

workbench.files.action.collapseExplorerFolders as in

{
  "key": "alt+l",    // whatever you want
  "command": "search.action.collapseSearchResults",
  "when": "searchViewletFocus"   // if you want to limit it when focus is already on the search results area
}

in your keybindings.json.

v1.41 is making expanded search results the default, see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_41.md#search

Expand all search results by default

Previously, if a full text search returned more than 10 results in a single file, it would appear collaped in the results tree. However, this sometimes made finding a particular result difficult, so with this release all results will appear expanded. You can set search.collapseResults to auto to revert to the old behaviour.

And see Visual Studio Code - Include context in search results for showing the search results in an editor.

Solution 2:[2]

There are two quick ways to expand all at any time:

  • Click the icon in the top right of the search panel that has a "+" inside a square. This toggles all items expanded/collapsed.
  • Use the command palette (Cmd-Shift-P) to trigger the "Search: Expand All" command. To make it quicker to access, you can add a custom keyboard shortcut: when looking at the "Expand All" search result in the command palette, click the gear icon next to it and it will take you to an editor for adding a shortcut binding.

Both of these only work for the Search panel though – despite the similarity, they do not work for the "Find All References" results panel.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 peterflynn