How to Comment Multiple Lines in Visual Studio

To comment out multiple lines in Visual Studio, select the lines and press Ctrl + K, then Ctrl + C. To uncomment them, select the lines and press Ctrl + K, then Ctrl + U. You can also use Edit > Advanced > Comment Selection, or the comment buttons on the toolbar. In Visual Studio Code, the shortcut is Ctrl + / (Command + / on a Mac).

Visual Studio uses the right comment style for each language, like // for C# and JavaScript or ' for Visual Basic. Here are all the shortcuts, plus block comments and how to change the keys.

Visual Studio comment shortcuts

Action Visual Studio (Windows) Visual Studio Code
Comment selected lines Ctrl + K, Ctrl + C Ctrl + / (toggle)
Uncomment selected lines Ctrl + K, Ctrl + U Ctrl + / (toggle)
Toggle line comment Ctrl + K, Ctrl + / (Visual Studio 2022) Ctrl + /
Toggle block comment Ctrl + Shift + / (Visual Studio 2022) Shift + Alt + A
On a Mac N/A Command + / and Shift + Option + A

How to comment multiple lines in Visual Studio

  1. Select the lines you want to comment. You don’t need to select entire lines. Any part of each line works.
  2. Press Ctrl + K, release, then press Ctrl + C.

Or use the menu: Edit > Advanced > Comment Selection.

Visual Studio with several lines of code selected and Edit, Advanced, Comment Selection highlighted with its Ctrl+K, Ctrl+C shortcut
(1) Select the lines, then (2) choose Comment Selection or press Ctrl+K, Ctrl+C. (Illustration)

Use the toolbar buttons

The Text Editor toolbar has Comment out the selected lines and Uncomment the selected lines buttons. If you don’t see them, go to View > Toolbars > Text Editor.

How to uncomment multiple lines

  1. Select the commented lines.
  2. Press Ctrl + K, then Ctrl + U. Or choose Edit > Advanced > Uncomment Selection.

Block comments

For languages like C#, C++, and JavaScript, you can wrap code in /* and */ to create a block comment. In Visual Studio 2022, select the code and press Ctrl + Shift + / to toggle a block comment. Line comments (//) are usually easier to add and remove.

How to comment in Visual Studio Code

  1. Select the lines.
  2. Press Ctrl + / to comment them. Press it again to uncomment.
  3. For a block comment, press Shift + Alt + A.

You can also use the Edit menu > Toggle Line Comment or Toggle Block Comment.

How to change the comment shortcut

  • Visual Studio: Go to Tools > Options > Environment > Keyboard, search for Edit.CommentSelection, and assign a new shortcut.
  • VS Code: Press Ctrl + K, Ctrl + S to open Keyboard Shortcuts, then search for “comment.”

More questions about commenting code in Visual Studio

Why doesn’t Ctrl + / work on my keyboard?

On some international keyboard layouts, the slash key is in a different place or needs Shift. Change the shortcut in your keyboard settings as shown above.

Does commenting code delete it?

No. Commented code stays in the file but is ignored when the program runs.

When a build crashes outside the editor, turning on Just-In-Time debugging lets Visual Studio open at the failing line.

Can I comment out code in XAML or HTML?

Yes. Visual Studio uses <!-- --> automatically for markup files when you press Ctrl + K, Ctrl + C.