jbyrd
jbyrd

Reputation: 5595

Visual Studio "Toggle Line Comment" Not Adding // To Already Commented Out Code

I want to comment out a block of Scss code with single line comments (multiline comment will not work b/c I need it to not be processed). In most editors you can select a block of code, and then use a shortcut to simply add // to the beginning of every line. The problem is that in Visual Studio 19 (version 16.7.7), the "Toggle Line Comment" (ctrl+k, ctrl+/) tries to be smart and does not add an extra // to the beginning of a line that already begins with a comment. That's a problem b/c when I toggle the comments off, it then removes comments that were originally there.

This seems really silly that it works this way. Is there some setting or way to change this behavior?

Upvotes: 1

Views: 789

Answers (2)

Nenad Tesovic
Nenad Tesovic

Reputation: 11

The shortcut you need is Ctrl+Shift+/

It comments selected lines by adding // before the first character. Toggles back if pressed again.

Upvotes: 0

Sergey Vlasov
Sergey Vlasov

Reputation: 27910

You can use the following command with my Visual Commander extension to add "//" to every selected line:

public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
    EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
    EnvDTE.TextDocument doc = DTE.ActiveDocument.Object("TextDocument") as EnvDTE.TextDocument;
    EnvDTE.EditPoint p = doc.CreateEditPoint();
    for (int i = ts.TopLine; i <= ts.BottomLine; ++i)
    {
        p.MoveToLineAndOffset(i, 1);
        p.Insert("//");
    }
}

Upvotes: 1

Related Questions