Visual Studio 2019: Insert multiple C++ style single-line comments

Viewed 142

Using Visual Studio 2019 Community (v16.6.1). When editing C/C++/C# code, select several lines of code:

int countLines = 0;
if (!editingDocument)
    countLines = GetDocumentLines();

Now trigger Edit.ToggleBlockComment (Ctrl+Shift+/), or Edit.ToggleLineComment (Ctrl+K, Ctrl+/). In both cases, "C style" comments are applied:

/*int countLines = 0;
if (!editingDocument)
    countLines = GetDocumentLines();*/

This is ok, but I actually wanted C++ style, single-line comments:

// int countLines = 0;
// if (!editingDocument)
//     countLines = GetDocumentLines();

Is this possible? Ideal would be Edit.ToggleBlockComment to use "C style" and Edit.ToggleLineComment to use "C++ style".

Update

Thanks to one of the comments, I can confirm that when there is at least one comment inside the selected block, VS is smart enough to use C++ single-line comments for the whole block. While this doesn't answer the question, it is a useful workaround for now.

2 Answers

You can do that in editor by selecting text column via Shift + Alt + Up/Down arrow then just type //

You can get the following...

  • Click anywhere in a line, and toggle "//" comments

  • Select multiple lines, and toggle "//" comments

  • Preserve previously commented sub-sections

  • With a single key combination, eg Ctrl-/

By setting the command Edit.ToggleLineComment to Ctrl-/ in the keyboard editor.

This is identical functionality to Eclipse, IntelliJ and Android Studio. Once you've got it into your fingers anything else is clumsy.

Related