Comment and Uncomment Code

Keyboard:  CTRL + K, CTRL + C (comment); CTRL + K, CTRL + U (uncomment)
Menu:  Edit -> Advanced -> Comment Selection; Edit -> Advanced -> Uncomment Selection
Command:  Edit.CommentSelection; Edit.UncommentSelection
Versions:  2008,2010
Published:  4/13/2010
Code:  vstipEdit0047


Download the seriously cool Tip of the Day Extension to get the daily tips delivered to your Start Page!


Sometimes it's the simple stuff we forget about.  So I present to you the classic Comment and Uncomment Selection.  Naturally, you have the Comment and Uncomment buttons:



And, of course, we have the Menu items:



But it's the keyboard shortcuts that really rock!  These will, predictably, comment or uncomment lines of code for you.  So, let's say you have some code you want commented out.  Just select it:



Then press CTRL + K, CTRL + C (in this example):



Voila!  It's commented out.  Okay, great, but what if you don't want to use the mouse?  No problem!  Just hold ALT + SHIFT + [UP or DOWN ARROW] to do a vertical selection (NOTE:  In VS2008 you have to go right or left one character before you can go up or down for vertical selection):



Then press CTRL + K, CTRL + U (in this example):



And there you go!  Comment and Uncomment actions anytime you want!

Comments (11)
  1. Paresh says:

    One more interesting thing about this is that this shortcut works in sql server as well. Also it is smart enough to know how to comment in vb, or c# or HTML.

    So you have to learn it only once and use it in multiple places.

  2. Jeev says:

    Simple one ; but one that you use regularly

  3. Daniel Rose says:

    A very useful command. The only annoying part is that it works slightly differently in C++ and C#.

    C#: Each line where some text is selected is commented at the line-start with double-slash. If nothing is selected, the line where the cursor is is commented.

    C++: If nothing is selected or complete lines are selected, it behaves as above. However, if parts of a line are selected, and no comment is selected as part of the selection (ex. select something in the middle of a code line), then the selection is surrounded by /* and */.

  4. zainnab says:

    yeah that drives me crazy, too.  Also there is currently no CSS support for commenting  but it is coming in the next rev i believe


  5. I use this with SQL Server all of the time.  I have put an item in the tool bar for easy access.

  6. Michael says:

    Why not have it comment/uncomment without hitting a combination of keys like you're playing a Street Fighter game?

    Ctrl K and then Ctrl C? Why not Ctrl+Shift+C or something similar?

  7. zainnab says:

    Hey Michael 🙂

    You can always rebind the keys to anything you want.  See this post for more information:…/keyboard-shortcuts-creating-new-shortcuts-vstiptool0063.aspx


  8. Neerja says:

    Another interesting thing is that if you reverse the order of K and C then you can switch between single line and multi line comments

  9. Andreas says:

    in, when you type 3 times ' on top of a function, class or property, then will autogenerate a comment :

       ''' <summary>


       ''' </summary>

       ''' <value></value>

       ''' <returns></returns>

       ''' <remarks></remarks>

  10. aa says:


  11. Perrin says:

    Trying to do this in Visual Studio 2013, but it always just says

    "The key combination (Ctrl+K, Ctrl+U) is bound to command (Uncomment Selection) which is not currently available."

    The #$%^ its not! I'm editing a local C# file in my project like normal, with a simple block of single-line-commented-out code selected!

    So yeah. Doesn't work for me, because why NOT add more random stupidity?

Comments are closed.

Skip to main content