100

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

12 Answers 12

45

Edit: As of Visual Studio 17.11 Preview 1, the default shortcut of toggle line comment has been changed to Ctrl+/, see https://learn.microsoft.com/en-us/visualstudio/releases/2022/release-notes-preview#toggle-line-comments


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.

12
  • 5
    Oddly, they do not work, not even the defaults, in VS 16.2.2. The error "The key combination (Ctrl-K, Ctrl-/) is bound to command (Toggle Line Comment) which is not currently available." appears at the bottom in the status bar for the default keystrokes, and it doesn't even do that much if you use a custom one. For now, I'll be giving the "Toggle Comment" plugin a try. Commented Aug 19, 2019 at 6:03
  • 1
    This did work for me and if you have VS 2019 is the best solution, actually, is THE solution. VS 12.2.5
    – Norcino
    Commented Oct 4, 2019 at 14:44
  • 1
    this should be the contemporary answer for VS 2019. Commented Oct 28, 2020 at 18:33
  • 1
    @orionelenzil Done :)
    – Marc
    Commented Jun 21, 2021 at 15:57
  • 2
    This still works in VS 2022. It's insane that this isn't a default yet.
    – JD Byrnes
    Commented Feb 6, 2022 at 6:47
59

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.

2
  • This should be the accepted answer, and still works in Aug 2020
    – olonge
    Commented Aug 29, 2020 at 20:18
  • Still works on vs 2019, win11 in Oct 2023! After trying so many methods :( Commented Oct 28, 2023 at 10:52
48

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

1- Download and Install > https://marketplace.visualstudio.com/items?itemName=munyabe.ToggleComment

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
  • 2
    Doesn't work in 2019
    – IC_
    Commented May 31, 2020 at 9:40
  • 10
    The secret to making this work was selecting TextEditor rather than global before pressing Assign.
    – Gulzar
    Commented Jul 16, 2020 at 12:53
  • @Herrgott Support for 2019 was supposedly added in v1.8
    – otoomey
    Commented Aug 2, 2020 at 22:41
  • @Gulzar confirmed, but what a stupid solution
    – mjr
    Commented Aug 7, 2020 at 15:55
28

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
  • 5
    so to directly answer the original question, assign CTRL + / to ReSharper.ReSharper_LineComment
    – Chin
    Commented Mar 9, 2016 at 1:59
  • You can also create custom shortcuts using above procedure by placing keys inside 'Press shortcuts keys', step by step procedure here: qawithexperts.com/article/c-sharp/… Commented Jul 31, 2021 at 8:41
8

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

1
  • The Visual Studio Code mappings are indeed the best way to go about this!
    – Chris
    Commented Dec 11, 2022 at 8:03
4

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
  • In a default install this shortcut will just comment, not uncomment.
    – heavyd
    Commented Feb 14, 2013 at 16:28
  • Clarified key combinations Commented Feb 14, 2013 at 16:39
  • Edit.CommentSelection doesn't show up in VisualStudio 2015 community edition.
    – allan ruin
    Commented Jul 30, 2015 at 14:53
2

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.

3
  • Steps 1-3 are sufficient in latest VS2019, which has ctrl + / as the Global default for Edit.ToggleLineComments.
    – victorlin
    Commented Nov 18, 2020 at 15:56
  • In Visual Studio 2022, <CTRL> / was not a Global Default. Commented Feb 22, 2022 at 18:50
  • This is the answer(VS 2022). After setting ToggleLineComment to CTRL+/, It didn't work. But removing the CTRL+/ from the GenerateDocumentationComments did make the command work. Both steps are needed. Thank you!
    – diox8tony
    Commented May 6 at 16:57
1

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.

1

In Visual studio 2022 you also need to install the workload for ASP.NET and webdevelopment if you want to use the shortcut for Edit.ToggleBlockComment, as is explained here. If this workload is not installed then pressing CTRL + K will generate an error message in the bottom left of the IDE that says something like The key combination (Ctrl-K, ....) is bound to command (....) which is not currently available.

You can search for the required workload by entering web workload in the searchbar at the top of the IDE. The workload should pop up as a search suggestion. To install this workload you need 2 GB of free diskspace on your computer. Alternatively you can also install the workload with the Visual studio installer app. You can easily find and open this app using the search bar of windows. Once the app has started click on Modify. Then under the tab Workloads select the workload ASP.net and web development and press Modify in the bottom right. Visual studio needs to be closed before you can start the workload installation.

When you've installed the workload for webdevelopment in ASP.NET, Javascript/HTML the shortcuts should work. You can change the shortcut of Edit.ToggleBlockComment to CTRL + / by going to Tools > Options > Environment > Keyboard where you can assign the shortcut. You can leave the Use new shortcut in field on Global. After that you can use CTRL+/ to both comment as well as uncomment a line of code.

0

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.

0

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

0

In visual studio 2022, you can find an extension to toggle blocks of comments. I use Toggle Comment 2022 and it's pretty straight forward. Ctrl+/ comment and uncomment.

icon of the extension

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .