1

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 keyboard shortcut for folding?

Thanks for your responses.

rsmets
  • 789
  • 1
  • 13
  • 23
  • I assume it is already folded and you want to comment out the entire folded region? If so, just select the entire folded area, i.e., include the closing brackets for example, and then Ctrl+/ will comment each line of the folded area. Block commenting is a little wonky on folded regions. You need to open the folded region first and then toggle off the block comment. – Mark Mar 02 '22 at 21:57

2 Answers2

1

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.

Sabina940
  • 13
  • 4
  • Thanks for the suggestion to edit the block comment keyboard shortcut. I agree ```\``` is easer to remember than the default `a`. However this answer does not quite satisfy my question. – rsmets Mar 03 '22 at 00:35
1

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...

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Thanks I suppose that does work but is there a Keyboard Shortcut for block commenting a foldable region? similar to the keyboard command for folding. – rsmets Mar 03 '22 at 00:34
  • No, there is only a block comment shortcut which I mentioned, nothing specific for a folded region. – Mark Mar 03 '22 at 02:50