How to Comment Multiple Lines in Visual Studio
To comment out multiple lines in Visual Studio on Windows, select the lines, then press Ctrl+K followed by Ctrl+C. To uncomment them, press Ctrl+K followed by Ctrl+U. Here’s how to use the keyboard and menu commands, choose the right syntax for your language, and fix a shortcut that does nothing.
How Comments Work in Visual Studio
Comment syntax follows the file’s language, not the Visual Studio window.
Commenting out code makes the compiler or interpreter ignore it without deleting it. A line comment extends to the end of its line; a block comment has opening and closing delimiters and can span multiple lines.
That distinction matters: commenting several selected lines does not necessarily create a single block comment. Visual Studio may instead add a line-comment marker to each selected line, depending on the language and command.
These instructions cover the Visual Studio IDE for Windows. Visual Studio Code is a separate editor with different default shortcuts.
How to Comment Out Multiple Lines in Visual Studio
Choose your action using these Visual Studio default General-profile shortcuts; bindings can differ with your keyboard profile, layout, and language settings.
| What You Want to Do | Visual Studio Action | Default Shortcut |
|---|---|---|
| Comment out selected code | Comment Selection | Ctrl+K, then Ctrl+C |
| Restore commented code | Uncomment Selection | Ctrl+K, then Ctrl+U |
| Add or remove line comments with the same shortcut | Toggle Line Comment | Ctrl+/ in Visual Studio 2022 version 17.11 or later |
Step 1: Select the Code in the Editor
- Open the source file in Visual Studio’s code editor.
- Select the lines you want to disable, using the mouse or holding
Shiftwhile moving the cursor. - Check where the selection starts and ends so it does not include neighboring code you want to keep active.
For whole-line commenting, selecting complete lines makes the intended scope clear. To comment only part of an expression, use a block comment if the language supports one.
Step 2: Apply the Comment Command
- Press
Ctrl+K, release the keys, then pressCtrl+C. This is a two-part key sequence, not a command that requires holding all the keys simultaneously. - Check that Visual Studio has inserted comment markers across the intended selection.
For a mouse-based alternative, choose Edit > Advanced > Comment Selection instead of pressing the shortcut.
If you choose Toggle Line Comment, invoke its shortcut once and inspect the result. For a selection containing a mixture of commented and active lines, the explicit Comment Selection or Uncomment Selection command makes your intended action clearer than a toggle.
Step 3: Check and Save the Change
- Confirm that the first and last intended lines are commented.
- Check that the code immediately after the selection remains active.
- Save the file.
When you want to restore the code, select the commented lines and choose Edit > Advanced > Uncomment Selection. Inspect the restored code and save again; a dedicated uncomment command is more precise than relying on Undo after making other edits.
Multiline Comment Syntax by Language
C# supports // and /* */ comments, while Visual Basic requires a ‘ marker on each commented line.
C# Line and Block Comments
C# supports both line and block comments, per Microsoft’s Visual Studio language-service documentation.
For complete lines, the comment command normally adds line-comment markers:
// var total = price * quantity;
// Console.WriteLine(total);
To manually comment out a continuous region, put /* before it and */ after it:
/*
var total = price * quantity;
Console.WriteLine(total);
*/
A block comment can also occupy part of a line:
var total = price /* before tax */ * quantity;
The important limitation is that C# block comments do not nest. If the selected code already contains a block comment, wrapping another block comment around it can close the outer comment earlier than intended. Use whole-line comments for that selection instead.
After adding a block comment, check that its closing delimiter is in the right place and that subsequent code remains outside the comment, then save the file.
Visual Basic Multiline Comments
In Visual Basic, select one or more lines of code, then choose the Comment button on the Edit toolbar. Choose Uncomment on that toolbar to remove the comment markers from the selection.
Visual Basic requires the comment symbol ' on each line:
' Dim total = price * quantity
' Console.WriteLine(total)
An underscore does not continue a comment onto the next line, under Visual Basic’s line-continuation rules. Leaving the following line without a comment marker can leave it as active code.
Visual Basic also does not use the C#-style block-comment wrapper. Use the editor’s selection command rather than trying to surround several lines with another language’s delimiters.
After commenting or uncommenting, inspect the entire selection and save the file.
When Visual Studio Comment Shortcuts Do Not Work
Check the Active Editor
Start by clicking inside the source-code editor. Comment shortcuts are editor commands; focus in a designer, search box, or tool window can prevent them from acting on your code.
Select the intended lines and try Edit > Advanced > Comment Selection.
- If the menu command works: the likely issue is the keyboard binding, keyboard layout, or an extension intercepting the shortcut. Check the inserted comments and save the file.
- If the command is unavailable: check whether the file is editable and whether its editor supports commenting.
- If comment markers create errors: confirm that they are valid for the file’s language. Standard JSON, for example, does not support comments.
A shortcut cannot make unsupported comment syntax valid.
Verify or Assign the Keyboard Shortcut
Visual Studio’s General-profile defaults are not guaranteed to match an imported profile or customized installation. Check the actual binding before changing settings.
- Open Tools > Options > Environment > Keyboard, then search for the Comment Selection command to inspect or change its shortcut.
- In Show commands containing, search for the comment command:
Edit.CommentSelectionFor the uncomment command, search instead for:
Edit.UncommentSelection - Select the command and inspect Shortcuts for selected command.
- To assign a different binding, choose Text Editor under Use new shortcut in, click Press shortcut keys, and press the shortcut you want.
- Check Shortcut currently used by before assigning it. If another command already uses that binding in the relevant context, choose a different shortcut unless you intend to replace it.
- Select Assign, then OK.
- Return to the source file, select the intended lines, and try the binding. Confirm that it adds or removes the correct comment markers, then save.
If a correctly assigned shortcut still fails while the menu command works, investigate extensions or external keyboard utilities that may be capturing those keys.
Check That Commenting Out Code Has the Intended Effect
Correct comment syntax does not guarantee that the remaining program is valid. For example, disabling a variable declaration while leaving its uses active can introduce errors. Commenting out only part of a conditional or loop can also leave unmatched braces or an incomplete statement.
Before treating the edit as finished:
- Check the remaining code’s structure. Keep braces, parentheses, and language-specific opening and closing statements balanced.
- Look for dependencies on disabled code. Remaining statements may still use variables, functions, or configuration created by the commented section.
- Review the version-control diff. Confirm that the edit disabled only the intended code and did not include unrelated changes.
- Decide whether the change should remain. Restore temporary debugging changes before committing unless disabling the code is intentional.
Save the finished edit, then run your usual build, syntax check, or tests to verify that the remaining code behaves as intended.
Frequently Asked Questions
How Do I Comment Out Multiple Lines in VS Code?
In Visual Studio Code, select the lines and press Ctrl+/ on Windows or Linux, or Cmd+/ on macOS, to toggle line comments. Check that the intended lines are commented, then save the file. To restore them, select the lines, repeat the shortcut, and save again.
How Do I Comment Multiple Lines in a CMake File in Visual Studio 2022?
In CMake, prefix each line with #, or enclose a multiline region between #[[ and ]]. If Visual Studio’s comment action is unavailable in your CMake editor, insert those markers manually. When using a bracket comment, check that the enclosed text does not contain an unintended closing delimiter, then save and configure the project to check for errors.
How Do I Write Comments in C?
In C, /* ... */ creates a block comment that can span multiple lines; // comments out the remainder of a line in C99 and later. C block comments do not nest, so do not wrap an existing block comment inside another one. After adding the comments, check their boundaries, save the file, and rebuild to verify that the remaining code is valid.
