How to comment multiple lines in Visual Studio Code?

1,121,483

Solution 1

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.

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.

Share:
1,121,483
gog
Author by

gog

Updated on July 08, 2022

Comments

  • gog
    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
    Mario almost 8 years
    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.
  • Jo Smo
    Jo Smo over 7 years
    On mac, it's cmd+- (cmd and minus).
  • xastor
    xastor over 7 years
    Weird, with me it's cmd+= (macos sierra and vscode 161)
  • Amit Kumar Gupta
    Amit Kumar Gupta over 7 years
    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
    Amit Kumar Gupta over 7 years
    But it doesn't talk about multiline comment.
  • codepleb
    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
    Eran Or about 7 years
    Shift+Alt+A for mac OS
  • Admin
    Admin about 7 years
    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.
  • laishiekai
    laishiekai almost 7 years
    @AmitGupta: ⌘/ Toggle Line Comment <-- this works for multiline too.
  • chinnychinchin
    chinnychinchin over 6 years
    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.
  • Adam Cox
    Adam Cox over 6 years
    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?
  • Firze
    Firze over 6 years
    Same for Finnish layout, which is actually same as Swedish.
  • Eduard
    Eduard over 6 years
    Thank you! This is MULTILINE commenting, while other suggest SINGLE LINE commenting for multilines.
  • hraban
    hraban over 6 years
    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.
  • pythlang
    pythlang about 6 years
    highlight multiple lines and ⌘K then ⌘C or ⌘K then ⌘U will work. ⌘/ also does the same thing. @AmitGupta
  • John Smith
    John Smith almost 6 years
    For 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
    Casey Kuball almost 6 years
    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).
  • sianipard
    sianipard almost 6 years
    How can I change this shortcut, I can't seem to find it in File > Preferences > Keyboard Shorcuts?
  • Ferroao
    Ferroao over 5 years
    in my ubuntu 16 does not work, strange, i can change keys but maybe depends on the file extension?
  • Saba Ahang
    Saba Ahang over 5 years
    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
  • ckkkitty
    ckkkitty over 5 years
    I found the suggestion to click on the settings icon and clicking Keyboard Shortcuts very helpful.
  • Chris Koester
    Chris Koester over 5 years
    You can find the comment options in the Edit menu if you forget the keyboard shortcuts.
  • Avani Khabiya
    Avani Khabiya about 5 years
    ctrl + shift + A worked for me for adding multi-line comment.
  • akinuri
    akinuri almost 5 years
    Isn'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
    Tiago Martins Peres over 4 years
    This one worked fine for me. A nice add-on to this answer is "How to uncomment".
  • tdensmore
    tdensmore over 4 years
    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...
  • Chris Hayes
    Chris Hayes over 4 years
    CTRL + / works great for any commenting with the Vim emulator extension.
  • Yacine Rouizi
    Yacine Rouizi about 4 years
    to uncomment use Ctrl + k, Ctrl + U
  • mr.boris
    mr.boris almost 4 years
    Thanks, Shift + Alt + A what i've been looking for
  • java-addict301
    java-addict301 over 3 years
    finally a shoutout for Linux in the 4th answer! works on 20.04
  • Kamlesh
    Kamlesh over 3 years
    not working for me. What is your version of VS Code editor?
  • Gulzar
    Gulzar over 3 years
    comment on remapping the settings stackoverflow.com/a/62676435/913098
  • Juan Antonio Tubío
    Juan Antonio Tubío over 3 years
    In some keyboards, its ctrl + ç
  • carloswm85
    carloswm85 about 3 years
    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.
  • Robert
    Robert almost 3 years
    Perfect! - [ctrl] + [/] woks best for me to comment out multiple lines.
  • Daniel Martin
    Daniel Martin almost 3 years
    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.
  • Gidi9
    Gidi9 almost 3 years
    On a German keyboard for me, I used cmd + shift + / and it worked.
  • Cyril N.
    Cyril N. almost 3 years
    Works on Fedora too! Thank you for your comment, <kbd>shift</kbd>+<kbd>alt</kbd>+<kbd>A</kbd> was not working for me!
  • Mona Jalal
    Mona Jalal over 2 years
    CTRL+/ worked for me in CentOS 7
  • Ankit Maheshwari
    Ankit Maheshwari over 2 years
    [Shift + Option + A] is correct answer. Thanks.
  • user5249203
    user5249203 over 2 years
    how to undo it ?
  • curios
    curios about 2 years
    The URL for "All VSCode Shortcuts" is for windows. Mac users, the link is: code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf