741

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?

1
  • How to comment multiple lines when one of the line is already commented html Commented Jul 30, 2019 at 2:28

45 Answers 45

938

Windows: Shift + Alt + A

Linux: Shift + Ctrl + A

macOS: Shift + Option + A

Here you can find all the keyboard shortcuts:

Windows Visual Studio Code shortcuts

Linux Visual Studio Code shortcuts

macOS Visual Studio Code 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.

Sign up to request clarification or add additional context in comments.

17 Comments

This 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).
You can find the comment options in the Edit menu if you forget the keyboard shortcuts.
ctrl + shift + A worked for me for adding multi-line comment.
CTRL + / works great for any commenting with the Vim emulator extension.
NOTE for Mac users: in general, shortcuts that are Ctrl+Whatever on Windows are Command+Whatever on Mac; In particular this is Command+/ to toggle or Command+K,Command+C to add a layer of comments.
|
346

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 documentation: Key Bindings for Visual Studio Code

12 Comments

Sadly, 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.
On mac, it's cmd+- (cmd and minus).
Weird, with me it's cmd+= (macos sierra and vscode 161)
I'm using it on js file. But it doesn't work for /* .. */ multiline comment. instead, it puts/removes '//' infront of all the lines selected
@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.
|
231

How to comment out multiline / single line in Visual Studio 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: FilePreferencesKeyboard Shortcuts.

macOS: CodePreferencesKeyboard 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 documentation: Key Bindings for Visual Studio Code

2 Comments

comment on remapping the settings stackoverflow.com/a/62676435/913098
Shift Alt A does NOT comment things out on Windows, it just wraps text in quotations.
115

The keyboard shortcut to comment multiple lines in Windows is shift+alt+A.

3 Comments

This 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.
How can I change this shortcut, I can't seem to find it in File > Preferences > Keyboard Shorcuts?
this 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
57

In my case, Ubuntu, the shortcut is ctrl+shift+A.

3 Comments

in my ubuntu 16 does not work, strange, i can change keys but maybe depends on the file extension?
finally a shoutout for Linux in the 4th answer! works on 20.04
Works on Fedora too! Thank you for your comment, <kbd>shift</kbd>+<kbd>alt</kbd>+<kbd>A</kbd> was not working for me!
43

You can see all available keybindings on the official documentation.

Here's the relevant bit for OS X:

Key            Command

⌘ + K, ⌘ + C   Add line comment
⌘ + K, ⌘ + U   Remove line comment
⌘ + /          Toggle line comment
⇧ + ⌥ + A      Toggle block comment

You will need to select the lines you want to comment first, and then execute the above shortcut, i.e., + / on OS X, and Ctrl / on Windows.

3 Comments

But it doesn't talk about multiline comment.
@AmitGupta: ⌘/ Toggle Line Comment <-- this works for multiline too.
highlight multiple lines and ⌘K then ⌘C or ⌘K then ⌘U will work. ⌘/ also does the same thing. @AmitGupta
34

Try option 1 first. If it doesn't work for you then try option 2 which is the actual shortcut for Visual Studio Code:

Option 1. Alt / Option + Shift + A

Option 2. While the text is selected hit Ctrl / Cmd+K and then Ctrl / Cmd+C.

As of now (version 1.16.3), you can check all available keyboard shortcuts by going into menu FilePreferencesKeyboard shortcuts. Here you can search for shortcuts related to code comments.

For users who are coming from Sublime Text or likes to have Ctrl+Shift+/, you can make the change from the above mentioned settings panel or simply install the Sublime Text Keymap extension by Microsoft.

As Visual Studio Code has become pretty much the standard editor for almost everything these days, I would say picking option 1 is always helpful.

1 Comment

Ctrl K + Ctrl U to uncomment
32

In macOS, comment out multi-lines in Visual Studio Code by this keyboard shortcut:

Option + Shift + A

/*
...
...
*/

Or, select all lines and then press Ctrl + /.

3 Comments

Thank you! This is MULTILINE commenting, while other suggest SINGLE LINE commenting for multilines.
how to undo it ?
my comment <!-- --> how to do like that ?
23

For me to comment, I use:

Ctrl + k, Ctrl + c

And to uncomment:

Ctrl + k, Ctrl + u

I am using Ubuntu 18.04 (Bionic Beaver).

1 Comment

This worked on my Windows box when working on files in WSL where WSL was running Ubuntu.
21

For my keyboard layout it's Ctrl + § for:

//
//
//

And Shift + Alt + A for:

/*
...
*/

1 Comment

Shift+Alt+A for mac OS
17

To comment multiple lines in Visual Studio Code, use

Shift + Alt + A

To comment a single line, use

Ctrl + /

2 Comments

Perfect! - [ctrl] + [/] woks best for me to comment out multiple lines.
Using ubuntu on virtual machine, left ctrl + / worked.
15
  1. Select all the lines you want to comment

  2. Ctrl + /

1 Comment

Seems 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.
14

It should be the same as Microsoft Visual Studio with:

  • Ctrl + up arrow/down arrow: to select
  • Ctrl + K, then Ctrl + C :to comment
  • Ctrl + K, then Ctrl + U :to uncomment

Comments

11

All the key board shorcuts for VS code can be found in the link : Link

  • Add a Line comment Ctrl+K Ctrl+C
  • Remove a line comment Ctrl+K Ctrl+U
  • More shortcut Ctrl+/

Comments

10

Windows 10 with French / English Keyboard Ctrl key + /, Ctrl + k + U and Ctrl + K + l don't work.

Here's how it works:

/* */ SHIFT + ALT + A

// CTRL + É

The É key is next to right Shift.

Comments

9

On Windows 10 you can use CTRL + } to comment/uncomment on any line or block of code.

enter image description here

Comments

9

Ctrl + Shift + A for CSS multiline

Comments

8

For multiline commenting in Python VSCode, all you have to do it just:

Ctrl + /

Comments

8

To comment multiple lines in Visual Studio Code, you can use the following keyboard shortcut:

  • Windows/Linux: Ctrl + /
  • Mac: Cmd + /

Comments

7

In the new version of Visual Studio Code (version 1.26.1)

Visual Studio Code version

  1. Go to menu FilePreferencesKeyboard Shortcuts
  2. Search for comment, and double-click the existing shortcut 'keybinding`
  3. And press the new keyboard shortcut. In my case, it is Ctrl + Shift + /

You can assign any shortcuts you want.

Enter image description here

Comments

7

If you are using Visual Studio Code in a Linux environment, then you can comment multiple lines by either:

Selecting a block of code => then, press Ctrl + Shift + A (Block commenting)

Or, selecting a block of code => then, press Ctrl + / (single-line commenting applied to all selected lines)

Comments

7

You can find the shortcut in the Edit menu:

Menu EditToggle Block Comment* → Shift + Alt + A

Comments

6

In Visual Studio 2017 we do a: Comment Selection:

Ctrl + K, Ctrl + C

Press Ctrl + K to get the shortcut. Press Ctrl+C to confirm.

http://visualstudioshortcuts.com/2017/

4 Comments

For some reason I have trouble with this one. I usually have to press multiple times. CTRL + ALT + A works the best for me.
This one worked fine for me. A nice add-on to this answer is "How to uncomment".
to uncomment use Ctrl + k, Ctrl + U
The link is broken: "Hmm. We’re having trouble finding that site. We can’t connect to the server at visualstudioshortcuts.com."
5

enter image description here

Select lines which you want to Comment

Then press Ctrl + / to make selected lines comment

enter image description here

And to uncomment:
Select the commented lines you which want to uncomment
First press Ctrl + K then Ctrl + Uto make commented lines uncomment

Comments

5

For me it was ctrl + ' (I am using a Swedish layout for the keyboard).

1 Comment

Same for Finnish layout, which is actually same as Swedish.
5

For doing it on Mac: CMD + Shift + 7

Comments

4

CTRL + / comment / uncomment multiple lines of code

Ctrl+k+u for upper case latter

ctrl+k+l for lower case latter

Comments

4

If your Ctrl+/ isn't working maybe you need to change it to Ctrl+NumPad_Divide.

1 Comment

CTRL+/ worked for me in CentOS 7
4

In Windows, you need to press Ctrl + /. On Mac, you can do + / to make multiple comments:

// public function index()
// {
//     $badgeIcon = BadgeIcon::all();
//     return $this->showAll($badgeIcon);
// }

Then if you want to uncomment, then select the lines again and then press Ctrl + / (in Windows) again + / (on Mac).

Comments

4

Multi-line comment in Visual Studio Code

To comment the code select the lines and hit: Alt + Shift + A

To uncomment, select the commented code and hit: Alt + Shift + A

Enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.