3 ways to Comment Out Lines in Visual Studio Code (VS Code)

Working in VS Code, youโ€™ve probably run into moments like these:

  • You want to temporarily disable a block of code without deleting it
  • Youโ€™re debugging and need to isolate a specific section
  • Youโ€™re editing config files (like YAML) and need to safely ignore some lines

Instead of removing code (and risking mistakes), the smarter move is to comment out lines. The good news? VS Code makes this incredibly fast with shortcuts and built-in tools.


๐Ÿ”‘ Key Takeaways

  • Use keyboard shortcuts for the fastest way to comment code
  • Line comments work best for files like YAML, Python, or shell scripts
  • Block comments are useful for larger sections in languages like JavaScript or CSS
  • You can toggle comments on and off instantly
  • Mastering this saves time when debugging or testing changes

Method 1: Use Line Comment (Best for YAML & Simple Files)

This is the most commonly used method, especially in files like YAML, Python, or Bash.

โœ… How it works

  1. Select multiple lines (e.g., lines 14โ€“31)
  2. Press the shortcut:
OSShortcut
MacCmd + /
Windows/LinuxCtrl + /

๐Ÿ’ก Example

Before:

database:
  host: localhost
  port: 5432
  user: admin

After commenting:

# database:
#   host: localhost
#   port: 5432
#   user: admin

๐Ÿ‘‰ VS Code automatically adds # (or the correct symbol for your language).


Method 2: Use Command Palette (More Control)

If you prefer a GUI-style approach, this method is for you.

โœ… Steps

  1. Select the lines
  2. Open Command Palette:
    • Mac: Cmd + Shift + P
    • Windows/Linux: F1
  3. Type: Toggle Line Comment
  4. Press Enter

๐Ÿ’ก What happens

VS Code applies or removes comments based on the current state.


Method 3: Use Block Comment (Best for Large Sections)

For larger chunks of code, block comments are cleaner.

โœ… Shortcut

OSShortcut
MacShift + Option + A
Windows/LinuxShift + Alt + A

๐Ÿ’ก Example (JavaScript)

Before:

See also: Mastering the Linux Command Line โ€” Your Complete Free Training Guide

console.log("start");
console.log("processing");
console.log("end");

After:

/*
console.log("start");
console.log("processing");
console.log("end");
*/

How to Uncomment Lines

Uncommenting is just as easy:

  • Select the commented lines
  • Press:
    • Mac: Cmd + /
    • Windows/Linux: Ctrl + /

๐Ÿ‘‰ VS Code will remove the comment symbols automatically.


Comparison of Methods

MethodBest ForShortcutNotes
Line CommentYAML, Python, BashCmd/Ctrl + /Fastest & most used
Command PaletteAll usersSearch commandNo shortcut needed
Block CommentJS, CSS, C-like languagesShift + Option/Alt + ACleaner for large blocks

Step-by-Step Process (Quick Workflow)

If you just want the fastest way:

  1. Open your file in Visual Studio Code
  2. Highlight the lines you want to comment
  3. Press:
    • Cmd + / (Mac) or Ctrl + / (Windows/Linux)
  4. Verify the comment symbols appear
  5. Press the same shortcut again to undo

FAQ

1. Why is my shortcut not working?

Check:

  • Your keyboard layout
  • VS Code keybindings settings
  • Conflicts with other extensions

2. Why does block comment not work in YAML?

Because YAML only supports line comments (#), not block comments.


3. Can I customize shortcuts in VS Code?

Yes:

  • Go to Keyboard Shortcuts
  • Search for Toggle Line Comment
  • Assign your own keybinding

4. Does this work for all programming languages?

Yes. VS Code automatically applies the correct comment syntax based on the file type.


David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 629

Leave a Reply

Your email address will not be published. Required fields are marked *