Visual Studio Code
Programming
Code Editing
Software Development
Coding Efficiency

Collapse all methods in Visual Studio Code

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To collapse (fold) all methods in Visual Studio Code, press Ctrl+K Ctrl+0 on Windows/Linux or Cmd+K Cmd+0 on macOS. This folds every collapsible block in the file, giving you a bird's-eye view of your code structure. VS Code also offers level-specific folding, region folding, and language-aware folding that let you control exactly what gets collapsed.

Quick Reference: All Folding Shortcuts

ActionWindows / LinuxmacOS
Fold AllCtrl+K Ctrl+0Cmd+K Cmd+0
Unfold AllCtrl+K Ctrl+JCmd+K Cmd+J
Fold current blockCtrl+Shift+[Cmd+Option+[
Unfold current blockCtrl+Shift+]Cmd+Option+]
Fold Level 1Ctrl+K Ctrl+1Cmd+K Cmd+1
Fold Level 2Ctrl+K Ctrl+2Cmd+K Cmd+2
Fold Level 3Ctrl+K Ctrl+3Cmd+K Cmd+3
Fold All Block CommentsCtrl+K Ctrl+/Cmd+K Cmd+/
Fold All RegionsCtrl+K Ctrl+8Cmd+K Cmd+8
Unfold All RegionsCtrl+K Ctrl+9Cmd+K Cmd+9
Toggle FoldCtrl+K Ctrl+LCmd+K Cmd+L

All of these are chord shortcuts: press the first key combination, release, then press the second.

Fold All vs. Fold by Level

Fold All (Ctrl+K Ctrl+0) collapses every single block, including nested blocks inside methods. You see only the top-level declarations.

Fold by Level gives you finer control. For example, in a class file:

  • Level 1 (Ctrl+K Ctrl+1) folds classes, leaving imports visible.
  • Level 2 (Ctrl+K Ctrl+2) folds methods inside classes but keeps the class declaration expanded.
  • Level 3 (Ctrl+K Ctrl+3) folds if/for/while blocks inside methods.

This is usually what developers actually want when they say "collapse all methods." Use Level 2 to fold methods while keeping the class structure visible.

Example: Level 2 fold on a TypeScript file

Before folding:

typescript
1class UserService {
2  private db: Database;
3
4  constructor(db: Database) {
5    this.db = db;
6  }
7
8  async getUser(id: string): Promise<User> {
9    const result = await this.db.query("SELECT * FROM users WHERE id = $1", [id]);
10    if (!result.rows.length) {
11      throw new NotFoundError("User not found");
12    }
13    return result.rows[0];
14  }
15
16  async createUser(data: CreateUserDTO): Promise<User> {
17    const existing = await this.getUser(data.email);
18    if (existing) {
19      throw new ConflictError("User already exists");
20    }
21    return this.db.insert("users", data);
22  }
23}

After Ctrl+K Ctrl+2:

typescript
1class UserService {
2  private db: Database;
3
4  constructor(db: Database) { ... }
5
6  async getUser(id: string): Promise<User> { ... }
7
8  async createUser(data: CreateUserDTO): Promise<User> { ... }
9}

You can see every method signature at a glance while the implementation details are hidden.

Using the Command Palette

If you forget a shortcut, open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and type "fold". You will see all available fold commands:

  • Fold All
  • Unfold All
  • Fold Level 1 / 2 / 3 / 4 / 5 / 6 / 7
  • Fold All Block Comments
  • Fold All Regions
  • Fold Recursively
  • Unfold Recursively

Custom Fold Regions

You can define custom foldable regions using language-specific markers. These regions fold independently of the code structure and are useful for organizing files that do not have natural block boundaries.

JavaScript / TypeScript:

javascript
1// #region Database Helpers
2function connect() { /* ... */ }
3function disconnect() { /* ... */ }
4function healthCheck() { /* ... */ }
5// #endregion
6
7// #region Auth Middleware
8function authenticate() { /* ... */ }
9function authorize() { /* ... */ }
10// #endregion

Python:

python
1# region Database Helpers
2def connect():
3    pass
4
5def disconnect():
6    pass
7# endregion
8
9# region Auth Middleware
10def authenticate():
11    pass
12# endregion

C# / Java:

csharp
1#region Database Helpers
2public void Connect() { /* ... */ }
3public void Disconnect() { /* ... */ }
4#endregion

CSS / SCSS:

css
1/* #region Header Styles */
2.header { /* ... */ }
3.header-nav { /* ... */ }
4/* #endregion */

Fold all custom regions with Ctrl+K Ctrl+8 and unfold them with Ctrl+K Ctrl+9.

Configuring Fold Behavior in Settings

VS Code exposes several settings that control how folding works. Open Settings JSON (Ctrl+, then click the JSON icon) and add:

json
1{
2  "editor.showFoldingControls": "always",
3  "editor.foldingStrategy": "auto",
4  "editor.foldingHighlight": true,
5  "editor.unfoldOnClickAfterEndOfLine": false,
6  "editor.foldingMaximumRegions": 5000
7}
SettingOptionsEffect
showFoldingControls"always" / "mouseover"Whether gutter arrows are always visible or only on hover
foldingStrategy"auto" / "indentation"auto uses the language server; indentation uses whitespace
foldingHighlighttrue / falseHighlights the folded region background
foldingMaximumRegionsnumberCaps fold regions for performance on very large files

Language-specific folding strategy

Some languages (like Python or YAML) work better with indentation-based folding. You can set it per language:

json
1"[python]": {
2  "editor.foldingStrategy": "indentation"
3},
4"[yaml]": {
5  "editor.foldingStrategy": "indentation"
6}

Auto-Fold on File Open

VS Code does not auto-fold files on open by default, but you can achieve this with a task or an extension. The simplest approach is to use the Auto Fold extension from the Marketplace:

  1. Install Auto Fold from the Extensions panel.
  2. Set "autofold.default": 2 in your settings to auto-fold at level 2 when files open.

Alternatively, you can add a keybinding that runs fold after opening:

json
1{
2  "key": "ctrl+shift+o",
3  "command": "runCommands",
4  "args": {
5    "commands": [
6      "workbench.action.quickOpen",
7      "editor.foldLevel2"
8    ]
9  }
10}

Common Pitfalls

  • Chord shortcuts not working. These are two-step shortcuts. Press Ctrl+K, release both keys, then press Ctrl+0. If you hold all keys at once, it will not register.
  • Folding does not detect methods. If foldingStrategy is set to "indentation" for a language that has a language server (like TypeScript or Java), switch it to "auto" for structure-aware folding.
  • Fold All collapses too much. Use level-specific folding (Ctrl+K Ctrl+2) instead of Fold All (Ctrl+K Ctrl+0) to keep class declarations visible.
  • Folded code disappears from search. Ctrl+F searches all text including folded regions. Matches inside folded blocks will auto-expand when you navigate to them.
  • Saving does not preserve fold state. Folds reset when you close and reopen a file. Use the "Remember Fold State" extension if you need persistence.
  • Performance on very large files. Files with more than 5,000 foldable regions may lag. Increase foldingMaximumRegions or split the file.

Summary

  • Fold all methods: Ctrl+K Ctrl+0 (Windows/Linux) or Cmd+K Cmd+0 (macOS).
  • Fold only methods (keep class visible): Use Level 2 folding with Ctrl+K Ctrl+2.
  • Unfold everything: Ctrl+K Ctrl+J.
  • Use #region / #endregion markers for custom foldable sections in any language.
  • Set "editor.foldingStrategy": "auto" for language-aware folding, or "indentation" for whitespace-based languages.
  • All fold commands are accessible through the Command Palette by typing "fold".

Course illustration
Course illustration

All Rights Reserved.