How to make CTRL + / toggle a comment in Visual Studio

How can I make CTRL + / toggle a comment in Visual Studio, as it does with XCode and Eclipse?

10 Answers

Toggle single line comment and toggle block comment commands have been added in VS 2019 for C#

You can change the shortcut for these commands to whatever you want in Tools -> Options -> Environment -> Keyboard, search for Edit.ToggleBlockComment or Edit.ToggleLineComment.

6

You can't make it toggle without going into either a macro or other VS extension.

However, I always setup VS to comment with Ctrl + / and uncomment to Ctrl + Shift + /

You can customize the keyboard shortcuts by going int the "Tools" menu and selecting "Options". Then select "Keyboard" from the "Environment" branch. From there you can bind the Edit.CommentSelection and Edit.UncommentSelection commands to whichever keyboard shortcuts you'd like.

1

Here is a plugin to have the almighty "Toggle Comment" command...

1- Download and Install >

2- Restart VS

3- Go to "Tools > Options... > Environment > Keyboard"

4- Search for the command "ToggleComment" & Bind it to your favorite key

5- Enjoy

(Thank you, I had gave up but finally found this easy way to do it ;D)

4

If you have ReSharper installed (and using VS without it is/was tedious), you can assign a single key to the command ReSharper.ReSharper_LineComment.

For instance, I bind the keyboard shortcut Ctrl-K, Ctrl-C to the command, and then if I use it on a line that is uncommented, it comments it, and if the line is commented, it will uncomment it.

enter image description here

2

Apparently, VS2019 has finally introduced alternative keyboard mapping schemes. They're available at Options -> Environment -> Keyboard. I've been looking for a solution for toggle comment shortcut problem and I got a whole surprise package. This is more than I wanted.

Keyboard mapping options in vs

Ctrl-K and Ctrl-C will comment one or more selected lines.

Ctrl-K and Ctrl-U will uncomment one or more selected lines.

You can get toolbar icons that will do this by adding the "Text Editor" toolbar. In the toolbar area right-click and select "Text Editor" This will add a strip of icon buttons like this:

enter image description here

You can customize this strip by selecting the dropdown at the end of the ribbon:

enter image description here

To edit the keyboard combination go to Tools-Options and select Keyboard under the Environment tree. Here you can change the keys used to trigger the Comment and Uncomment actions:

enter image description here

3

If you're using Resharper, there is an almost identical alternative:

Right Alt+/

By using this single shortcut, you can simply toggle between commenting/uncommenting code.

In Visual Studio Code 2019, this can be done by:

  1. Enter hotkeys in the IDE search field and click Change hotkeys and keyboard shortcuts.
  2. In Show commands containing, search for and select EditorContextMenus.CodeWindow.GenerateDocumentationComments.
  3. Click Remove on the right side of Shortcuts for selected command, where ctrl+/ is currently selected.
  4. In Show commands containing, search for and select Edit.ToggleLineComments.
  5. Click the Press shortcut keys box and press ctrl + /.
  6. Click Assign to assign the new hotkey.

Note that unbinding the already existing function for ctrl + / is necessary for this to work.

2

I don't know if it is the same in 2012 but in 2015 you can.

Go to Tools > Options > Environment > Keyboard

Either, find "VisualD.ToggleCommentSelection" or
Search for "comment" and the bottom option should be "VisualD.ToggleCommentSelection"

Snip showing how to set toggle comment shortcut.

In visual studio 2019 this works by default using Ctrl + K and then Ctrl + /

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like