How do I fold/collapse/hide sections of code 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.
VS Code supports code folding through keyboard shortcuts, gutter icons, the Command Palette, and custom region markers. The fastest way to collapse a block is to place your cursor inside it and press Ctrl+Shift+[ on Windows/Linux or Cmd+Option+[ on macOS. To unfold, use Ctrl+Shift+] or Cmd+Option+]. This article covers every folding mechanism VS Code provides, including language-aware folding, indentation-based folding, custom regions, and the settings that control folding behavior.
Keyboard Shortcuts for Folding
These are the core shortcuts every VS Code user should know:
| Action | Windows / Linux | macOS |
| Fold current block | Ctrl+Shift+[ | Cmd+Option+[ |
| Unfold current block | Ctrl+Shift+] | Cmd+Option+] |
| Fold all | Ctrl+K, Ctrl+0 | Cmd+K, Cmd+0 |
| Unfold all | Ctrl+K, Ctrl+J | Cmd+K, Cmd+J |
| Fold level 1 | Ctrl+K, Ctrl+1 | Cmd+K, Cmd+1 |
| Fold level 2 | Ctrl+K, Ctrl+2 | Cmd+K, Cmd+2 |
| Fold level 3 | Ctrl+K, Ctrl+3 | Cmd+K, Cmd+3 |
| Fold all block comments | Ctrl+K, Ctrl+/ | Cmd+K, Cmd+/ |
| Fold recursively | Ctrl+K, Ctrl+[ | Cmd+K, Cmd+[ |
| Unfold recursively | Ctrl+K, Ctrl+] | Cmd+K, Cmd+] |
| Toggle fold | Ctrl+K, Ctrl+L | Cmd+K, Cmd+L |
The level-based shortcuts are particularly useful for large files. Ctrl+K, Ctrl+2 collapses everything to two levels of nesting, giving you a high-level overview of the file structure while keeping top-level blocks expanded.
Gutter Folding Icons
The fold/unfold arrows appear in the gutter (the area between line numbers and the editor content) when you hover over a foldable region. Click the downward-pointing arrow to collapse a block, and click the rightward-pointing arrow to expand it.
By default, these icons only appear on hover. To make them always visible:
Set this in your settings.json file (open with Ctrl+, then click the JSON icon in the top right).
Folding Strategies
VS Code determines where fold regions begin and end using one of two strategies:
Indentation-Based Folding
This is the default fallback. VS Code looks at the indentation of consecutive lines to determine foldable blocks. Any line that is followed by more-indented lines creates a fold region.
This works for any language, even languages without a dedicated VS Code extension. It is the reason you can fold YAML, plain text files, and configuration formats without any special setup.
Language-Based Folding
When a language extension provides a folding range provider, VS Code uses the language's actual syntax structure to determine fold regions. This is more accurate than indentation because it understands constructs like multi-line strings, comments, and nested blocks that may not follow strict indentation rules.
To control which strategy is used:
| Value | Behavior |
"auto" | Use language provider if available, fall back to indentation |
"indentation" | Always use indentation-based folding |
Most of the time, "auto" is the correct choice. Switch to "indentation" only if a language extension's folding provider produces incorrect regions.
Custom Folding Regions
Most languages support region markers that let you define arbitrary foldable sections, independent of the code's syntactic structure.
JavaScript / TypeScript
Python
C# / Java
HTML
CSS / SCSS
Region markers are especially valuable in large configuration files, test suites, and CSS files where natural syntactic blocks may not align with logical sections.
Folding Settings Reference
All folding-related settings in VS Code:
The foldingMaximumRegions setting is worth knowing about. For very large files (10,000+ lines), computing fold regions can slow down the editor. If you notice lag, check whether this limit is being hit.
Folding in the Command Palette
Open the Command Palette with F1 or Ctrl+Shift+P and type "fold" to see all available commands:
- Fold: Fold the innermost block at the cursor
- Unfold: Unfold the block at the cursor
- Fold All: Collapse everything
- Unfold All: Expand everything
- Fold Level 1-7: Collapse to a specific nesting depth
- Fold All Block Comments: Collapse only comment blocks
- Fold All Regions: Collapse only custom region markers
- Unfold All Regions: Expand only custom region markers
- Toggle Fold: Toggle the fold state at the cursor
The "Fold All Block Comments" command is particularly useful when reviewing code. It hides documentation blocks so you can focus on the implementation.
Practical Workflow Tips
Reviewing a Large File
When opening a large unfamiliar file, start with Ctrl+K, Ctrl+1 to fold everything to the top level. This gives you the class and function structure. Then selectively unfold the sections you need.
Organizing Test Files
Use region markers to group related tests:
Hiding Boilerplate
In files with repetitive boilerplate (imports, configuration, serialization), fold those sections to focus on the logic:
Common Pitfalls
- Confusing
Ctrl+[(outdent line) withCtrl+Shift+[(fold block). WithoutShift, you are changing indentation instead of folding. - Using indentation-based folding in a language that has a dedicated folding provider. The indentation strategy may produce wrong regions for multi-line strings, comments, or template literals. Keep
foldingStrategyset to"auto". - Adding region markers without corresponding end markers. An unmatched
#regionor//#regionis silently ignored but creates confusion when someone tries to fold it. - Over-using region markers in files that already have natural structure (classes, functions). If every function is wrapped in a region, the markers add noise without value. Use them for logical groupings that cross syntactic boundaries.
- Not knowing about
Ctrl+K, Ctrl+0(fold all) andCtrl+K, Ctrl+J(unfold all). These two shortcuts are the fastest way to get an overview of a large file or restore it to full view. - Ignoring the
foldingMaximumRegionssetting when working with very large generated files. If folding stops working in a file, this limit may have been reached.
Summary
- Fold blocks with
Ctrl+Shift+[(Windows/Linux) orCmd+Option+[(macOS). Unfold with the matching]shortcut. - Use
Ctrl+K, Ctrl+0to fold everything andCtrl+K, Ctrl+Jto unfold everything. - Level-based folding (
Ctrl+K, Ctrl+1throughCtrl+K, Ctrl+7) gives you a structural overview at any nesting depth. - Define custom foldable sections with language-specific region markers (
//#region,# region,#region,<!-- #region -->). - Configure folding behavior through
editor.foldingStrategy,editor.showFoldingControls, andeditor.foldingMaximumRegionsinsettings.json. - Use "Fold All Block Comments" from the Command Palette to hide documentation while reviewing implementation.

