Ora

How do you expand all files in VS Code?

Published in VS Code Code Folding 4 mins read

To expand all folded code regions within an active file in VS Code, the most direct method is to use the keyboard shortcut Ctrl+K followed by Ctrl+J. This action will unfold every collapsed section of code in the current editor.

Visual Studio Code offers robust code folding features that allow you to collapse and expand blocks of code, enhancing readability and navigation, especially in large files. When you want to see all your code without any hidden sections, expanding all folds becomes essential.

Understanding Code Folding in VS Code

Code folding is a powerful feature that allows developers to collapse sections of code, such as functions, classes, loops, or comments, into a single line. This provides a high-level overview of the file's structure and helps focus on specific parts of the code without being distracted by other details. VS Code automatically detects foldable regions based on language syntax (e.g., curly braces {}, indentation).

Expanding All Code Regions in an Open File

When working within an open editor, you can quickly expand all previously folded code regions using various methods.

1. Using the Keyboard Shortcut

The fastest way to expand all folded code in your current file is with a specific key sequence:

  • Press Ctrl+K (on Windows/Linux) or Cmd+K (on macOS).
  • Release K, then immediately press Ctrl+J (on Windows/Linux) or Cmd+J (on macOS).

This sequence will unfold all collapsed sections of code. However, be mindful that expanding all regions in a very long file might make navigation challenging or temporarily impact performance, so it's advisable to use this carefully in such scenarios.

2. Expanding via the Command Palette

The Command Palette provides a text-based interface to access all VS Code commands:

  • Open the Command Palette by pressing Ctrl+Shift+P (on Windows/Linux) or Cmd+Shift+P (on macOS).
  • Type "Fold: Unfold All" or "Unfold All Regions".
  • Select the command Fold: Unfold All Regions from the dropdown list.

3. Using the Context Menu

You can also access folding commands directly from the editor:

  • Right-click in the margin area of the editor (typically where line numbers appear).
  • A context menu will appear. Hover over the "Folding" submenu.
  • Select "Unfold All" from the options.

4. Expanding Specific Fold Levels

VS Code also allows you to unfold code up to a specific folding level:

  • Unfold All (Level 1-7): To unfold all regions up to a specific indentation level (e.g., all functions but keep inner blocks folded), you can use Ctrl+K followed by Ctrl+1 through Ctrl+7. For instance, Ctrl+K, Ctrl+1 expands all folds at level 1.
  • Unfold Current Region: If your cursor is within a folded region, Ctrl+K then Ctrl+[ (or Cmd+K, Cmd+[ on macOS) will unfold just that specific region.

Common Code Folding and Unfolding Shortcuts

Here’s a quick reference for essential folding actions in VS Code:

Action Keyboard Shortcut (Windows/Linux) Keyboard Shortcut (macOS) Description
Unfold All Regions Ctrl+K, Ctrl+J Cmd+K, Cmd+J Expands all collapsed code regions in the current file.
Fold All Regions Ctrl+K, Ctrl+0 Cmd+K, Cmd+0 Collapses all foldable code regions in the current file.
Unfold Level N Ctrl+K, Ctrl+N (e.g., Ctrl+1) Cmd+K, Cmd+N Unfolds all regions up to a specific indentation level N.
Fold Level N Ctrl+K, Ctrl+[ or Ctrl+] Cmd+K, Cmd+[ or Cmd+] Folds/unfolds the innermost region at the cursor's position.
Fold All Comment Regions Ctrl+K, Ctrl+/ Cmd+K, Cmd+/ Collapses all comment blocks.

For more detailed information on configuring and using code folding, you can refer to the VS Code Documentation on Code Folding.

What About Expanding Folders in the Explorer View?

While the question refers to "expanding all files," it's often interpreted as expanding code regions within an active file, as detailed above. However, if you are looking to expand all sub-folders within the Explorer sidebar in VS Code:

  • There isn't a single universal "expand all folders" keyboard shortcut for the entire file tree in the Explorer view.
  • You typically expand folders by clicking the small arrow icon next to each folder name.
  • To quickly expand a deeply nested folder to reveal its contents, you can often right-click the parent folder and use an option like "Reveal in Explorer" (Windows) or "Reveal in Finder" (macOS), which opens your operating system's file browser at that location.

In summary, for expanding code within an open file, Ctrl+K then Ctrl+J is your primary tool.