How to Comment Out Code in VS Code

Commenting out blocks of code is an essential practice for debugging and keeping your codebase clean. This article provides a quick guide on how to instantly comment and uncomment multiple lines of code in Visual Studio Code (VS Code) using keyboard shortcuts for Windows, macOS, and Linux.

The Quickest Method: Toggle Line Comments

The fastest way to comment out a highlighted block of code is to use the Toggle Line Comment shortcut. This method adds the language-specific single-line comment character (like // or #) to the beginning of every selected line.

  1. Highlight the block of code you want to comment out.
  2. Press the following keyboard shortcut:
    • Windows / Linux: Ctrl + /
    • macOS: Cmd + /

To uncomment the code, simply highlight the block again and press the same keyboard shortcut.

The Alternative Method: Toggle Block Comments

If you prefer to wrap your selected code inside a single block comment (such as /* ... */ in JavaScript or CSS), you can use the Toggle Block Comment shortcut.

  1. Highlight the block of code you want to wrap.
  2. Press the following keyboard shortcut:
    • Windows / Linux: Shift + Alt + A
    • macOS: Shift + Option + A

Like the line comment shortcut, pressing this combination again on the selected block will remove the comment.