This tutorial shows source code sections folded and unfolded all in Visual Studio code with shortcut commands.

How to fold and unfold all code sections in Visual Studio Code

There are below shortcut commands to do it.

  • Open Visual Studio Code

  • Open a code file, for example, a Java code file

  • Fold

    • Ctrl + Shift + [ in Windows and Linux
    • shift + ⌘ + [ in MacOS
  • Unfold

    • Ctrl + Shift + ] in Windows and Linux
    • shift + ⌘ + ] in MacOS
  • Fold All

    • Ctrl + Shift + Alt + [ in Windows and Linux
    • shift + ⌘ + Alt + [ in MacOS
  • Unfold All

    • Ctrl + Shift + Alt + ] in Windows and Linux
    • shift + ⌘ + Alt + ] in MacOS

You can still update the custom keyboard bindings using GUI settings.

  • Go to File > Preferences > Keyboard Shortcuts

Update the below commands with new keys

  • editor.fold
  • editor.unfold
  • editor.foldRecursively
  • editor.unfoldRecursively
  • editor.foldAll
  • editor.unfoldAll

For example, Change the fold shortcut key from ctrl+alt+] to ctrl+alt+F.

{
        "key": "ctrl+alt+k",
        "command": "editor.fold",
        "when": "editorTextFocus && foldingEnabled"
}

You can give a custom key at your convenience.

Show folding by default

Sometimes, you want to fold code blocks by default,

  • Go to File > Preferences > Settings
  • go to User settings
  • update the below settings and
{
    "editor.showFoldingControls": "always",
    "editor.folding": true,
    "editor.foldingStrategy": "indentation",
}