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.
Visual Studio Code (VS Code) is a highly popular code editor among developers, thanks largely to its efficiency and vast range of functionalities. One such functionality that enhances productivity and code readability is the ability to fold or collapse sections of code. This feature allows you to temporarily hide parts of your code that you are not currently working on, making it easier to focus on other sections. This article provides a comprehensive guide on how to fold, collapse, or hide sections of code in Visual Studio Code. It includes technical details, examples, and additional insights to help you manage your code more effectively.
Understanding Code Folding in VS Code
Code folding is a feature that allows you to expand or collapse blocks of code. It helps in managing large files by letting you hide portions you are not immediately concerned with. In Visual Studio Code, this can be achieved either manually through specific actions or automatically based on certain criteria.
How to Fold or Collapse Code
VS Code provides several ways to fold or collapse code:
- Folding icons: In the gutter (left margin) of the editor, you'll find small triangles next to the line numbers where code can be folded. Clicking these triangles will collapse the block of code.
- Keyboard shortcuts:
- Fold:
Ctrl+[on Windows and Linux, orCmd+[on macOS. - Unfold:
Ctrl+]on Windows and Linux, orCmd+]on macOS. - Fold All:
Ctrl+(K, 0)on all platforms - Unfold All:
Ctrl+(K, J)on all platforms
- Command Palette: You can open the Command Palette by pressing
F1orCtrl/Cmd+Shift+Pand then typing "Fold" to see all related options. - Context Menu: Right-click anywhere in the editor and you will find options to fold, unfold, and other related actions under the 'Folding' submenu.
Code Folding Levels
VS Code allows you to fold code at different levels. This is useful when you are dealing with nested blocks of code and want to collapse only certain levels:
- Fold Level X: Levels start from 1 and go up depending on the nesting depth of blocks in your code. Use the command
Fold Level Xwhere X is the level you want to fold.
Automatically Folding Code
VS Code also supports automatic folding based on language-specific folding markers. These are pre-defined or custom markers/comments that can signify regions of code to fold. For example, in JavaScript:
Any code between //#region and //#endregion will be folded automatically.
Settings for Code Folding
Visual Studio Code provides settings related to folding that can be adjusted based on your needs. These settings include:
editor.folding: Enables or disables folding.editor.foldingStrategy: Controls whether folding is based on indentation or language-specific folding markers.editor.foldLevel: Set the default fold level when the editor loads.
These settings can be modified in your settings.json file.
Tips and Tricks for Effective Code Folding
- Custom Folding Regions: Utilize language-specific folding markers to create custom collapsible regions. This is handy for encapsulating logical parts of your code.
- Keyboard Shortcuts: Memorize and get comfortable with keyboard shortcuts to speed up your workflow.
- Auto-folding: Configure your settings to auto-fold files when opened based on your preferences.
Summary Table
Here is a summary of the key points discussed:
| Feature | Description | Usage |
| Folding icons | Click to fold/unfold in the gutter | Manual folding |
| Keyboard shortcuts | Various shortcuts for folding operations | Quick folding/unfold |
| Command Palette | Access via F1 or Ctrl/Cmd + Shift + P | All folding functions |
| Context Menu | Right-click for folding options | Alternative access |
| Folding levels | Manage nested blocks | Structured folding |
| Automatic markers | Use predefined/comment markers | Auto-fold regions |
| Settings | Customize folding behaviour | Configurations |
By understanding and utilizing these features, you can enhance your productivity and manage complex files more effectively in Visual Studio Code. Whether manually collapsing sections or using markers to automatically hide code, folding is an essential skill in a developer's toolkit to maintain readability and focus in their coding environment.

