Home > Software engineering >  How do I comment out a folded region in VS Code?
How do I comment out a folded region in VS Code?

Time:03-03

I am curious how to comment out a "folded" region of code with VS Code?

For example, I would like to comment out the "foldable", aka "collapsable", region:

if (a == b) {
  dance();
}

I know the keyboard shortcut for folding regions as such is:

  • Ctrl Shift [ on Windows and Linux
  • [ on macOS

Is there a similar keyboard shortcut to comment out that same region similar to the key board shortcut for folding?

Thanks for your responses.

CodePudding user response:

On Windows, the shortcut is: CTRL / On Mac, the shortcut is: Command /

Ctrl Shift / for toggling block comments because Ctrl / is shortcut for toggling line comments so it's naturally easier to remember. To do so, just click on the settings icon in the bottom left of the screen and click 'Keyboard Shortcuts' and find "toggle block...". Then click and enter your desired combination.

CodePudding user response:

If the region is already folded, then select both lines of the fold, so include the closing bracket, if any, and then either line comment or block comment.

To toggle off the comments, again select the entire fold, and toggle off with the line comment Ctrl / works as you would expect. When you unfold all the line comments are toggled off.

But if you had block commented the fold originally, with Shift Alt A, then toggling it off with the same command produces a mess (even if all of the folded are is selected).

To properly toggle off a block commented folded area, you have to unfold it first and then select it all and then toggle off. This seems like a bug to me...

  • Related