Comment and Uncomment Code

Tips Search

Comment and Uncomment Code

  • Comments 9

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:

image

 

And, of course, we have the Menu items:

image

 

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:

image

 

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

image

 

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):

image

 

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

image

 

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

  • 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.

  • Simple one ; but one that you use regularly

  • 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 */.

  • 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

    Z

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

  • 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?

  • Hey Michael :)

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

    Z

  • 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

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

       ''' <summary>

       '''

       ''' </summary>

       ''' <value></value>

       ''' <returns></returns>

       ''' <remarks></remarks>

Page 1 of 1 (9 items)
Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post