Okay, let's be honest. If you're coding in Visual Studio 2019, figuring out how to comment out multiple lines in Visual Studio 2019 is probably one of the first things you needed to learn. It's fundamental, right up there with saving your files. But hey, maybe you're new, or maybe you've been using the mouse way too long and know there's gotta be a faster keyboard way. Or perhaps you hit that shortcut once and it did something weird, and you never tried again. Whatever brought you here, I promise we'll sort it out properly.
I remember back when I switched from another IDE years ago, the comment shortcuts drove me nuts for a week. I kept accidentally collapsing regions instead! It took some trial and error (and a bit of googling, honestly) to get fluent. Now, it's second nature, but I see teammates struggle with it all the time. This isn't just about knowing one keyboard combo; it's about knowing the *right* way for what you're doing, and the gotchas that can trip you up. Let's dig in.
The Absolute Basics: Your Go-To Shortcut (The One You Need Daily)
Visual Studio 2019, like its predecessors, relies heavily on keyboard shortcuts. For commenting multiple lines, there's one king:
METHOD The Keyboard Shortcut Way:
- Select the Lines: Highlight the exact lines of code you want to turn into comments. Click and drag, or use Shift+Down Arrow/Up Arrow. Got a huge block? Click the start, hold Shift, click the end. Easy.
- Execute the Magic: Press
Ctrl + K
, immediately followed byCtrl + C
.- Do NOT press them together. It's a sequence: Hold Ctrl, press K, release both, then hold Ctrl, press C.
That's it. Visual Studio slaps the single-line comment character for your current language (like //
for C#/JavaScript, '
for VB, #
for Python, //
or /* */
depending on context for C++) onto the start of every single line you selected.
What this looks like:
// if (someCondition) // { // DoSomething(); // DoSomethingElse(); // }
Notice *every* line gets its own //
. That's the hallmark of this method.
Pro Tip: Uncommenting is just as easy! Select the commented lines and press Ctrl + K
then Ctrl + U
. Poof, comments gone.
Now, here's where some folks get stuck. They press Ctrl+K, C together like a chord on a piano, and nothing happens, or maybe the "Go to All" search bar pops up (because Ctrl+K alone activates that). You gotta do the two-step: Ctrl+K *release*, then Ctrl+C. Practice it a few times.
What About Block Comments? (/* ... */)
Hold on, you might be thinking. What if I want those fancy block comments that wrap around a whole section, like /* ... */
? Especially common in C, C++, CSS, or sometimes JavaScript.
Visual Studio 2019 doesn't have a single dedicated *built-in* shortcut for toggling multi-line block comments across languages out of the box like it does for single-line comments. This trips people up constantly when searching for how to comment out multiple lines in Visual Studio 2019 expecting one shortcut to rule them all.
Option 1: The Manual Wrap (Works Everywhere)
- Select your lines.
- Type
/*
at the very beginning of the first line you want commented. - Type
*/
at the very end of the last line you want commented.
/* if (someCondition) { DoSomething(); DoSomethingElse(); } */
Simple, reliable, but feels a bit manual compared to the shortcut bliss.
Option 2: The (Sometimes Available) Edit.CommentSelection Command
Buried within Visual Studio is a command called Edit.CommentSelection
. Its behavior can vary depending on the context and language:
- In C#/VB: This command usually does the single-line comment thing (
Ctrl+K, Ctrl+C
is mapped to it). - In CSS/HTML/Some C++ Contexts: This command sometimes uses block comments (
/* ... */
or<!-- ... -->
) when you invoke it on a multi-line selection.
How to try it:
- Select your lines.
- Go to the menu:
Edit
>Advanced
>Comment Selection
.
Or, better yet:
- Open Tools > Options > Environment > Keyboard.
- Type "commentselection" in the search box.
- Select
Edit.CommentSelection
. - Click in the "Press shortcut keys" box and press your desired NEW combo (e.g., maybe
Ctrl + /
like many other editors?). - Click "Assign".
I actually mapped mine to Ctrl + /
years ago because muscle memory from other editors. Works great for single-line in C#, and often does block comments in CSS/HTML. Worth investigating for your primary language. But test it! It's not perfectly consistent.
Option 3: Extensions (The Power User Fix)
If you live and breathe in block comments, or just want consistent, powerful commenting across all files, extensions are the answer. My personal favorite for years has been:
- Comment Remover: Despite its name, it adds superb commenting features too. Lets you easily choose between line comments and block comments regardless of language with configurable shortcuts. Free and lightweight. (Find it via Tools > Extensions and Updates > Online).
Honestly, for heavy multi-language work, an extension saves so much friction. It plugs that gap Visual Studio leaves open.
Beyond the Basics: Tips, Tricks & Annoyances
Okay, you know the shortcuts now. But using them effectively? That's where the real juice is. Here are things I've learned (often the hard way) over countless hours in VS2019.
Selection Matters (A Lot)
- Whole Lines vs Partial Lines: The
Ctrl+K, Ctrl+C
shortcut works on whole lines. If you only select part of lines 2, 3, and 4 (like from the middle of line 2 to the middle of line 4), pressing the shortcut will comment the ENTIRE lines 2, 3, and 4. This is usually what you want, but be aware! - Collapsed Regions: Trying to comment code inside a collapsed region (like a collapsed method or
#region
)? Expand it first! Commenting while collapsed can lead to unpredictable results or only commenting the region header line.
The XML Comment Trap (Triple Slash Madness ///)
Be careful around existing XML doc comments (///
). If you select a block that includes them and use Ctrl+K, Ctrl+C
, VS might add another set of //
, turning ///
into ////
, which breaks the documentation. Annoying!
//// <summary> //// This gets messed up. //// </summary>
Solution: Either uncomment carefully line-by-line afterwards, or use block comments temporarily around the entire section including the XML docs if you really need to disable it all.
Commenting for Debugging vs. Permanent Removal
This is more workflow than mechanics, but it's crucial:
- Temporary (Debugging): Using
Ctrl+K, Ctrl+C
andCtrl+K, Ctrl+U
is perfect for quickly disabling chunks during testing. It's reversible and fast. - Permanent Removal: If you're genuinely removing old code, don't just comment it out forever. That leads to messy, confusing codebases ("comment graveyards"). Use Source Control (like Git)! Comment it out temporarily if you need a reference while writing the replacement, but once the new code works, delete the old commented block and commit. Your future self (and teammates) will thank you. Seriously, this is one of my biggest pet peeves in legacy code.
What If My Shortcut Isn't Working?!
Panic? Nah. Happens. Here's the checklist I run through:
- Keyboard Layout: Using a non-US keyboard? The physical keys might be mapped differently. Ensure your Windows OS keyboard layout matches what VS expects for shortcuts.
- VS Keyboard Scheme: Did someone (or some extension) change your keyboard mapping scheme? Go to Tools > Options > Environment > Keyboard. Check the "Apply the following keyboard mapping scheme" dropdown. "Visual Studio Code" or "C# (or VB)" are common culprits if shortcuts change. Switch it back to "Visual Studio" (or your preferred scheme) and see if it fixes it. You can also look up the
Edit.CommentSelection
andEdit.UncommentSelection
commands here to see what they're mapped to. - Extensions Conflict: Got a fancy new extension installed recently? Some (especially productivity packs) remap keys. Temporarily disable suspicious extensions (Tools > Extensions and Updates > Installed) and restart VS.
- Corrupted Settings: Rare, but possible. Try resetting VS settings: Tools > Import and Export Settings > Reset all settings. Warning: This nukes *all* your customizations! Backup if you have custom keybindings or themes you care about.
Comparing Your Options: How to Comment Out Multiple Lines in VS 2019
Let's break down the main methods side-by-side. This table should help you pick the right tool for the job based on language and need:
Method | How To | Language Suitability | Pros | Cons | Best For |
---|---|---|---|---|---|
Ctrl+K, Ctrl+C / Ctrl+K, Ctrl+U | Select lines, use shortcut sequence. | C#, VB, JavaScript, TypeScript, F#, Python, C++ (Line Comments), PowerShell, SQL, and more. | Fast, built-in, reversible, works on any line selection. | Uses line comments (//), not block (/* */). Can conflict with XML docs (///). | Daily driver for temporary commenting/uncommenting in most languages. |
Manual Block Comment (/* ... */) | Type /* at start, */ at end of selection. | C, C++, CSS, JavaScript (block style), Java, GLSL, etc. | Works everywhere, creates true block comments. | Manual, slower, easy to mismatch delimiters. | Languages requiring block comments, or when line comments are undesirable. |
Edit.CommentSelection Command | Select lines, use menu or custom shortcut. | Varies: Line comments in C#/VB, often block comments in CSS/HTML/C++. | Can be remapped to convenient key (e.g., Ctrl+/). Context-sensitive. | Behavior inconsistent across languages. Not always block comments. | Those wanting a single shortcut mapped to Ctrl+/; Contexts where it uses the desired style. |
Extensions (e.g., Comment Remover) | Install extension, use its shortcuts/menus. | All (Highly customizable). | Most powerful; choose comment style per language; block comment toggle; custom shortcuts. | Requires installing and managing an extension. | Power users, multi-language developers, those needing reliable block comments. |
XML Documentation Comments (///) | Place cursor above element, type /// | Primarily C#, F#, VB | Generates structured doc comments. Intellisense support. | NOT for disabling code. Used for generating documentation. | Documenting classes, methods, properties. |
Region Directives (#region ... #endregion) | Type #region "Name" and #endregion around code. | C#, VB, F# | Collapses code sections for organization. Not a comment. | Code inside is still compiled! NOT for disabling. | Organizing large files, grouping related methods/properties. |
Heads Up: See those last two entries? ///
and #region
? They are NOT methods for commenting out functional code. I see this confusion all the time. XML comments describe code for documentation tools. Regions just hide code visually; it still runs! Using them to disable code is a recipe for bugs and confusion. Stick to true commenting methods (//
, /* */
, '
) to ensure code is actually ignored by the compiler.
Frequently Asked Questions (Stuff Developers Actually Ask)
Based on forums, Stack Overflow, and questions I get personally, here are the common hangups people have when trying to figure out how to comment out multiple lines in Visual Studio 2019:
Why doesn't Ctrl+K, Ctrl+C work on my HTML/Razor file?
Ah, the Razor struggle! HTML and Razor files (.cshtml
, .vbhtml
) in Visual Studio often default to using HTML/XML commenting shortcuts. Try Ctrl+K, Ctrl+C
anyway – sometimes it works (adding <!-- -->
), sometimes it doesn't. The most reliable way:
- Select the lines (HTML, C# code blocks within Razor, whatever).
- Use Edit > Advanced > Comment Selection (or your custom
Edit.CommentSelection
shortcut). This should wrap the selection in<!--
and-->
.
If you do a lot of Razor, an extension like "Comment Remover" is again a lifesaver, giving you consistent commenting for both the HTML and C# parts.
Can I comment multiple lines in Visual Studio 2019 without selecting them first?
Sadly, no. Visual Studio requires you to explicitly select the lines you want to comment using the standard keyboard shortcuts or the built-in commands. There isn't a magic "comment the current function" button. You gotta highlight what needs commenting. Extensions might offer more targeted options, but selection is still the core mechanic.
How do I create a block comment in C#? Doesn't VS support /* */ with a shortcut?
This is arguably a bit of a gap in VS's default setup for C# devs who prefer block comments. Out of the box, no, there isn't a direct shortcut specifically for inserting /* */
around a selection in C#. The Ctrl+K, Ctrl+C
method will always use //
per line.
Your options are:
- Manual Typing: Type
/*
at the start,*/
at the end. - Customize Edit.CommentSelection: As mentioned earlier, try mapping a shortcut to
Edit.CommentSelection
and test its behavior in C#. It *might* sometimes work, but don't count on it consistently using/* */
. - Install an Extension: This is the most reliable solution for block commenting in C# (or any language). Extensions like "Comment Remover" or "Block Comment" add dedicated shortcuts for toggling block comments specifically.
- Snippet (Less Ideal): You could create a code snippet for
/* $selected$ */
, but selecting text and then using a snippet shortcut is slower than a true toggle command.
I messed up and commented too much! How do I uncomment multiple lines quickly?
Don't reach for delete! This is where Ctrl+K, Ctrl+U
shines. Select the lines containing the comments (whether they are //
or within a /* ... */
block), and press that sequence. It should strip the comment characters right off.
Important: For /* ... */
block comments, selecting just the first line with /*
or the last line with */
might not be enough. Select all the lines within the block, including the ones containing the delimiters, then hit Ctrl+K, Ctrl+U
. It usually cleans it up correctly.
Is there a difference between commenting in VS 2019 vs VS 2022?
For the core methods covered here (Ctrl+K, Ctrl+C
, Ctrl+K, Ctrl+U
, manual block comments, using Edit.CommentSelection
), no, there's no significant difference. Microsoft keeps these fundamental shortcuts consistent. Any extensions you use would likely work similarly too. So, if you're learning on 2019, it transfers directly to 2022.
My Ctrl+K, Ctrl+C comments the whole file! What's wrong?
Yikes! Sounds like you pressed Ctrl+A
(Select All) first without realizing it. If your entire document is selected when you hit the comment shortcut, it will indeed try to comment every single line. Press Ctrl+Z
(Undo) immediately to revert it. Then, carefully select ONLY the lines you actually want to comment before using Ctrl+K, Ctrl+C
again.
Wrapping Up: Choose Your Weapon Wisely
So, that's the real deal on how to comment out multiple lines in Visual Studio 2019. It's not rocket science, but knowing the nuances saves time and frustration.
- For 90% of quick, temporary commenting/uncommenting:
Ctrl+K, Ctrl+C
andCtrl+K, Ctrl+U
are your bread and butter. Learn the sequence muscle memory. - For true block comments (/* */ or <!-- -->): Decide if manual typing suffices, or embrace the power of an extension like "Comment Remover" for consistent shortcuts.
- Stuck? Check your keyboard mappings (Tools > Options > Environment > Keyboard) if shortcuts fail. And remember, selection is key!
Ultimately, commenting (and uncommenting) should be a frictionless part of your workflow. Pick the method that fits your style and the language you're in. And please, for the love of clean code, delete those commented blocks once they're obsolete! Your codebase deserves it. Happy (efficient) coding!
Leave a Comments