Reputation: 26061
In Visual Studio Professional, we have a shortcut key, Ctrl + M Ctrl + O to collapse all methods and properties in a class. How can I do a similar thing in Visual Studio Code?
I know there are shortcut keys like Ctrl + Shift + [, but this does not meet the needs.
Is it possible to get the Visual Studio Professional-like behaviour in Visual Studio Code?
Upvotes: 1090
Views: 885925
Reputation: 3500
The beauty of Visual Studio Code is you can use the Command Palette to search for any command:
Ctrl + Shift + P
In your case, search for fold all
.
Upvotes: 148
Reputation: 9546
It's called "Folding" in VS code. From docs:
You can fold regions of source code using the folding icons on the gutter between line numbers and line start. Move the mouse over the gutter and click to fold and unfold regions.
These shortcuts are listed there too.
Upvotes: 475
Reputation: 21867
Fold All:
0
0
Those are Zeros. and the one in the number row, not the numpad.
Unfold All:
To see all available shortcuts in the editor:
All shortcuts kept up to date by the Visual Studio Code team: Visual Studio Code Shortcuts
Upvotes: 1942
Reputation: 59
For the mac users, go to top-bar "Code > Settings > Keyboard Shortcuts" to see all the shortcuts
Search for Fold and there are multiple options to choose from, but most commonly I use
⌘ + K ⌘ + 0 --> Fold All ⌘ + K ⌘ + J --> Unfold all
Upvotes: 2
Reputation: 1098
You should add user settings:
{
"editor.showFoldingControls": "always",
"editor.folding": true,
"editor.foldingStrategy": "indentation",
}
Upvotes: 13
Reputation: 50014
Technically speaking (if you want to be really precise), every answer here is wrong (hopefully not mine though). The top answers all revolve around toggling folding by specific nesting levels, which can be close enough for certain programming languages and coding styles, but across all languages and their grammars, there is no such happy accident or law of the universe that things that are commonly given folding by language support extensions will somehow line up such that this works with 100% accuracy in all cases.
In Python (and many other languages), you can define classes in classes. That's just one simple example of how that assumption about folding nesting levels breaks.
This extends to many (but not all) similar questions like "how can I fold all functions?" and "how can I fold all classes?".
VS Code's current design doesn't have any arbitrarily extensible tagging/annotation/classification feature for folds provided by extensions. It does have a sealed enumeration of "kinds" of folds (see FoldingRangeKind
and FoldingRange
), which (at the time of this writing) only lists Comments
, Imports
, and Region
, which (obviously) does not include anything related to "methods". That distinction is what enables commands like Fold All Block Comments
and Fold All Regions
(and their associated, respective keybindings).
See the related issue ticket [folding] Please add more folding types (functions, classes) #98201, which was automatically closed because it didn't receive enough community support in time to get added to the backlog. You could try raising the feature-request again in a new issue ticket, but it'd still need to garner enough community support in a specific time window (see this), and it'd likely be declined for reasons I explain briefly later.
It is possible to write extensions that use language analysis to toggle the correct stuff using the editor.fold
command (see also the definition of FoldingArguments
) and executeCommand
. I don't know of many extensions that actually do this, but you might be able to find some by searching the extension marketplace. Ex. zeevro.fold-to-definitions
(I have no affiliation with this extension)
Actually..., looking at the source code of zeevro.fold-to-definitions
, the way it implements it is quite elegant/ingenious. It queries all symbols provided by language support extensions using the vscode.executeDocumentSymbolProvider
command, which includes the symbols' line numbers in the result set, filters by symbol kind, and then folds any folding regions for those line numbers. It's not totally foolproof, but I think it should probably work most of the time. As you can see from the API docs for symbol kinds, it's still a sealed enumeration, but it covers many more different kinds of language structures than are covered by FoldingRangeKind
. I've pinged a maintainer to take a look at it, and they weighed in that if a new issue ticket was raised requesting something like this in core VS Code, it would probably be declined in favour of implementation via extension, since it's a lot of work to maintain all the existing command palette commands already (again, see the related wiki page).
Upvotes: 4
Reputation: 261
Collapse All is Fold All in Visual Studio Code.
Press Ctrl + K + S for All Settings. Assign a key which you want for Fold All. By default it's Ctrl + K + 0.
Upvotes: 26
Reputation: 5502
Use Ctrl + K + 0 to fold all and Ctrl + K + J to unfold all.
Upvotes: 24
Reputation: 11
I recently made an extension for collapsing C# code to definitions since I was also missing that feature from Visual Studio. Just look for "Fold to Definitions" and you should find it, or just follow this link.
The repository is public, so you can easily inspect the extension.ts
file and adapt it to other languages. It is nowhere near perfect, but it does the job. It uses regular expressions to find methods, properties, and classes, and then moves the selection to those lines and executes a fold command.
Upvotes: 1
Reputation: 301
Ctrl+K, Ctrl+1 and then Ctrl+K, Ctrl+2 will do close to what you want.
The first command collapses level 1 (usually classes), and the second command collapses level 2 (usually methods).
You might even find it useful to skip the first command.
Upvotes: 30
Reputation: 2369
To collapse methods in the Visual Studio Code editor:
NB.: As per the Visual Studio Code documentation, a folding region starts when a line has a smaller indent than one or more following lines, and ends when there is a line with the same or smaller indent.
Upvotes: -3
Reputation: 15097
Like this ? (Visual Studio Code version 0.10.11)
Fold All (Ctrl+K Ctrl+0)
Unfold All (Ctrl+K Ctrl+J)
Fold Level n (Ctrl+K Ctrl+N)
Upvotes: 255