How to Comment Multiple Lines in VS Code: Line vs Block Commands
Select your code and press Ctrl + / (Windows/Linux) or ⌘ + / (macOS) to toggle line comments, or Shift + Alt + A (Windows/Linux) or ⇧ + ⌥ + A (macOS) to toggle block comments.
VS Code provides built-in commands to comment multiple lines efficiently using either line-by-line prefixes or block wrapping delimiters. According to the microsoft/vscode repository source code, these features are implemented in the editor's comment contribution layer and respect language-specific tokens defined in the language configuration registry. Understanding how to comment multiple lines in VS Code improves your editing workflow across any programming language.
Keyboard Shortcuts for Commenting Multiple Lines
VS Code offers two distinct approaches for commenting code: line comments that prepend tokens to each line, and block comments that wrap selections in start/end delimiters.
Toggle Line Comments (editor.action.commentLine)
The most common method to comment multiple lines is the Toggle Line Comment command, identified internally as editor.action.commentLine.
- Windows/Linux:
Ctrl + / - macOS:
⌘ + /
When you select multiple lines and execute this command, VS Code prepends the language-specific line-comment token (such as // in JavaScript or # in Python) to every selected line. Press the same shortcut again to uncomment the lines.
According to the source code in src/vs/editor/contrib/comment/browser/lineCommentCommand.ts, the LineCommentCommand class handles the logic for detecting existing comments and toggling them on or off across the selection.
Toggle Block Comments (editor.action.blockComment)
For wrapping an entire selection in block delimiters like /* ... */, use the Toggle Block Comment command (editor.action.blockComment).
- Windows/Linux:
Shift + Alt + A - macOS:
⇧ + ⌥ + A
If no text is selected, this command inserts an empty block comment pair and places the cursor between the delimiters. The implementation resides in src/vs/editor/contrib/comment/browser/blockCommentCommand.ts, which manages the insertion and removal of block comment tokens around the selected range.
Accessing Commands via Command Palette and Context Menu
If you prefer not to use keyboard shortcuts, open the Command Palette (Ctrl + Shift + P or ⌘ + Shift + P) and type:
- "Toggle Line Comment"
- "Toggle Block Comment"
Both commands also appear in the right-click context menu under the "Comment" submenu, allowing you to trigger them with your mouse.
Technical Implementation in the VS Code Source
The commenting functionality is deeply integrated into the editor's language system. The microsoft/vscode repository reveals the following key files that power these features:
src/vs/editor/contrib/comment/browser/comment.ts– Registers theeditor.action.commentLineandeditor.action.blockCommentcommands with the editor.src/vs/editor/contrib/comment/browser/lineCommentCommand.ts– Implements the logic for adding and removing line-comment tokens on the current selection.src/vs/editor/contrib/comment/browser/blockCommentCommand.ts– Implements the logic for wrapping and unwrapping selections in block comment delimiters.src/vs/editor/common/languages/languageConfiguration.ts– Defines theLanguageConfigurationinterface, which includes thelineCommentandblockCommentproperties that specify comment syntax for each language.src/vs/editor/common/languages/languageConfigurationRegistry.ts– Registers the actual comment tokens (e.g.,//,/*,*/) used by the commands above.
VS Code reads the language configuration to determine the appropriate comment tokens for the current file type. If a language does not declare a block comment in its configuration, the block comment command will be unavailable for that file.
Practical Example: Commenting JavaScript
Here is how the shortcuts transform your code:
// Before: Select these three lines
function example() {
console.log("line 1");
console.log("line 2");
}
// After pressing Ctrl+/ (line comments):
// function example() {
// console.log("line 1");
// console.log("line 2");
// }
// After selecting the function and pressing Shift+Alt+A (block comment):
/*
function example() {
console.log("line 1");
console.log("line 2");
}
*/
Customizing Keybindings
You can modify these shortcuts in File → Preferences → Keyboard Shortcuts. Search for:
editor.action.commentLine– to change the line comment shortcuteditor.action.blockComment– to change the block comment shortcut
Assign any key combination that suits your workflow, and the changes will take effect immediately across all VS Code windows.
Summary
- Press
Ctrl + /(Windows/Linux) or⌘ + /(macOS) to toggle line comments on selected lines, implemented insrc/vs/editor/contrib/comment/browser/lineCommentCommand.ts. - Press
Shift + Alt + A(Windows/Linux) or⇧ + ⌥ + A(macOS) to toggle block comments around selections, implemented insrc/vs/editor/contrib/comment/browser/blockCommentCommand.ts. - Both commands read language-specific tokens from
src/vs/editor/common/languages/languageConfigurationRegistry.ts. - Access the commands via the Command Palette (
Ctrl + Shift + P) or the right-click context menu if you forget the shortcuts. - Customize keybindings by modifying
editor.action.commentLineoreditor.action.blockCommentin Keyboard Shortcuts settings.
Frequently Asked Questions
What is the shortcut to comment multiple lines in VS Code?
The default shortcut for line comments is Ctrl + / on Windows and Linux, or ⌘ + / on macOS. For block comments that wrap your selection, use Shift + Alt + A on Windows/Linux or ⇧ + ⌥ + A on macOS. These shortcuts invoke the editor.action.commentLine and editor.action.blockComment commands respectively.
How do I uncomment multiple lines in VS Code?
Select the commented lines and press the same shortcut you used to add the comments. VS Code detects the existing comment tokens through the logic in src/vs/editor/contrib/comment/browser/lineCommentCommand.ts (for line comments) or blockCommentCommand.ts (for block comments) and removes them automatically.
Why does the block comment shortcut not work in some files?
If a language does not define block comment delimiters in its language configuration (as defined in src/vs/editor/common/languages/languageConfiguration.ts), the editor.action.blockComment command will be inactive. Some languages, such as Python, do not support traditional block comments and only offer line comments, so only the line comment shortcut will function.
Where does VS Code store the comment syntax for each language?
VS Code reads comment delimiters from the language configuration registry, specifically defined in src/vs/editor/common/languages/languageConfigurationRegistry.ts and declared in src/vs/editor/common/languages/languageConfiguration.ts. Language extensions contribute these definitions, specifying the lineComment string (e.g., //) and blockComment start/end pairs (e.g., /* and */) for the editor to use.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s https://instagit.com/install.md