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).
vscodevim
) you may add this to yoursettings.json
:"vim.handleKeys": { "<C-k>": false }
– Sprightly