How do I fold or collapse sections of code in Visual Studio Code?
Is this feature supported?
Folding has been rolled out and is now implemented since Visual Studio Code version 0.10.11. There are these keyboard shortcuts available:
Fold folds the innermost uncollapsed region at the cursor:
Unfold unfolds the collapsed region at the cursor:
Fold All folds all regions in the editor:
Unfold All unfolds all regions in the editor:
References: https://code.visualstudio.com/docs/getstarted/keybindings
As of Visual Studio Code version 1.12.0, April 2017, see Basic Editing > Folding section in the docs.
The default keys are:
Fold All: CTRL+K, CTRL+0 (zero)
Fold Level [n]: CTRL+K, CTRL+[n]*
Unfold All: CTRL+K, CTRL+J
Fold Region: CTRL+K, CTRL+[
Unfold Region: CTRL+K, CTRL+]
*Fold Level: to fold all but the most outer classes, try CTRL+K, CTRL+1
Macs: use ⌘ instead of CTRL (thanks Prajeet)
Code folding by regions has arrived with v1.17. Folding by regions documentation. And v1.19 and 1.23.
[Generally you can add a space, for example // region and // endregion
to //region and //endregion
and it will also work.]
TypeScript/JavaScript: //#region and //#endregion or // #region and // #endregion
C#: #region and #endregion
C/C++: #pragma region and #pragma endregion
F#: //#region and //#endregion
PowerShell: #region and #endregion
Python: #region and #endregion
VB: #Region and #End Region
PHP: #region and #endregion
Bat: ::#region and ::#endregion or REM #region and REM #endregion
Markdown: <!-- #region --> and <!-- #endregion -->
Golang //region and //endregion or //#region and //#endregion
Java //#region and //#endregion
CSS/SCSS/Less: /* #region */ and /* #endregion */ or /*#region*/ and /*#endregion*/
SCSS/Less: // #region and // #endregion
Go: // region, // endregion and // #region, // #endregion
shellscript: # region and # endregion
Perl5 #region and #endregion or =pod and =cut`
Important: If you don't see your language in the list::
Each language also has snippets available for the markers. Type '#' and invoke code completion to see them. To have region markers configured for your language, contact the language extension provider.
So type #
and then Ctrl+Space to see the region markers for any language.
The default shortcut for collapse/extend are:
Ctrl + Shift + [ : "Fold"
Ctrl + Shift + Alt + [ : "Fold all"
Ctrl + Shift + ] : "Unfold"
Ctrl + Shift + Alt + ] : "Unfold all"
Or go to keybindings.json and change as you wish.
For example:
{
"key": "cmd+k cmd+m",
"command": "editor.foldAll",
"when": "editorFocus"
},
{
"key": "cmd+m cmd+k",
"command": "editor.unfoldAll",
"when": "editorFocus"
},
Collapsing is now supported in release 1.0:
Source Code Folding Shortcuts
There are new folding actions to collapse source code regions based on their folding level.
There are actions to fold level 1 (Ctrl+K Ctrl+1) to level 5 (Ctrl+K Ctrl+5). To unfold, use Unfold All (Ctrl+Shift+Alt+]).
The level folding actions do not apply to region containing the current cursor.
I had a problem finding the ]
button on my keyboard (Norwegian layout), and in my case it was the Å
button. (Or two buttons left and one down starting from the backspace button.)
This feature is now supported, since Visual Studio Code 1.17. To fold/collapse your code block, just add the region tags, such as //#region my block name
and //#endregion
if coding in TypeScript/JavaScript.
Example:
No technical tips here, just simple adjustments of the preferences of VsCode.
I managed to show code folding controls always in VsCode by going to Preferences and searching for 'folding'. Now just select to always show these controls. This works with the Typescript code and HTML of templates in the Angular 8 solution I tested it with.
This was tested with VsCode Insiders 1.37.0 running on a Windows 10 OS.
v1.42 is adding some nice refinements to how folds look and function. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#folded-range-highlighting:
Folded Range Highlighting
Folded ranges now are easier to discover thanks to a background color for all folded ranges.
Fold highlight color Theme: Dark+
The feature is controled by the setting editor.foldingHighlight and the color can be customized with the color editor.foldBackground.
"workbench.colorCustomizations": { "editor.foldBackground": "#355000" }
Folding Refinements
Shift + Click
on the folding indicator first only folds the inner ranges.Shift + Click
again (when all inner ranges are already folded) will also fold the parent.Shift + Click
again unfolds all.
When using the Fold command (kb(
editor.fold
))] on an already folded range, the next unfolded parent range will be folded.
As of version 1.3.1 (2016-07-17), Block Collapse is much more convenient.
Any line followed by an indented line will have a '-' character to allow collapse. If the block is collapsed, it will then be replaced by a '+' character that will open the collapsed block.
The (Ctrl + Shift + Alt + ]) will still affect all blocks, closing one level. Each repeated use closed one more level. The (Ctrl + Shift + Alt + [) works in the opposite way.
Hooray, block collapse finally works usefully.
I wish Visual Studio Code could handle:
#region Function Write-Log
Function Write-Log {
...
}
#endregion Function Write-Log
Right now Visual Studio Code just ignores it and will not collapse it. Meanwhile Notepad++ and PowerGUI handle this just fine.
Update: I just noticed an update for Visual Studio Code. This is now supported!
Note: these shortcuts only work as expected if you edit your keybindings.json
I wasn't happy with the default shortcuts, I wanted them to work as follow:
To set it up:
Preferences: Open Keyboard Shortcuts (JSON)
(Ctrl + ⇧ Shift + p)Already have custom keybindings for fold/unfold? Then you'd need to replace them.
{
"key": "ctrl+alt+]",
"command": "editor.fold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+alt+[",
"command": "editor.unfold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+]",
"command": "editor.foldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+[",
"command": "editor.unfoldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+[",
"command": "editor.unfoldAll",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+]",
"command": "editor.foldAll",
"when": "editorTextFocus && foldingEnabled"
},
console.log
lines in VSC, I'd very much appreciate it at stackoverflow.com/q/65692310/470749 – Ryan