eadam
eadam

Reputation: 26061

Collapse all methods in Visual Studio Code

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

Answers (13)

Saurabh Bayani
Saurabh Bayani

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

willingdev
willingdev

Reputation: 9546

  • Ctrl + K + Ctrl + 0: fold all levels (namespace, class, method, and block)
  • Ctrl + K + Ctrl + 1: namespace / @Component(For Angular)
  • Ctrl + K + Ctrl + 2: class / methods
  • Ctrl + K + Ctrl + 3: methods / blocks
  • Ctrl + K + Ctrl + 4: blocks / inner blocks
  • Ctrl + K + Ctrl + [ or Ctrl + k + ]: current cursor block
  • Ctrl + K + Ctrl + j: UnFold

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

pearpages
pearpages

Reputation: 21867

  • Fold All:

    • Windows and Linux: Ctrl + K Ctrl + 0
    • Mac: + K + 0

Those are Zeros. and the one in the number row, not the numpad.

  • Unfold All:

    • Windows and Linux: Ctrl + K Ctrl + J
    • Mac: + K + J

To see all available shortcuts in the editor:

  • Windows and Linux: Ctrl + K Ctrl + S
  • Mac: + K + S

Screenshot of Visual Studio keyboard shortcuts

All shortcuts kept up to date by the Visual Studio Code team: Visual Studio Code Shortcuts

Upvotes: 1942

Dharmendra Rathod
Dharmendra Rathod

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

enter image description here

Upvotes: 2

Minh Tien Tran
Minh Tien Tran

Reputation: 1098

You should add user settings:

{
   "editor.showFoldingControls": "always",
   "editor.folding": true,
   "editor.foldingStrategy": "indentation", 
}

Upvotes: 13

starball
starball

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

Danish Azad
Danish Azad

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

Abdus Salam Azad
Abdus Salam Azad

Reputation: 5502

Use Ctrl + K + 0 to fold all and Ctrl + K + J to unfold all.

Upvotes: 24

CaptainTuna
CaptainTuna

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

MIke Pateras
MIke Pateras

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

Nelson Bwogora
Nelson Bwogora

Reputation: 2369

To collapse methods in the Visual Studio Code editor:

  1. Right-click anywhere in document and select "format document" option.
  2. Then hover next to number lines and you will see the (-) sign for collapsing method.

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

Eric Bole-Feysot
Eric Bole-Feysot

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

accimeesterlin
accimeesterlin

Reputation: 4718

Mac Users

Fold Commands

enter image description here

Unfold commands enter image description here

Upvotes: 38

Related Questions