Tutorial: How to Comment and Uncomment Multiple Lines in Visual Studio Code

August 27, 2023 (9mo ago)

Working with Visual Studio Code (VS Code) becomes smoother and more efficient when you master the use of shortcuts. This guide focuses on the shortcuts for commenting and uncommenting lines of code, a common task that can be streamlined with the right keystrokes. We'll talk you through each method, providing clear instructions and tips for customizing these commands. Let's dive in!

TL;DR

Select the code you want to comment out first, then:

  • Windows: Press Ctrl + /
  • macOS: Press Command + /

A Quick Guide to Commenting and Uncommenting in VS Code

Single Line Comments

For those quick, one-line notes, here's how to comment and uncomment a single line of code:

  • For Windows: Add a comment with Ctrl+K, Ctrl+C and remove it with Ctrl+K, Ctrl+U.
  • For Mac: Use COMMAND + / to both add and remove single line comments.

Multiple Lines Commenting

Sometimes, you'll want to comment out entire sections of your code. Here's how:

Using Standard Keyboard Shortcuts

  1. Open your file in VS Code and select the code block you'd like to comment or uncomment.

  2. Depending on your OS, press the following keys:

    • Windows: Alt+Shift+A
    • Mac & Ubuntu: Ctrl + Shift +A

Applying OS-Specific Shortcuts

  • On Windows: Comment lines with CTRL + K + C and uncomment with CTRL + K + U.
  • On Mac: Use CMD + K + C to comment and CMD + K + U to uncomment lines.

Universal Shortcut:

Use Ctrl + / on Windows or Command + / on Mac to toggle comments on and off.

Customizing Your Comment Shortcuts

VS Code allows you to customize these shortcuts to suit your workflow. Here's how:

  1. Navigate to 'Keyboard Shortcuts' under 'Preferences' in the 'File' menu for Windows or the 'Code' menu for MacOS.
  2. Search for keybindings (key names) and command names, and modify them as needed.

Bonus: Code Formatting Shortcut

Having neatly formatted code can significantly improve readability. Here are the shortcuts for formatting code:

  • Windows: Shift + Alt + F
  • MacOS: Shift + Option + F
  • Ubuntu: Ctrl + Shift + I

Mastering these shortcuts and customizing them to your liking can significantly enhance your coding efficiency in VS Code. Remember, practice makes perfect, so don't hesitate to use these shortcuts regularly to become proficient. Happy coding!

Want to write a blog? Try Hexo+Vercel