Comments are generally used for making code easy to understand and maintain. Commenting out sections of code is also part of the debugging process.
Now, diferent programming languages have different ways of adding comments. To add comments in bash, you use # and in C/C++, you use //.
Things become easier when you use a code editor like VS Code. The editor has hotkeys to add comments irrespective of the programming language.
Select the desired lines of code and then use Ctrl + / to comment them out in VS Code.
Not clear yet? Let me go in details.
Comment out a block of code in VS Code
The first step in commenting out a block of code is to select lines with the mouse. You can also use Ctrl + shift with down and up arrow keys:
But selecting the entire block of code might not be the most convenient way, especially while dealing with large projects. In that case, you can collapse the entire block of code by clicking on the little arrow located beside the number of lines:
Once you select the code in any of the shown ways, you just have to use Ctrl + / (Forward slash), and the selected block will be commented out:
But this was specific to only comment out the block of codes, but what if I tell you can comment on multiple lines placed randomly?
Bonus Tip: Comment out multiple lines placed randomly in VS Code
Need to comment out multiple lines that are not together? You can of course keep on pressing Ctrl+/ when you are on the desired lines.
But there is a neat trick pro VS Code users love to use here.
Hold the Alt key and click anywhere inside the line you want to comment out. And you’d see multiple cursors, something like this:
Once you are done placing cursors at desired lines, you can comment them out by using Ctrl + /:
A pretty neat way to comment out lines. Isn’t it?
This was a quick tutorial on how you can comment out multiple lines in VS Code. While VS Code is open source, the installation files contain tracking to help them improve user experience.
And if you want to have complete control, you can use VSCodium which is a clone of VS Code but does not track your data.
I hope this quick little tip on adding comments in VS Code help you with this awesome code editor.