How to Fold and Unfold Code in Visual Studio Code
Managing large files in Visual Studio Code (VS Code) is much easier when you collapse and expand sections of your code. This guide provides a direct, step-by-step overview of how to fold and unfold code blocks in VS Code using folding icons, keyboard shortcuts, and the Command Palette.
Using folding icons in the gutter
The easiest way to fold and unfold code is by using the mouse.
- Hover your mouse cursor over the gutter (the space between the line numbers and the code).
- Click the minus icon (−) that appears next to a code block to fold (collapse) it.
- Click the plus icon (+) that appears next to a folded code block to unfold (expand) it.
Keyboard shortcuts
Keyboard shortcuts allow you to fold and unfold code quickly without using a mouse.
Fold (collapse) the cursor’s current region
- Windows/Linux:
Ctrl+Shift+[ - macOS:
Cmd+Option+[
Unfold (expand) the cursor’s current region
- Windows/Linux:
Ctrl+Shift+] - macOS:
Cmd+Option+]
Fold all regions in the file
- Windows/Linux: Press
Ctrl+K, then pressCtrl+0(zero) - macOS: Press
Cmd+K, then pressCmd+0(zero)
Unfold all regions in the file
- Windows/Linux: Press
Ctrl+K, then pressCtrl+J - macOS: Press
Cmd+K, then pressCmd+J
Using the Command Palette
If you forget the keyboard shortcuts, you can access all folding commands through the Command Palette.
- Open the Command Palette using
Ctrl+Shift+P(Windows/Linux) orCmd+Shift+P(macOS). - Type Fold or Unfold.
- Select the desired command from the dropdown list (such as Fold All, Unfold All, Fold Recurse, or Unfold Recurse).