ChatGPT解决这个技术问题 Extra ChatGPT

How to comment multiple lines in Visual Studio Code?

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?

How to comment multiple lines when one of the line is already commented html

N
Nomnom

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.


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.
Isn't there a way to add these to editor context menu? I'd like to add comment/uncomment commands to the context menu.
CTRL + / works great for any commenting with the Vim emulator extension.
R
ROOT

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


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.
E
Eduard

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


comment on remapping the settings stackoverflow.com/a/62676435/913098
[Shift + Option + A] is correct answer. Thanks.
n
nbro

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


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
n
nbro

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


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, shift+alt+A was not working for me!
j
jottr

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.


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
S
Sajib Khan

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

Option + Shift + A

/*
...
...
*/

Or,

Select all lines and then press CTRL + /


Thank you! This is MULTILINE commenting, while other suggest SINGLE LINE commenting for multilines.
how to undo it ?
i
im_tsm

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.


J
J. Chomel

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

//
//
//

And Shift + Alt + A for:

/*
...
*/

Shift+Alt+A for mac OS
R
ROOT

To comment multiple line on visual code use

shift+alt+a

To comment single line use

ctrl + /


Perfect! - [ctrl] + [/] woks best for me to comment out multiple lines.
Y
Yacine Rouizi

For me to comment I use:

Ctrl + k, Ctrl + c

and to uncomment:

Ctrl + k, Ctrl + u

I am using ubuntu 18.0.4


R
ROOT

Select all line you want comments CTRL + /


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.
C
Community

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.


u
user3636989

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+/


R
ROOT

CTRL + SHIFT + A for CSS multiline

Ubuntu 16

VSCode Version 1.17.2


R
ROOT

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.


R
ROOT

In the new version of VSCODE ( version 1.26.1)

https://i.stack.imgur.com/cc40X.png

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

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

https://i.stack.imgur.com/3ZDQ0.png


S
Sylvain Rodrigue

You can find the shortcut in the Edit menu :

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


R
ROOT

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/


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
J
Jaydeep Dholakia

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

Ctrl + /


C
Camilo

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

https://i.stack.imgur.com/ZOvG2.gif


S
Stephen Rauch

For doing on mac: CMD + Shift + 7


R
ROOT

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


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

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).


T
Tanmoy Bhowmick

https://i.stack.imgur.com/JLTtJ.png

Select lines which you want to Comment

Then press Ctrl + / to make selected lines comment

https://i.stack.imgur.com/ty9wK.png

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


m
meta alpha

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.


R
ROOT

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

https://i.stack.imgur.com/fRX2a.png


R
ROOT

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

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


R
ROOT

CTRL + SHIFT + A For Red Hat,centos

Select item

then CTRL+SHIFT+A


R
ROOT

CTRL + / comment / uncomment multiple lines of code

Ctrl+k+u for upper case latter

ctrl+k+l for lower case latter