Skip to main content

Command Palette

Search for a command to run...

Multi-line Commenting in Visual Studio Code on Mac: A Quick Guide

Published
4 min read
Multi-line Commenting in Visual Studio Code on Mac: A Quick Guide
P

As an experienced Linux user and no-code app developer, I enjoy using the latest tools to create efficient and innovative small apps. Although coding is my hobby, I still love using AI tools and no-code platforms.

Have you ever needed to temporarily comment out a chunk of code in Visual Studio Code but didn't want to add comments line-by-line? Commenting out multiple lines is a common task for developers, but the shortcuts in VS Code aren't obvious. I've been there myself - trying every keyboard combo under the sun to try and comment out my code.

After banging my head on the desk enough times, I finally learned how to properly comment out blocks of code in one go. And luckily, it's easy once you know the simple tricks.

In this article, I'll walk you through exactly how to comment out multiple lines in VS Code on a Mac so you can save time and frustration.

Why Comment Out Code in VS Code?

Let's quickly cover why you'd want to comment out code chunks. There are a few common reasons:

  • Testing - Commenting out blocks lets you easily test how your program runs without certain components. No need to fully delete big sections while testing!

  • Debugging - When tracking down bugs, comment out various parts to isolate the problem area. Much faster than deleting code you might need later!

  • Temporary removal - Sometimes you want to remove code but might use it again later. Commenting it out gets it out of the way until you need it back.

I'm sure you have your own reasons - but in any case, commenting makes code deactivation quick and easy!

How to Comment Out Multiple Lines in VS Code on a Mac

Now let's dig into exactly how to comment out multiple lines of code in one fell swoop:

1. Highlight the code lines

First, highlight the full block of code you want to comment out. Click at the start of the first line you want to be commented out, then scroll to the end of the block and Shift+Click at the end of the last line. This will highlight the entire code portion you want commented on.

2. Open the Command Palette

Next, you need to open the Command Palette. Click View > Command Palette in the menu OR use the keyboard shortcut Shift+Command+P. This brings up a search field in VS Code.

3. Type "Toggle Line Comment"

In the Command Palette search bar, type in "Toggle Line Comment" and hit Enter.

4. See Your Code Commented!

That's it! After hitting Enter to run the Toggle Line Comment command, VS Code will comment out the selected lines. It adds // before each line, just like magic.

Now your code block is neatly commented out and won't run. Feel free to make use of those programming superpowers!

Custom Keyboard Shortcuts

If you want even faster access to comment toggling, you can also set up a custom keyboard shortcut.

Here's how to create a shortcut on a Mac specifically for multi-line commenting:

  1. Go to Code > Preferences > Keyboard Shortcuts

  2. Search for the "Toggle Line Comment" action

  3. Click the plus icon to add a shortcut like "Cmd+Shift+/“

  4. Hit Enter and that shortcut is now assigned!

Now your custom shortcut will instantly comment out any highlighted code. Pretty slick stuff to save yourself some headache!

Don't Forget to Uncomment Later!

One last tip - don't forget to uncomment your code blocks once you're ready to restore them!

After you've finished testing or debugging with code commented out, highlight the relevant section and use the Toggle Line Comment command again. This will conveniently remove all the comments so you can use that code once more.

No one wants to deal with a tangled mess of outdated comments after all! Removing comments is as simple as adding them.

Wrap Up

And there you have it - an easy way to rapidly comment out multiple lines of code in VS Code on a Mac!

No more wasting time adding line-by-line comments or fiddling with custom shortcuts. Just use the handy Toggle Line Comment command to comment/uncomment blocks with a click.

Give it a try for your next debugging or code-testing session. I guarantee it'll save you plenty of effort while making changes. Once you get the hang of this trick, you'll be commenting out code quicker than ever.

Let me know in the comments if you have any other VS Code tips and tricks up your sleeve. I could always use more efficiency wins. Now get back to coding.

More from this blog

T

TheTechDeck | Tech Made Simple for Everyone

772 posts

Explore the best tech tips and tricks for Windows, Mac, Linux, Android, and gaming. Simplify tech with TechUvy's expert guides