How to Fold Code and Create Custom Folding Regions in VS Code

Collapse code blocks, functions, and regions to focus on what matters. Learn keyboard shortcuts for folding, custom region markers, and manual folding ranges.

5 min read

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

ActionWindows / LinuxmacOS
Fold (innermost)Ctrl+Shift+[Cmd+Opt+[
UnfoldCtrl+Shift+]Cmd+Opt+]
Toggle FoldCtrl+K Ctrl+LCmd+K Cmd+L
Fold RecursivelyCtrl+K Ctrl+[Cmd+K Cmd+[
Unfold RecursivelyCtrl+K Ctrl+]Cmd+K Cmd+]
Fold AllCtrl+K Ctrl+0Cmd+K Cmd+0
Unfold AllCtrl+K Ctrl+JCmd+K Cmd+J
Fold Level 1Ctrl+K Ctrl+1Cmd+K Cmd+1
Fold Level 2Ctrl+K Ctrl+2Cmd+K Cmd+2
Fold All Block CommentsCtrl+K Ctrl+/Cmd+K Cmd+/
Fold Marker RegionsCtrl+K Ctrl+8Cmd+K Cmd+8
Unfold Marker RegionsCtrl+K Ctrl+9Cmd+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:

LanguageRegion startRegion 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:

javascriptjavascript
//#region Form validation
function validateEmail(email) { /* ... */ }
function validatePassword(password) { /* ... */ }
//#endregion

Both 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:

jsonjson
"[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

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+.).
RunePowered by Rune AI

Frequently Asked Questions

Why does folding not work in my file?

Folding works by indentation in all files. If your file is not indented or uses unusual indentation, folding may not detect regions correctly. Check that your indentation is consistent (spaces or tabs, same number per level). For languages like HTML and JSON, syntax-aware folding should work automatically. Switch folding strategy with editor.foldingStrategy if needed.

Can I share folding regions with my team?

Yes. Region markers like //#region and //#endregion are part of the source code. Commit them to Git and every contributor sees the same foldable regions. Manual folding ranges (created with Ctrl+K Ctrl+,) are local and not stored in the file, so they do not travel with the project.

How do I fold only specific levels?

Use Fold Level shortcuts. Press Ctrl+K Ctrl+1 to fold everything at level 1 (top-level blocks, like classes). Press Ctrl+K Ctrl+2 for level 2 (methods inside classes). Press Ctrl+K Ctrl+J to unfold everything.

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.