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

2

27

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

Eurystheus answered 4/9, 2019 at 13:53 Comment(0)
Y
39

With vscode v1.89 (it is in the Insiders Build now), there is a context menu option when you right-click on a folder in the Search Results:

Expand Recursively

This would be helpful if you had the Search: Collapse Results setting set to default or alwaysCollapse (mentioned below). Then you could right-click a particular folder you want to open (and its child folders) without having ALL the search results folders open by default.

There is also a command search.action.expandRecursively you can bind to a keybinding to do the same:

{
  "key": "alt+x",       // choose your keybinding
  "command": "search.action.expandRecursively"
}

If a folder is focused in the Search Results, then triggering this command will recursively open only it and its subfolders.


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://mcmap.net/q/505360/-fold-all-quot-search-results-quot-in-visual-code-editor 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.

Yeargain answered 7/9, 2019 at 1:5 Comment(2)
Yeah, I also figured from the "Find All References" results, when you are in the state where some references are expanded, and some are collapsed, there is no Expand All option from the UI. (There is Collapse all though) :(Carnotite
Do you happen to know how to achieve a similar behavior when using "Find File References"? I got here searching for that, but no luck so far! Thanks edit: I just found github.com/microsoft/vscode-cpptools/issues/4333 but that is only for the c++ tools extensionVaucluse
C
32

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.

enter image description here

  • 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.

Cormier answered 26/1, 2021 at 20:52 Comment(2)
Very handy. Thank you!Ikkela
Such awful UI design, putting that icon so far away from what it affects! And the icon doesn't even look anything like the actual collapse icon either, so there's nothing to connect the two visually. Thanks for helping me find it!Pathos

© 2022 - 2024 — McMap. All rights reserved.