625

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?

AjayKumarBasuthkar
  • 2,885
  • 2
  • 23
  • 27
gog
  • 11,788
  • 23
  • 67
  • 129
  • How to comment multiple lines when one of the line is already commented html – Vivek Shukla Jul 30 '19 at 02:28
  • **CTRL+K** creates a **-BREAKPOINT-**. Breakpoints don't respond to CTRL+/ so highlight your code, **use CTRL+K to remove the breakpoint**; then press CTRL+/ to uncomment your block. – Wolfpack'08 Sep 01 '22 at 10:15

42 Answers42

784

Windows: Shift+Alt+A

Linux: Shift+Ctrl+A

Here you can find all the keyboard shortcuts:

Windows VSCode Shortcuts

Linux VSCode 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.

Nomnom
  • 4,193
  • 7
  • 27
  • 37
saran3h
  • 12,353
  • 4
  • 42
  • 54
  • 80
    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). – Casey Kuball Aug 01 '18 at 19:50
  • I found the suggestion to click on the settings icon and clicking Keyboard Shortcuts very helpful. – ckkkitty Feb 21 '19 at 17:23
  • 3
    You can find the comment options in the Edit menu if you forget the keyboard shortcuts. – Chris Koester Feb 28 '19 at 19:58
  • 8
    **ctrl + shift + A** worked for me for adding multi-line comment. – Avani Khabiya Jun 10 '19 at 11:16
  • 1
    Isn't there a way to add these to editor context menu? I'd like to add comment/uncomment commands to the context menu. – akinuri Sep 05 '19 at 13:27
  • NOTE: 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... – tdensmore Dec 17 '19 at 23:27
  • 2
    CTRL + / works great for any commenting with the Vim emulator extension. – Chris Hayes Mar 18 '20 at 17:40
  • not working for me. What is your version of VS Code editor? – Kamlesh Oct 09 '20 at 13:15
  • 1
    For whatever reason, in my VSCode 1.55.2, `Ctrl+Shift+/ ` nor `Ctrl+/ ` work. Though they're correctly set in the `Files >Preferences > Keyboard Shortcuts` menu. I had to manually change the keybinding. Now, `Toggle Line Comment` uses the keybinding of `Ctrl + NumPad_Divide` which is the same as `Ctrl+/`, but VSC seems to think otherwise. Probably the issue is somewhere in my system (W10), but this solution fixed it. – carloswm85 May 06 '21 at 19:03
  • 1
    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. – Daniel Martin Jul 06 '21 at 14:16
  • 1
    The URL for "All VSCode Shortcuts" is for windows. Mac users, the link is: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf – curios Apr 03 '22 at 21:29
  • Even I tried the solution and the comments here. One thing I missed here is Terraform extension needs to be added to get it work. After installing the extension for my use case, it worked. Thanks. – thulasi39 Dec 21 '22 at 12:53
287

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

ROOT
  • 11,363
  • 5
  • 30
  • 45
Wosi
  • 41,986
  • 17
  • 75
  • 82
  • 16
    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. – Mario Sep 01 '16 at 08:48
  • 5
    On mac, it's cmd+- (cmd and minus). – Jo Smo Oct 11 '16 at 12:34
  • 1
    Weird, with me it's cmd+= (macos sierra and vscode 161) – xastor Oct 19 '16 at 11:23
  • 2
    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 – Amit Kumar Gupta Jan 27 '17 at 20:22
  • 1
    @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. – codepleb Apr 12 '17 at 07:29
  • 1
    It 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. – chinnychinchin Nov 10 '17 at 20:46
  • 1
    Using 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? – Adam Cox Nov 16 '17 at 17:34
  • Thanks, `Shift + Alt + A` what i've been looking for – mr.boris Sep 15 '20 at 13:23
  • 1
    In some keyboards, its ctrl + ç – Juan Antonio Tubío Feb 28 '21 at 19:11
  • On a German keyboard for me, I used `cmd + shift + /` and it worked. – Gidi9 Aug 05 '21 at 14:18
206

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

Eduard
  • 8,437
  • 10
  • 42
  • 64
111

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

nbro
  • 15,395
  • 32
  • 113
  • 196
Abdu Rahiman
  • 1,349
  • 2
  • 9
  • 10
  • 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. – hraban Feb 05 '18 at 14:32
  • How can I change this shortcut, I can't seem to find it in File > Preferences > Keyboard Shorcuts? – sianipard Aug 06 '18 at 04:44
  • 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 – Saba Ahang Feb 05 '19 at 13:42
55

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

nbro
  • 15,395
  • 32
  • 113
  • 196
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85
42

You can see all available keybindings on the official documentation.

Here's the relevant bit for osx:

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, then execute above shortcut, i.e. ⌘/ on osx Ctrl/ on Windows.

jottr
  • 3,256
  • 3
  • 29
  • 35
32

In MacOS comment out multi-lines in Visual Studio Code by keyboard shortcut:

Option + Shift + A

/*
...
...
*/

Or,

Select all lines and then press CTRL + /

Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
28

Try option 1 first. If it doesn't work for you then try option 2 which is the actual shortcut for VS 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 File > Preferences > Keyboard 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.

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

im_tsm
  • 1,965
  • 17
  • 29
21

For me to comment I use:

Ctrl + k, Ctrl + c

and to uncomment:

Ctrl + k, Ctrl + u

I am using ubuntu 18.0.4

Yacine Rouizi
  • 1,360
  • 2
  • 15
  • 31
19

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

//
//
//

And Shift + Alt + A for:

/*
...
*/
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Sumpfkraut
  • 311
  • 2
  • 4
17

To comment multiple line on visual code use

shift+alt+a

To comment single line use

ctrl + /

ROOT
  • 11,363
  • 5
  • 30
  • 45
Karthiga
  • 859
  • 2
  • 17
  • 33
14
  1. Select all line you want comments

  2. CTRL + /

ROOT
  • 11,363
  • 5
  • 30
  • 45
Rakesh
  • 2,732
  • 29
  • 28
  • 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. –  Jun 18 '17 at 04:11
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+/
user3636989
  • 161
  • 1
  • 9
10

Win10 with French / English Keyboard CTRL + / , ctrl+k+u and ctrl+k+l don't work.

Here's how it works:

/* */ SHIFT + ALT + A

// CTRL + É

É key is next to right Shift.

Community
  • 1
  • 1
ArsTV
  • 321
  • 3
  • 5
9

CTRL + SHIFT + A for CSS multiline

  • Ubuntu 16
  • VSCode Version 1.17.2
ROOT
  • 11,363
  • 5
  • 30
  • 45
charlyRoot
  • 161
  • 1
  • 9
9

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

enter image description here

Camilo
  • 6,504
  • 4
  • 39
  • 60
8

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

Ctrl + /

7

visual studio 2017 we do a: Comment Selection

Ctrl+K, Ctrl+C

press Ctrl+K to get shortcut. press Ctrl+C to confirm http://visualstudioshortcuts.com/2017/

ROOT
  • 11,363
  • 5
  • 30
  • 45
Joao Victor
  • 509
  • 5
  • 10
7

In the new version of VSCODE ( version 1.26.1)

VSCODE Version

  1. Go to File > preferences > Keyboard Shortcuts
  2. Search for comment Double click the existing shortcut 'keybinding`
  3. And press the new keyboard shortcut in my case ctrl + shfit + /

You can assign any shortcuts you want. Hope it helps.

enter image description here

ROOT
  • 11,363
  • 5
  • 30
  • 45
Ragavan Rajan
  • 4,171
  • 1
  • 25
  • 43
7

If you are using VSCode 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)

Hope this helps.

ROOT
  • 11,363
  • 5
  • 30
  • 45
Sabbir Ahmed
  • 1,468
  • 2
  • 16
  • 21
6

You can find the shortcut in the Edit menu :

Edit > Toggle Block Comment => Shift-Alt-A

Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
5

For me it was ctrl + ' (I am using a swedish layout for the keyboard). Hope that helps anyone.

ROOT
  • 11,363
  • 5
  • 30
  • 45
martin36
  • 2,253
  • 4
  • 18
  • 27
5

For doing on mac: CMD + Shift + 7

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
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

Tanmoy Bhowmick
  • 1,305
  • 15
  • 20
4

CTRL + / comment / uncomment multiple lines of code

Ctrl+k+u for upper case latter

ctrl+k+l for lower case latter

ROOT
  • 11,363
  • 5
  • 30
  • 45
Aashish
  • 41
  • 1
4

In windows you need to press ctrl + / in mac you can do + / to make multiple comment

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

Then if you want to un-comment then select the lines again and then press ctrl + /(in windows) again + /(in mac).

ROOT
  • 11,363
  • 5
  • 30
  • 45
Prashant Barve
  • 4,105
  • 2
  • 33
  • 42
4

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

andz
  • 274
  • 1
  • 3
  • 12
3

1.Select the text, Press Cntl + K, C to comment (Ctr+E+C ) 2.Move the cursor to the first line after the delimiter // and before the Code text. 3.Press Alt + Shift and use arrow keys to make selection. ... 4.Once the selection is done, press space bar to enter a single space.

meta alpha
  • 31
  • 2
3

CTRL + SHIFT + A For Red Hat,centos

  • Select item
  • then CTRL+SHIFT+A
ROOT
  • 11,363
  • 5
  • 30
  • 45
RAHUL KUMAR
  • 1,123
  • 11
  • 9
3

For windows, the default key for multi-line comment is Alt + Shift + A

For windows, the default key for single line comment is Ctrl + /

ROOT
  • 11,363
  • 5
  • 30
  • 45
Anoop P S
  • 754
  • 1
  • 12
  • 31
3

Multi-Line Comment in VS 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

ROOT
  • 11,363
  • 5
  • 30
  • 45
3

Keyboard shortcuts for VSCode on MacOS: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf

I use ⌘/ to toggle commenting.

3

To change the setting on VS2019:

Tools->Options

Search for Keyboard

Under Show Commands Containing search for ToggleLineComment

See what it is set to or change to whatever you like.

VERY IMPORTANT: sometimes it is required to select TextEditor and not Global. this killed me for hours.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
2

You can view the existing shortcuts using the same steps as below. I find the default ones to be inconvenient, so I used the following process to change the IDE to use the '/' command seen in the comments.

You can customize the keyboard shortcuts by going int the "Tools" menu and selecting "Options". Then select "Keyboard" from the "Environment" branch. From there you can bind the Edit.CommentSelection and Edit.UncommentSelection commands to whichever keyboard shortcuts you'd like.

Andy
  • 21
  • 4
2

In Windows

Select the lines you want to comment. Then press Ctrl + /

ROOT
  • 11,363
  • 5
  • 30
  • 45
1

on Windows 10, Italian Keyboard, VSC 1.19.1:

Select lines that you want comment and press "Ctrl + ù"

0

This is somewhat of an extension to the answer when the comment line is too long to fit on a line (above 80 chars or whatever). If the comment is too long and text needs to wrap, it's better to keep it under control (rather than use the editor's text wrap feature). This plugin Rewrap helps do just that https://marketplace.visualstudio.com/items?itemName=stkb.rewrap&ssr=false#review-details

Install the plugin in VS Code, select the text comment, comment it using one of the right methods described in the answers (Ctrl + / is easiest) and then once it's commented, press Alt + Q and this will split the text to multiple lines and also comment it. Found it pretty useful. Hope this helps someone :)

Ravi R
  • 1,692
  • 11
  • 16
0

If you want to use a custom shortcut here is the command name: Toggle Block Comment

enter image description here

Mr world wide
  • 4,696
  • 7
  • 43
  • 97
0

for add cltr + ù to comment , go to shortcut and search ctrl + shift + 7 (default comment enabled ) or search comment , right-click on shortcut > add shortcut key [ctrl + k ctrl + a ] ( is equivalent to aggiungi tasto di scelta rapida [ctrl + k ctrl+a ] ) , press the keys : ctrl + ù > enter .

should add a new line like this ( line above the one clicked ). vs code settings shorcut

salvo720
  • 166
  • 1
  • 4
0

For powershell it's CTRL + #. So basically, select your lines and use a combination of CTRL + the sign to comment of the code. PWS is #, C# is /.. and so on.

iAlucard
  • 21
  • 5
0

To define shortkey Ctrl + Alt + / to mutiplelines comment

Add this to your keybindings.json :

{
    "key": "ctrl+alt+oem_2",
    "command": "editor.action.blockComment",
    "when": "editorTextFocus && !editorReadonly"
},
anesboz
  • 412
  • 4
  • 10
0

On my version of visual studio code and windows it is to insert """ in the following way

"""This is the code i want to comment out
over several lines
I want to comment it out
 """

or also the shourtcut alt+shift+a

PV8
  • 5,799
  • 7
  • 43
  • 87