14

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?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • `This will comment only selected, but I want that all selected lines should be commented.` Could you rephrase that, please? You want X but you got only X makes no sense to me. To make your problem clear, you could refer to the `/**/` - comments as C-comments, the others are C++-comments – Arne Mertz Feb 14 '13 at 10:30
  • Ah, I got it - it comments only the selected *characters*, if you did not select complete lines but you want the whole *lines* to be commented. – Arne Mertz Feb 14 '13 at 10:34
  • Yes, I want complete lines even if I have selected some characters of that line. – fhnaseer Feb 14 '13 at 10:37
  • @ArneMertz The more common way to refer to them is as "one-line comments" and "block comments". This better describes what they do, and is especially useful since one-line comments have been available in C since C99. That's 14 years, now. – Agentlien Feb 14 '13 at 10:41
  • looked around a bit - this is a duplicate of (part of) http://stackoverflow.com/questions/4350744/visual-studio-c-toggle-comment-comment-while-not-whole-line-is-selected - seems there is no solution apart of selecting whole lines. – Arne Mertz Feb 14 '13 at 10:42
  • @Agentlien I suspected that the one-line comments had been added in C99. However, I have seen the term "C++-comment" rather often for that style - and since MSVC does not support C99 I guess that term is somewhat valid in MSVC. Vut after all, it's a matter of taste :-) – Arne Mertz Feb 14 '13 at 10:46
  • @ArneMertz Well, it isn't exactly the most important thing to debate. The important thing is that there's no confusion. What I find really absurd is that Visual C++ still doesn't support C99.. – Agentlien Feb 14 '13 at 10:49
  • Note that you can also add your macros to customize these features. – thang Feb 14 '13 at 10:58
  • @Agentlien apparently the MS guys don't mind focusing on C any more since they are too engaged in C++, .NET and so on. – Arne Mertz Feb 14 '13 at 11:03
  • @ArneMertz Which I would think was fine, if their C++11 support was at least on par with most competitors. Which it isn't. It is getting frustrating that there are still many things I can do at in both Clang and GCC which I can't use in production code using Visual Studio. – Agentlien Feb 14 '13 at 11:07
  • We can do block commenting in c# why not in c++. I think there will be some registry key which handles this. – fhnaseer Feb 14 '13 at 12:36
  • @ArneMertz: yes it's partly a dup of mine - and it is extremely annoying when one comes from (all) other ides :) – Mr_and_Mrs_D May 02 '15 at 17:22
  • how do you uncomment – Script Kitty Jul 02 '18 at 19:59

4 Answers4

11

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.

Ogmios
  • 646
  • 7
  • 12
  • 1
    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 :-) – Arne Mertz Feb 14 '13 at 10:52
  • @lesliel I want to do it without selecting whole line. – fhnaseer Feb 14 '13 at 12:17
  • I wish there was a way that CTRL+K, CTRL+C would create a block comment even if the selection has inline comments. – LazerSharks May 21 '15 at 18:18
3

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.

Bogdan
  • 461
  • 4
  • 10
1

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.

-1

Single Line Comment Shortcut

To achieve more than one single line comment, select the lines and use ctrl + /.

According to ques asked, just use the above shortcut, instead of what you are using to achieve what you want.

Multiline Comment Shortcut

Additionally, instead of such a long shortcut(mentioned in ques). To do multiline comments, select the text and use ctrl+shift+a.

Same shortcuts are used to uncomment.

Optider
  • 550
  • 4
  • 11