Another keyboard shortcut that I use in Visual Studio lets me quickly comment out a block of code. There are menu items and toolbar buttons that also let us do this, but the keyboard shortcut lets me keep my hands on the keyboard and not look for icons.
They shortcut to comment out a block of code is "Ctrl+K, C" or "Ctrl+E, C" (as a reminder, I'm using the default C# settings in Visual Studio).
To comment out a selection, we first highlight the code:
And then just press "Ctrl+K, C":
That's it. Just another one of the many keyboard shortcuts available.
If you can't remember the shortcut, you can always use the "Edit" menu. Just go to "Edit -> Advanced":
And by default, there is also a button on the toolbar:
Reversing the process is just as easy: "Ctrl+K, U" or "Ctrl+E, U". Just highlight the block of commented code:
And press "Ctrl+K, U" to uncomment the selection:
As you can see, this uses the single-line comment notation (the //). This means that it will work in most situations. If a line already has a single-line comment, then the comment notations will be doubled-up. This means that if you then uncomment the same block, you will end up with the original code (including the original single-line comments).
One thing to watch out for is when using the delimited-comment notation ( /* */ ) in our code. If the delimited comments span multiple lines, then we want to make sure to include the entire delimited comment in the selection. Otherwise, we'll end up commenting out an opening or closing tag, and we'll be left with an unbalanced comment.
Ctrl+K or Ctrl+E?
You may have noticed that in this and the previous article about Format Document, we have the option of using "Ctrl+K" or "Ctrl+E" to start off the command. Which one should we use?
That's a question I can't quite answer. Currently, both keybindings work (again with the default C# settings). I'm used to using "Ctrl+K" to start off the shortcuts (whether formatting documents or commenting/uncommenting), so that's my current habit. In addition "Ctrl+K" starts off "insert snippet" or "Surround With", so that's less for me to try to remember.
You'll notice that the menu items in Visual Studio all have "Ctrl+E". This might lead you to believe that Ctrl+K is the old way of doing things and we should use Ctrl+E.
But in Visual Studio Code, the comment/uncomment functions are bound to "Ctrl+K" (from Visual Studio Code Key Bindings):
So at least for the time being, I'll continue to use "Ctrl+K". But either one will work.
Keyboard shortcuts can make us more efficient as we're typing in code, editing, or debugging. If we find ourselves using a feature frequently, instead of reaching for the menu or tool bar, we should practice using the keyboard shortcuts.
With just a few of these in our toolbox, we can get our work done more quickly.
Love ❤️ this awesome post ✍️ valuable & informative 👍ReplyDelete
Thanks a lot. So helpful.ReplyDelete
What, could you make a video tutorial about visual studio in C#? Thank youReplyDelete
can someone help me in knowing how to comment without commenting the code?ReplyDelete
a = ("Harry","Barry")
print(a) #I want to comment here without affecting the code
The "//" comment goes to the end of the line and stops in C# (and other C-style languages). So the following will work:Delete
print(a); // This is a comment
The "print(a);" part is code that will run. The "//" and everything after it is a comment.
thank you sirReplyDelete