909

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eadam
  • 23,151
  • 18
  • 48
  • 71
  • 2
    see this link https://stackoverflow.com/questions/30067767/how-do-i-collapse-sections-of-code-in-visual-studio-code-for-windows/30077543#30077543 – moshtaba morsali Nov 06 '18 at 13:59
  • Good question however can you mark an answer? I think they deserve the credit. – RayLoveless Feb 04 '19 at 17:48
  • 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. – RollingInTheDeep Jul 01 '20 at 09:59
  • 1
    cmd + shift + p (then search for "fold" to find it or even select it) – Ben Racicot Jan 21 '21 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. – kabeleced Jun 10 '23 at 15:39
  • 1
    For (currently) most famous vim-addon Vim (`vscodevim`) you may add this to your `settings.json`: `"vim.handleKeys": { "": false }` – kabeleced Jun 10 '23 at 16:01

11 Answers11

1631
  • Fold All:

    • Windows and Linux: Ctrl + K Ctrl + 0 (ZERO)
    • Mac: + K + 0
  • 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

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
pearpages
  • 18,703
  • 1
  • 26
  • 27
  • 43
    Thanks with Cmd+k Cmd+S to open the short key setting. It's really helpful. – Chau Pham Apr 06 '18 at 02:03
  • 32
    For those confused like I was, that's a ZERO, not an o. – itcropper Jul 11 '19 at 15:33
  • 4
    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 Feb 28 '20 at 08:20
  • 2
    seems like shortcuts made to `longcuts`. :). – sjd Dec 18 '20 at 11:55
  • 1
    @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 Jan 06 '21 at 20:31
  • @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] – Satria Jan 08 '21 at 03:33
  • 1
    PS: The `0` button needs to be the one above the alphabets. Num-pad `0` doesn't seem to work. – deekeh Nov 19 '21 at 06:57
  • 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") – El Bachir Jan 06 '22 at 15:02
  • 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. – Matt Penner Feb 17 '22 at 07:12
  • vscode is actually too good – Andy Feb 24 '22 at 20:03
  • This might not work if you imported Sublime shortcuts (at least it did not work for me). If so you can `⌘+shift+p` and type `fold all` as mentioned by reply bellow – euh Jul 06 '22 at 07:24
355
  • 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

From Visual Studio Code

enter image description here

jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
M Komaei
  • 7,006
  • 2
  • 28
  • 34
  • 13
    btw, for those of us on OSX, use cmd in place of ctrl – ekkis May 31 '19 at 22:52
  • 6
    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 – Marius Jun 25 '20 at 11:24
  • 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. – aderchox Sep 20 '21 at 13:01
  • 5
    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. – sw1337 Feb 01 '22 at 21:12
242

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)

slava
  • 791
  • 1
  • 11
  • 26
Eric Bole-Feysot
  • 13,949
  • 7
  • 47
  • 53
133

The beauty of Visual Studio Code is

Ctrl + Shift + P

Hit it and search anything you want.

In your case, hit Ctrl + Shift + P and type fold all.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saurabh Bayani
  • 3,350
  • 2
  • 25
  • 44
36

Mac Users

Fold Commands

enter image description here

Unfold commands enter image description here

accimeesterlin
  • 4,528
  • 2
  • 25
  • 18
27

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MIke Pateras
  • 271
  • 3
  • 3
  • What do you mean by *"will do close to what you want."* (it seems incomprehensible)? – Peter Mortensen Jul 27 '19 at 18:07
  • @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). – bubbleking Jan 15 '20 at 16:46
  • 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! – Satria Feb 28 '20 at 08:23
  • 1
    thx, `Ctrl`+`K`, `Ctrl`+`2` do the job in May, 2020 – devellopah May 16 '20 at 09:49
  • 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. – ToolmakerSteve Nov 01 '20 at 21:20
24

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Danish Azad
  • 241
  • 2
  • 2
22

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abdus Salam Azad
  • 5,087
  • 46
  • 35
12

You should add user settings:

{
    "editor.showFoldingControls": "always",
    "editor.folding": true,
    "editor.foldingStrategy": "indentation", 
}
Minh Tien Tran
  • 1,038
  • 9
  • 11
  • 1
    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 May 24 '18 at 14:00
  • @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. – Jake T. May 30 '18 at 19:58
  • @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 Jun 01 '18 at 13:07
  • @Urasquirrel No problem. I don't know if getting fine grained on which types to open/close is feasible, but the answer above ( https://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. – Jake T. Jun 01 '18 at 15:56
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nelson Bwogora
  • 2,225
  • 1
  • 18
  • 21
  • 2
    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. – JGFMK May 14 '17 at 13:44
  • 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. – Xonshiz Sep 14 '21 at 14:51