How to comment multiple lines in Visual Studio Code?
Solution 1
Windows: Shift+Alt+A
Linux: Shift+Ctrl+A
Here you can find all the keyboard shortcuts:
PS: I prefer Ctrl+Shift+/ for toggling block comments because Ctrl+/ is shortcut for toggling line comments so it's naturally easier to remember. To do so, just click on the settings icon in the bottom left of the screen and click 'Keyboard Shortcuts' and find "toggle block...". Then click and enter your desired combination.
Solution 2
First, select the lines you want to comment/uncomment (CTRL+L is convenient to select a few lines)
Then:
-
To toggle line comments, execute
editor.action.commentLine
(CTRL+/ on Windows)or
-
To add line comments, execute
editor.action.addCommentLine
(CTRL+K CTRL+C)To remove line comments, execute
editor.action.removeCommentLine
(CTRL+K CTRL+U)or
To toggle a block comment, execute
editor.action.blockComment
(SHIFT-ALT-A)
See the official doc : Key Bindings for Visual Studio Code
Solution 3
How to comment out multiline / single line in VS Code:
MacOS:
Shift + Option + A:
/* multiline
comment */
CMD + /:
// single line comment
Windows:
Shift + Alt + A:
/* multiline
comment */
CTRL + /:
// single line comment
How to remap / change these shortcuts?
Windows: File > Preferences > Keyboard Shortcuts.
MacOS: Code > Preferences > Keyboard Shortcuts.
You can search through the list both by keybindings (key names) and command names.
Read: How to set shortcuts from another text editor (Sublime, Atom etc)?
Official docs: Key Bindings for Visual Studio Code
Solution 4
The keyboard shortcut to comment multiple in Windows is shift+alt+A.
Solution 5
In my case, Ubuntu, the shortcut is ctrl+shift+A.
![gog](https://i.stack.imgur.com/xr3hP.jpg?s=256&g=1)
gog
Updated on July 08, 2022Comments
-
gog almost 2 years
I cannot find a way to comment and uncomment multiple lines of code in Visual Studio Code.
Is it possible to comment and uncomment multiple lines in Visual Studio Code using some shortcut? If yes, how to do it?
-
Mario almost 8 yearsSadly,
cmd+/
is not a valid shortcut for a German (Mac) keyboard. FWIW: I changed it to{ "key": "cmd+shift+7", "command": "editor.action.commentLine", "when": "editorTextFocus" }
to match Atom. -
Jo Smo over 7 yearsOn mac, it's cmd+- (cmd and minus).
-
xastor over 7 yearsWeird, with me it's cmd+= (macos sierra and vscode 161)
-
Amit Kumar Gupta over 7 yearsI'm using it on js file. But it doesn't work for /* .. */ multiline comment. instead, it puts/removes '//' infront of all the lines selected
-
Amit Kumar Gupta over 7 yearsBut it doesn't talk about multiline comment.
-
codepleb about 7 years@Mario: I struggled for a long time, until I finally decides to move from my Swiss-German Layout to the US-Layout. The umlauts are the only Problem, but on Mac you get used to the "workaround" fast. The US Layout is the standard for (nearly) all programs, when it comes to shortcuts, so I would consider switching.
-
Eran Or about 7 yearsShift+Alt+A for mac OS
-
Admin about 7 yearsSeems a bit odd that vscode uses Shift+Alt+A for block comment when line comment is Ctrl+/. ReSharper uses Ctr+Alt+/ and Ctrl+Shift+/ for line and block comment respectively. For vscode one can see that the Shift-Alt-A keys (for block comment) are somewhat easier to press than typical three key combos but it's so non-intuitive as a keyboard shortcut. I configure ReSharper (could also do for regular Visual Studio) to use Ctrl+/ and Ctrl+Shift+/ for line and block comment respectively and vscode to use Ctrl+Shift+/ for block comment so that they are both the same and intuitive.
-
laishiekai almost 7 years@AmitGupta: ⌘/ Toggle Line Comment <-- this works for multiline too.
-
chinnychinchin over 6 yearsIt is worth noting: if you have other key-binding/mapping extensions installed and enabled, they can interfere with the base VScode bindings. For example, I had the mapping extensions for Sublime Text enabled. Once I disabled it (cmd + / (mac)) worked for comments.
-
Adam Cox over 6 yearsUsing either the suggested, or comment blocking toggle
ctrl + shift + a
inside the grave-block in Angular template places either//
or/* */
, respectively. Feature request for contextual comment blocking? -
Firze over 6 yearsSame for Finnish layout, which is actually same as Swedish.
-
Eduard over 6 yearsThank you! This is MULTILINE commenting, while other suggest SINGLE LINE commenting for multilines.
-
hraban over 6 yearsThis one is better in some languages because it toggles "Block Comment". CTRL-/ toggles "Line Comment" which doesn't work in e.g. CSS or HTML.
-
pythlang about 6 yearshighlight multiple lines and ⌘K then ⌘C or ⌘K then ⌘U will work. ⌘/ also does the same thing. @AmitGupta
-
John Smith almost 6 yearsFor some reason I have trouble with this one. I usually have to press multiple times. CTRL + ALT + A works the best for me.
-
Casey Kuball almost 6 yearsThis is probably not the top voted answer because of how VS Code works for different languages. For python code, the "comment block" command Alt + Shift + A actually wraps the selected text in a multiline string, whereas Ctrl + / is the way to toggle any type of comment (including a "block" comment as asked here).
-
sianipard almost 6 yearsHow can I change this shortcut, I can't seem to find it in File > Preferences > Keyboard Shorcuts?
-
Ferroao over 5 yearsin my ubuntu 16 does not work, strange, i can change keys but maybe depends on the file extension?
-
Saba Ahang over 5 yearsthis is the only one that I was looking for. Simply because it's selection comment not whole line comment. It wraps the selection with
/**/
not the whole line. I'm on Windows 10 -
ckkkitty over 5 yearsI found the suggestion to click on the settings icon and clicking Keyboard Shortcuts very helpful.
-
Chris Koester over 5 yearsYou can find the comment options in the Edit menu if you forget the keyboard shortcuts.
-
Avani Khabiya about 5 yearsctrl + shift + A worked for me for adding multi-line comment.
-
akinuri almost 5 yearsIsn't there a way to add these to editor context menu? I'd like to add comment/uncomment commands to the context menu.
-
Tiago Martins Peres over 4 yearsThis one worked fine for me. A nice add-on to this answer is "How to uncomment".
-
tdensmore over 4 yearsNOTE: keyboard shortcuts for comments do not appear to work until the file is saved - presumably because VSCode needs to know the file context to add the correct comment type. So none of these will work until you save the file...
-
Chris Hayes over 4 yearsCTRL + / works great for any commenting with the Vim emulator extension.
-
Yacine Rouizi about 4 yearsto uncomment use
Ctrl
+k
,Ctrl
+U
-
mr.boris almost 4 yearsThanks,
Shift + Alt + A
what i've been looking for -
java-addict301 over 3 yearsfinally a shoutout for Linux in the 4th answer! works on 20.04
-
Kamlesh over 3 yearsnot working for me. What is your version of VS Code editor?
-
Gulzar over 3 yearscomment on remapping the settings stackoverflow.com/a/62676435/913098
-
Juan Antonio Tubío over 3 yearsIn some keyboards, its ctrl + ç
-
carloswm85 about 3 yearsFor whatever reason, in my VSCode 1.55.2,
Ctrl+Shift+/
norCtrl+/
work. Though they're correctly set in theFiles >Preferences > Keyboard Shortcuts
menu. I had to manually change the keybinding. Now,Toggle Line Comment
uses the keybinding ofCtrl + NumPad_Divide
which is the same asCtrl+/
, but VSC seems to think otherwise. Probably the issue is somewhere in my system (W10), but this solution fixed it. -
Robert almost 3 yearsPerfect! - [ctrl] + [/] woks best for me to comment out multiple lines.
-
Daniel Martin almost 3 yearsNOTE for Mac users: in general, shortcuts that are
Ctrl+Whatever
on Windows areCommand+Whatever
on Mac; In particular this isCommand+/
to toggle orCommand+K,Command+C
to add a layer of comments. -
Gidi9 almost 3 yearsOn a German keyboard for me, I used
cmd + shift + /
and it worked. -
Cyril N. almost 3 yearsWorks on Fedora too! Thank you for your comment, <kbd>shift</kbd>+<kbd>alt</kbd>+<kbd>A</kbd> was not working for me!
-
Mona Jalal over 2 yearsCTRL+/ worked for me in CentOS 7
-
Ankit Maheshwari over 2 years[Shift + Option + A] is correct answer. Thanks.
-
user5249203 over 2 yearshow to undo it ?
-
curios about 2 yearsThe URL for "All VSCode Shortcuts" is for windows. Mac users, the link is: code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf