Code folding lets you collapse blocks of code so you can see the structure of a file without scrolling through every line. A long file with classes, methods, and nested blocks becomes a clean outline. You expand only the section you are working on.
VS Code supports folding through gutter icons, keyboard shortcuts, language-aware regions, custom markers, and manual folding ranges.
Fold using the gutter
Move your mouse to the left of the line numbers. A small chevron icon appears next to each foldable region. Click it to collapse the block. Click again to expand.
A collapsed region shows an ellipsis (...) with a preview of the hidden lines when you hover. Shift+Click the folding icon to fold or unfold the region and all nested regions inside it.
The gutter method is fastest when you are already using the mouse. For keyboard-first workflows, the shortcuts below are more efficient.
Keyboard shortcuts
| Action | Windows / Linux | macOS |
|---|---|---|
| Fold (innermost) | Ctrl+Shift+[ | Cmd+Opt+[ |
| Unfold | Ctrl+Shift+] | Cmd+Opt+] |
| Toggle Fold | Ctrl+K Ctrl+L | Cmd+K Cmd+L |
| Fold Recursively | Ctrl+K Ctrl+[ | Cmd+K Cmd+[ |
| Unfold Recursively | Ctrl+K Ctrl+] | Cmd+K Cmd+] |
| 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 All Block Comments | Ctrl+K Ctrl+/ | Cmd+K Cmd+/ |
| Fold Marker Regions | Ctrl+K Ctrl+8 | Cmd+K Cmd+8 |
| Unfold Marker Regions | Ctrl+K Ctrl+9 | Cmd+K Cmd+9 |
Fold Level shortcuts (1 through 7) collapse everything at that nesting depth:
- Level 1 is the outermost blocks, like classes and top-level functions.
- Level 2 is methods inside classes.
- Level 3 is blocks inside methods.
- Higher numbers show more detail.
You can also fold and unfold from the menu: Edit > Folding.
Custom region markers
You can mark any section of code as a foldable region by adding comment markers. These work in most languages:
| Language | Region start | Region end |
|---|---|---|
| JavaScript / TypeScript | //#region | //#endregion |
| Python | #region | #endregion |
| C# | #region | #endregion |
| C/C++ | #pragma region | #pragma endregion |
| Java | //#region | //#endregion |
| HTML / Markdown | <!-- #region --> | <!-- #endregion --> |
| CSS / SCSS | /*#region*/ | /*#endregion*/ |
| PowerShell | #region | #endregion |
Example in JavaScript:
//#region Form validation
function validateEmail(email) { /* ... */ }
function validatePassword(password) { /* ... */ }
//#endregionBoth functions fold into a single line labeled "Form validation." Region markers can be nested. A region inside another region folds independently.
To fold all marker regions at once, press Ctrl+K Ctrl+8 (Cmd+K Cmd+8). To unfold them, press Ctrl+K Ctrl+9 (Cmd+K Cmd+9).
Manual folding ranges
If your language does not support automatic folding or region markers, you can create folding ranges manually.
Select the lines you want to collapse. Press Ctrl+K Ctrl+, (Windows/Linux) or Cmd+K Cmd+, (macOS). VS Code creates a folding range from the selection and collapses it.
Manual ranges are displayed with a distinct icon in the gutter. They stay until you remove them. To remove a manual folding range, press Ctrl+K Ctrl+. (Cmd+K Cmd+.).
Manual ranges are stored in memory, not in the file. They are lost when you close the editor. If you need persistent foldable regions that survive across sessions and teammates, use region marker comments instead.
Change folding strategy
VS Code decides how to fold code using a folding strategy. The default is automatic: syntax-aware folding when the language supports it, falling back to indentation-based folding otherwise.
To switch to indentation-only folding, useful for languages where syntax folding does not work well, set editor.foldingStrategy to indentation. Scope it to one language or apply it globally:
"[html]": {
"editor.foldingStrategy": "indentation"
}Remove the language scope, for example "[html]", to apply indentation folding to every language. Set the value back to "auto" to restore the default behavior.
When folding does not behave as expected
Folding depends on consistent indentation. If some parts of your file use tabs and others use spaces, folding regions may break at unexpected places. Format the file (Shift+Alt+F) to normalize indentation.
If folding regions appear at wrong levels, check your editor.foldingStrategy setting. Some language extensions override the default folding provider. Disable the extension temporarily to see if folding improves.
For more editing productivity tools, see the keyboard shortcuts cheat sheet and the guide on using multiple cursors.
Rune AI
Key Insights
- Fold the current block with Ctrl+Shift+[ (Cmd+Opt+[) and unfold with Ctrl+Shift+] (Cmd+Opt+]).
- Fold all with Ctrl+K Ctrl+0 (Cmd+K Cmd+0). Unfold all with Ctrl+K Ctrl+J (Cmd+K Cmd+J).
- Create custom regions with //#region and //#endregion comments. These work in JavaScript, TypeScript, Python, C#, Java, and most languages.
- Fold by level: Ctrl+K Ctrl+1 shows only top-level blocks. Ctrl+K Ctrl+2 shows methods. Higher numbers show more detail.
- Create manual folding ranges with Ctrl+K Ctrl+, (Cmd+K Cmd+,) on selected lines. Remove them with Ctrl+K Ctrl+. (Cmd+K Cmd+.).
Frequently Asked Questions
Why does folding not work in my file?
Can I share folding regions with my team?
How do I fold only specific levels?
Conclusion
Code folding collapses blocks so you can focus on the code you are working on. Use the gutter icons to click-fold, or learn the keyboard shortcuts: Ctrl+Shift+[ to fold, Ctrl+Shift+] to unfold. Add //#region markers to group related code in any language. Create manual folding ranges with Ctrl+K Ctrl+, when the language does not provide automatic folding. Fold by level with Ctrl+K Ctrl+1 through Ctrl+K Ctrl+7 to see only the structure you need.
More in this topic
How to Use VS Code with WSL 2 on Windows
Run VS Code connected to Windows Subsystem for Linux so you can develop in a full Linux environment with native tools, terminals, and debugging, all from Windows.
20 Best VS Code Extensions for Web Developers in 2026
Twenty carefully chosen VS Code extensions every web developer should know. Covers formatting, linting, frameworks, debugging, Git, and developer experience.
How to Install, Disable, Update, and Uninstall VS Code Extensions
Learn how to install, disable, update, and uninstall VS Code extensions from the Marketplace and the command line. Step-by-step instructions for every action.