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.
Table of Contents
๐ 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
- Select multiple lines (e.g., lines 14โ31)
- Press the shortcut:
| OS | Shortcut |
|---|---|
| Mac | Cmd + / |
| Windows/Linux | Ctrl + / |
๐ก 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
- Select the lines
- Open Command Palette:
- Mac:
Cmd + Shift + P - Windows/Linux:
F1
- Mac:
- Type: Toggle Line Comment
- 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
| OS | Shortcut |
|---|---|
| Mac | Shift + Option + A |
| Windows/Linux | Shift + 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 + /
- Mac:
๐ VS Code will remove the comment symbols automatically.
Comparison of Methods
| Method | Best For | Shortcut | Notes |
|---|---|---|---|
| Line Comment | YAML, Python, Bash | Cmd/Ctrl + / | Fastest & most used |
| Command Palette | All users | Search command | No shortcut needed |
| Block Comment | JS, CSS, C-like languages | Shift + Option/Alt + A | Cleaner for large blocks |
Step-by-Step Process (Quick Workflow)
If you just want the fastest way:
- Open your file in Visual Studio Code
- Highlight the lines you want to comment
- Press:
Cmd + /(Mac) orCtrl + /(Windows/Linux)
- Verify the comment symbols appear
- 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.




