1138

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?

7
  • 2
    see this link stackoverflow.com/questions/30067767/… Commented Nov 6, 2018 at 13:59
  • 2
    You can also press F1 whilst editing, and in the search bar, type fold or unfold and you will see clickable options to do everything below and more, it also shows the keyboard shortcuts. Commented Jul 1, 2020 at 9:59
  • 1
    cmd + shift + p (then search for "fold" to find it or even select it) Commented Jan 21, 2021 at 13:41
  • For those having problems with any of the CTRL-shortcuts: check your active addons. I had a vim-addon installed which seemed to block those shortcuts. Commented Jun 10, 2023 at 15:39
  • 2
    For (currently) most famous vim-addon Vim (vscodevim) you may add this to your settings.json: "vim.handleKeys": { "<C-k>": false } Commented Jun 10, 2023 at 16:01

13 Answers 13

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

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately this does not apply to current versions like 1.41+ (or even lower versions). Anyway - the posted picture still seems to be correct, because you have to Press&Hold Ctrl. then press K, release Ctrl, press&hold it again, and then press one of the other keys like O, 1, 2 and so on! Pressing Ctrl.+K, releasing everything and then pressing S for example does "Save all". The releasing and pressing of Ctrl. again is important here and makes the difference! See some answer below...
@Satria Your statement that [control + k, 1] and [control + k, control + 1] are two different shortcuts is correct and usefully points out that this answer isn't as clear as it should be. However, your statement that you need to release and re-press control in between the k and the 1 to activate the latter shortcut is wrong. You can simply hold control while you press and release k and then press and release 1 to activate the shortcut [control + k, control + 1]. Just wanted to clarify that.
@MarredCheese Thanks for your info. Just tried it, you are right. No need to release CTRL in between K and 1. At least now, in version 1.51.1. Maybe I simply misunderstood the answer those days. The answer in its current state is fine. It's [CTRL+K+S], not [CTRL+K, S]
For those that are confused, VS Code handles listening to a first keystroke then a second one. To Fold all regions, you would press on Ctrl+K then on Ctrl+0 You would need to release Ctrl between the two presses. (You actually can notice that VS code prompts a message in its Blue bar status stating that it's "waiting for a second key or chord")
Just for added clarity, I had mistakenly thought Ctrl K + Ctrl # would unfold those items that are not folded by the particular number. For instance, I had already folded all levels using Ctrl K + Ctrl 0 and then hit Ctrl K + Ctrl 2. I couldn't figure out why nothing was happening, assuming it would unfold to level 2. However, it simply folds everything to the level set, regardless of it's previous state. So if you have some portions folded that you would like unfolded it might be best to unfold everything using Ctrl K + Ctrl J and then fold to your desired level.
|
497
  • 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.

5 Comments

btw, for those of us on OSX, use cmd in place of ctrl
This should be the accepted answer as "fold all" is cleary not the same as "collapse all methods and properties", this anwers the question perfectly
Sounds like Ctrl + K + 3 doesn't work fine in Java, folds a for loop in a method instead of folding the method itself. Tried multiple times.
That's incorrect, namespaces, classes, methods, etc. do not necessarily sit at levels 1, 2, 3, etc. It just deals with folding/unfolding whatever is at these levels.
The fold will not fold the code you are currently in, as Visual Studio does. You need to go to the top and hit, for example, Ctrl+K, Ctrl+3 in order to collapse all methods.
269

Like this? (Visual Studio Code v0.10.11+)

Fold All (Ctrl+K Ctrl+0)

Unfold All (Ctrl+K Ctrl+J)

Fold Level n, with a digit 1-7 (Ctrl+K Ctrl+n)

10 Comments

Unfortunately no. This one collapses everything, including the class itself, methods,if-else conditions and loops inside the methods. Where as in VS professional it just collapses methods. When we open a method it opens up fully.
Added a 'fold level' shortcut. Maybe it's what you're looking for ?
@eadam In a typescript file, Ctrl + K, 2 does exactly that for me.
This answer is both concise and also completely correct/clear. Most others, including the accepted answer, fail to resolve the ambiguity between the shortcuts [control + k, 0] and [control + k, control + 0].
For dummies => press Ctrl+K then take your hand off the keys and press Ctrl+0 to fold all
|
156

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.

4 Comments

Cmd + Shift + p for Mac users.
There are some things I still use key combinations for, but getting used to Cmd + P and Cmd + Shift + P is by far the best that ever happened to my VSCode workflow.
Not the best answer for me, if you're used to the word "collapse" then you won't find it in that search.
@artemis_clyde to add one more to your list cmd + shift + o -> Search method or variables directly
39

Mac Users

Fold Commands

enter image description here

Unfold commands enter image description here

1 Comment

For dummies => press ⌘+K then take your hand off the keys and press ⌘+0 to fold all –
31

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.

5 Comments

What do you mean by "will do close to what you want." (it seems incomprehensible)?
@PeterMortensen - In English, this construct basically resolves to "[this suggestion] will [accomplish something similar to] what you want [to accomplish]." He's saying the suggestion will do (accomplish) close to (similar to) what the OP wants (the thing he wants to do).
Correct and precise answer for the current version 1.42.1, because it helps to understand, that you have to release Ctrl. and press&hold it again before each of the K and 1 keys!
thx, Ctrl+K, Ctrl+2 do the job in May, 2020
If its already fully collapsed, first do Ctrl+K,Ctrl+J to "expand all". THEN do one of the mentioned commands, to expand just the level(s) you want. Otherwise, you may find that the contents of each method are collapsed at the block level, requiring unfolding each block.
25

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.

2 Comments

This does not bring value as this is already discussed in other upvoted answers.
@Nilambar I disagree. This is the only answer that helped me change the keybind quickly.
24

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

2 Comments

@SunilGarg You can use Ctrl + Shift + [ to fold single code block and Ctrl + Shift + ] to unfold
Use Ctrl + K + / to old all comments multiple lines.
15

You should add user settings:

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

4 Comments

I've tried messing with this several times. Unsure how these settings help. I notice more - symbol buttons, but that's it. This doesn't seems to accomplish closing or opening "all" blocks.
@Urasquirrel I think the first line just shows all those icons instead of only showing them when you hover over the thin space near line numbers. the indentation foldingStrategy seems to be the default, as I haven't changed that myself, so you didn't notice anything. Not sure what folding does.
@JakeT. Thanks! That makes more sense. I kinda figured out the first one. I wish there was a way to auto fold all blocks or specific types of code when you open the files. For example typescript interfaces at the top of files are annoying as hell. When I hover an interface I can see it's definition so I don't need them open and taking up real estate.
@Urasquirrel No problem. I don't know if getting fine grained on which types to open/close is feasible, but the answer above ( stackoverflow.com/a/43710782/4544328 ) will show you how to close all, and then you could use the command to open an individual block as needed while editing code. It's the best low-effort solution available that I've found.
5

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

Comments

2

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

Comments

1

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.

Comments

-3

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.

2 Comments

Is there a way to make this work more intelligently. When you have TypeScript, the { } symbols don't just signify functions/methods. Often it relates to Objects, if/else statements too - and suddenly all that is being collapsed too.
OP and the people coming to this question are looking for shortcuts to do what you described. Manually collapsing every code blocks is a tedious task.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.