This tutorial shows source code sections fold and unfold all in Visual studio code with shortcut commands .

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

There are below short cut commands to do it.

  • Open Visual studio code

  • Open code file, for example, 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 below commands with new keys

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

For example, Change 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 default fold code block by default,

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