How do you hide arbitrary section of code in VS Code?

Unfortunately, it doesn't appear that VSCode currently allows you to hide an arbitrary selection of code like Visual Studio does via Ctrl+M,Ctrl+H. For now you could use the code folding feature, which depends on indentation. That is, if you indent the code you want to hide, you could then collapse it via Ctrl+Shift+[, like so:

code folding in vscode

Obviously, this is kind of an ugly solution to your problem; it requires multiple steps and makes actual changes to the file. Also, it's clearly useless if you're writing in a whitespace-dependent language like Python, but I don't think you are going to find a better solution short of finding an extension (or writing one yourself). It also might be worth posting an issue on the official VSCode GitHub repo if this feature is important to you.


You can use the following delimiters for code folding:

C/C++:       #pragma region and #pragma endregion
C#:          #region and #endregion
CSS:         /* #region */ and /* #endregion */
Java:        //region and //endregion
JavaScript:  //#region and //#endregion and //region and //endregion
PHP:         #region and #endregion
Powershell:  #region and #endregion
Python:      #region and #endregion
VB:          #Region and #End Region

See https://github.com/Microsoft/vscode/issues/12146 ([folding] fold regions)


In the Insiders Build v1.70 now is the functionality and command to truly hide arbitrary lines of code. The command is

Create Manual Folding Range from Selection
editor.createFoldingRangeFromSelection

It is bound to Ctrl+K Ctrl+, by default. Select any lines you want to fold.

You can unfold those lines either by clicking the gutter folding controls or this command when the cursor is somewhere on the folded line:

Remove Manual Folding Ranges
editor.removeManualFoldingRanges

The above command is bound to Ctrl+K Ctrl+. by default.

fold selected lines demo