13

I know there is keyboard shortcut for single line(//....) commenting Ctrl + K + C and uncommenting Ctrl + K + U .

My question is that, is there any default keyboard shortcut for block (/* ...... */) commenting and uncommenting? If yes how?

And If there is no default block commenting keyboard shortcut defined, So is there a way i could add my own keyboard shortcut for this? How do i do that?

I have found lot of questions regarding commenting, but haven't found spoken about block commenting anywhere. Any help is appreciated :)

aimme
  • 6,385
  • 7
  • 48
  • 65
  • 2
    Pretty sure there isn't. All you can do is highlight multiple lines and do Ctrl K + C / K + U as you mentioned. This will at least comment all of the selected lines. So it's similar behavior. I haven't yet used VS 2015, actually, but every other version works this way. Don't know about adding your own shortcut, unfortunately. – sab669 Sep 16 '15 at 13:32
  • is there a way i could add a shortcut to do block commenting? – aimme Sep 16 '15 at 13:34
  • 1
    I know you can add custom shortcuts to existing behavior, but I don't think you can create new shortcuts with new behavior. You'd have to create a macro and bind that macro to a key, or drop a button for it on a toolbar somewhere. Not sure how you'd do this though, I scarcely use VS Macros. – sab669 Sep 16 '15 at 13:37
  • 1
    Related: https://stackoverflow.com/questions/31859468/why-does-visual-studio-resort-to-single-line-comments-when-commenting-a-multi-li – Saragis Sep 16 '15 at 14:04
  • 1
    There are numerous reasons why the style of comment with `/* */` are not recommended: - they cannot be placed inside one another, they can be mistaken in some cases by compilers as regular expressions. – Code Whisperer Feb 16 '16 at 20:25

6 Answers6

10

for me, in Visual Studio 2015 community edition, when I select full lines it will insert // comments. If I select the lines only partially (the first line is not selected from the very beginning or the last line is not selected till the end), it will insert /* comments. The shortcut is the same, Ctrl + K + C.

Full lines selected:
These lines will be commented with //

Press Ctrl + K + C

Result:

//These lines will //be commented with //

Partial lines selected:
These lines will be commented with /*

Press Ctrl + K + C

Result:

These /*lines will be commented*/ with /*

GoTo
  • 5,966
  • 2
  • 28
  • 31
  • 1
    Is this really working? Or this /*.....*/ is limited to community version only? I have used VS 2000, 2015 & 2017 Professional version, but neither of them supports the block comment. – Diablo Dec 29 '17 at 11:48
  • I just tested again and the behavior changed a little in VS Community v. 15.5.2: It will add /* only if you select partially **one** line. Otherwise it will add //. – GoTo Dec 29 '17 at 15:32
  • 1
    In latest version of Visual Studio Code (Version: 1.29.1 (user setup)), ctrl+k+u is not working but ctrl+k+c only works like //be commented with // -- irrespective of selecting single or multi-line. – whoami - fakeFaceTrueSoul Dec 06 '18 at 15:50
7
  1. I used FeinCtrl to list all available commands, and there are only two related to commenting code in/out: Edit.CommentSelection and Edit.UncommentSelection; there are no other commands that could do a block commenting.

  2. You can add your own shortcuts to any EXISTING command by going into Tools -> Options -> Environment -> Keyboard, selecting a command and assigning your new key combination.

  3. If you search this site, you'll find a lot of reasons to NOT use block comments at all.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
5

If you have resharper, you can use keyboard shortcut

Ctrl+Shift+/

to put block comment around selected statements. I hope this helps.

MKMohanty
  • 956
  • 7
  • 23
  • Just what I needed! It's worth noting that this acts as a toggle, so selecting the same text and repeating the keystrokes removes the comment. – Chris B Aug 08 '18 at 09:27
5

You can use three /// to create...

/// <summary>
/// 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
Cliff
  • 51
  • 1
  • 1
2

For a simple block comment you can create the following C# command in Visual Commander and assign a shortcut to it:

public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
{
        EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
        ts.Text = "/* " + ts.Text + " */";
}
Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
0

In latest version of Visual Studio Code (Version : 1.29.1(user setup)), you can try Ctrl+/ for single line comment & Shift+Alt+A for block comment. If you can click on edit of your menu bar, there you should be able to find the necessary info.

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46