Visual Studio Code
Programming
Code Editing
Software Development
Coding Efficiency

VS Code collapse all: fold all functions and methods

Codemia Knowledge Hub ·

To collapse 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. VS Code also has level-specific folding, region folding and language-aware folding when you need finer control over what gets collapsed.

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

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 block, including nested blocks inside methods, so only the top-level declarations remain visible.

Fold by level is more precise. 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.

Level 2 is usually what people mean by "collapse all methods": the methods fold while the class structure stays 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}

Every method signature is visible while the implementations are hidden.

Finding fold commands in the Command Palette

If you forget a shortcut, open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and type "fold" to list the 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 with #region markers

Language-specific markers define foldable regions that fold independently of the code structure. They help organize files that have no 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.

Folding settings

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

Per-language folding strategy

Languages like Python or YAML often fold better by indentation. Set it per language:

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

Auto-folding when a file opens

VS Code does not fold files on open by default. The simplest fix is 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, bind Fold Level 2 to a single key and press it right after opening a file:

json
1{
2  "key": "ctrl+alt+2",
3  "command": "editor.foldLevel2",
4  "when": "editorTextFocus"
5}

When folding does not behave as expected

  • Chord shortcuts do nothing. Press Ctrl+K, release both keys, then press Ctrl+0. Holding all keys at once will not register.
  • Methods are not detected. If foldingStrategy is "indentation" for a language with a language server (like TypeScript or Java), switch it to "auto" for structure-aware folding.
  • Fold All collapses too much. Use Ctrl+K Ctrl+2 instead of Ctrl+K Ctrl+0 to keep class declarations visible.
  • Search and folded code. Ctrl+F searches folded regions too, and matches inside folded blocks expand when you navigate to them.
  • Fold state is lost on reopen. Folds reset when you close and reopen a file. The "Remember Fold State" extension adds persistence.
  • Very large files lag. Files with more than 5,000 foldable regions may lag. Increase foldingMaximumRegions or split the file.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms