Visual Studio C++ Multiline comments

21,529

Solution 1

You have to select the whole line (i.e. from the very first character of the line) in order to use c++ comments for multiple lines too.

Update: if there are comments among the selected lines, Ctrl+K,Ctrl+C will generate C++ style comments even if the selection does not start from the beginning of the lines.

Solution 2

Triple click on the first line and while keeping the mouse button pressed drag to the bottom (end) line. after that you have easy select the whole lines and pressing the Ctrl+K, Ctrl+C will comment all those lines with "//" in front.

Solution 3

If you select a block of code and use the key sequence Ctrl+K+C, you'll comment out the section of code. Ctrl+K+U will uncomment the code.

Share:
21,529
fhnaseer
Author by

fhnaseer

C#, WPF, MVVM, Entity Framework, WCF, TDD, Unit-Testing, Agile, Scrum,

Updated on July 05, 2022

Comments

  • fhnaseer
    fhnaseer almost 2 years

    In VS C++ code, if i haven't selected anything or full line selected and press comment selection (Ctrl+K + Ctrl+C) then it will comment the whole line with //

    int x = 5;
    

    After Pressing Ctrl+K + Ctrl+C without anything selected or full line selected.

    // int x = 5;
    

    Now if I select some part of the line and press comments button again only selected text will be commented (bold means selected)

    int x = 5;

    After Pressing Ctrl+K + Ctrl+C with x = 5 selected.

    int /*x = 5*/;
    

    Incase of multiple lines

    int x = 5;

    int y = 2;

    int z = x * 5;

    And after comments shortcut

    int/* x = 5;
    int y = 2;
    int z =*/ x * 5;
    

    What I want

    //int x = 5;
    //int y = 2;
    //int z = x * y;
    

    Now this is what I don't like. Usually I select multiple lines and press comments button. This will comment only the selected characters, but I want all selected lines tobe commented. Is there anyway to do that any extension or from visual studio settings I can change that?

  • Arne Mertz
    Arne Mertz about 11 years
    It's easy to do for mouse-lazy guys like me: Pos1, Shift+Up/Down to select multiple lines and Ctrl+K-Ctrl+C - done :-) After a while it becomes the "natural" way to comment out mutliple lines :-)
  • fhnaseer
    fhnaseer about 11 years
    @lesliel I want to do it without selecting whole line.
  • LazerSharks
    LazerSharks almost 9 years
    I wish there was a way that CTRL+K, CTRL+C would create a block comment even if the selection has inline comments.