22

2 questions actually :

1) shortcut to toggle comment on selected lines ? Available on all iDEs I used starting with notepad++

2)the ctrl-k, ctrl-c exhibits this behavior (quoted from someplace nicely worded):

C#: Each line where some text is selected is commented at the line-start with double-slash. If nothing is selected, the line where the cursor is is commented.

C++: If nothing is selected or complete lines are selected, it behaves as above. However, if parts of a line are selected, and no comment is selected as part of the selection (ex. select something in the middle of a code line), then the selection is surrounded by /* and */.

since I code in C++ I find this behavior annoying - I want to be able to comment out lines partially selected - any workarounds ?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • 4
    Well, the workaround is to select the whole line! – Billy ONeal Dec 03 '10 at 23:11
  • 2
    Can I vote down ? Even notepad++ has this behavior and it is really useful - you are in the midle of line with something selected - hit Alt-Q line commented out - I can't believe this is not possible in VS ! – Mr_and_Mrs_D Dec 03 '10 at 23:12
  • 1
    @user: You can't vote it down because it's not actually an answer lol. I didn't indend that to be a serious answer to the question, I more inteded it to be funny. Though I have to say this is a feature I'd never use -- It's almost as fast to press "Home" and then two /s as it would be to press the key combo (In fact it's one fewer keystroke!). – Billy ONeal Dec 03 '10 at 23:15
  • I know - was joking - well a matter of taste - also (more) useful when selecting multiple lines and do not caring to select exactly the whole of first and last - and how about the *toggle comment* part ? – Mr_and_Mrs_D Dec 03 '10 at 23:19
  • Why is toggling comments so important? Is it that you do not have color coding turned on and so it is hard for you to tell whether a line is already commented or not? Just use `Ctrl+K, Ctrl+C` for commenting and `Ctrl+K, Ctrl+U` for uncommenting. I'd much rather have the VS team implement things like code refactoring than fancy commenting shortcuts. – Praetorian Dec 04 '10 at 00:13
  • @Billy: Home and 2 /s is the same number of keystrokes as Ctrl+K, Ctrl+C if you hold the Ctrl key down while hitting K & C ;-) – Praetorian Dec 04 '10 at 00:16
  • 1
    @Praetorian - it is important, dear, when you comment out something, write and alternative below it and you want to *quickly* (without having to select the whole lines, just a quick mouse gesture) switch between the two - *for instance*. Gosh - I asked a question - either answer or not and please spare me the programming tips. And those "fancy" commenting shortcuts are *elementary* and present in all IDEs there are. I do agree that home//==Cntrl+K+C though. – Mr_and_Mrs_D Dec 04 '10 at 10:08
  • Write a macro for the behavior you want, at least until VS2012 where MS took macros out! Guess you'll have to use Notepad++ for that, too. – Mark Tolonen Jan 20 '13 at 08:07
  • @MarkTolonen:lol I gave up a long time ago - when I come back I will be sure to bug you with how to write those macros :) – Mr_and_Mrs_D Jan 20 '13 at 10:27

6 Answers6

9

Each line where some text is selected is commented at the line-start with double-slash. If nothing is selected, the line where the cursor is is commented.

In case of multiline selection: My solution uncomments only if all the lines in the selection are commented. I found it more intuitive.


Solution:

Tools -> Macros -> Macros IDE...

In Macro Explorer right click on Macros and click New Macro Project...

Name your macro for e.g. MyMacroProject and click Add.

Right click on Module1 in your new macro project in Macro Explorer and click Edit.

Paste this into the macro editor window:

Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Text.RegularExpressions

Public Module Module1
    Sub ToggleCommentLine()
        Dim sel As TextSelection = DTE.ActiveDocument.Selection

        Dim firstLine As Integer = sel.TopPoint.Line
        Dim lastLine As Integer = sel.BottomPoint.Line

        sel.GotoLine(firstLine, True)
        sel.LineDown(True, lastLine - firstLine)
        sel.EndOfLine(True)

        'we un-comment only if there is no commented line
        Dim allLinesCommented As Boolean = True

        Dim lineIndex As Integer = firstLine
        While allLinesCommented And (lineIndex <= lastLine)
            sel.GotoLine(lineIndex, True)
            allLinesCommented = Regex.IsMatch(sel.Text, "^\s*//.*$")
            lineIndex += 1
        End While

        'iterate over the lines
        For lineIndex = firstLine To lastLine
            sel.GotoLine(lineIndex, True)
            Dim line As String = sel.Text
            Dim m As Match = Regex.Match(line, "^(\s*)(//)(.*)$")
            If allLinesCommented Then
                sel.Text = m.Groups(1).Value & m.Groups(3).Value
            ElseIf Not m.Success Then
                sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
                sel.Text = "//"
            End If
        Next

        'select all the affected lines
        sel.GotoLine(firstLine, True)
        sel.LineDown(True, lastLine - firstLine)
        sel.EndOfLine(True)
    End Sub
End Module

Save this file and close the macro editor window.

Bind your macro to a key:

Tools -> Options... -> Environment -> Keyboard

Type this into Show commands containing: ToggleCommentLine

Select Macros.MyMacroProject.Module1.ToggleCommentLine.

Set a key at Press shortcut keys: . , then click Assign, then click OK.

Enjoy.

ch0kee
  • 736
  • 4
  • 12
  • Very cool, thank you! I guess the bounty is for you, I'll just wait a few more days to see if anyone else wants to answer. – static_rtti Sep 24 '13 at 07:26
  • Actually I have one remark: you seem to use a C-specific regex to do the work. Wouldn't it be better to use Visual Studio's commenting and uncommenting functions which are far more general and more robust? – static_rtti Sep 25 '13 at 07:21
  • Can you mention any use case when this solution is not general or robust enough ? You would like it to exclude lines from comment when is executed inside a multi line comment (`/* ... */`) ? – ch0kee Sep 25 '13 at 20:18
  • any non-C language, for instance. Haskell, HTML, you name it! – static_rtti Sep 27 '13 at 08:28
  • I know Haskell but I thought we are talking about C++ now :/ – ch0kee Sep 27 '13 at 19:44
  • 1
    @static_rtti: you are right - this solution needs to be made more generic - I would accept it if made more generic and tested by you (I do not have VS installed). ch0kee : "I thought we are talking about C++ now" -->general, _reusable_ code is always better than one shot hack - better rich and healthy than poor and sick as the saying goes. Moreover the question does not talk about toggling comments in C++ - although I was coding in C++ at the time - and everywhere this feature is offered is _offered_ (for all supported languages) ;) – Mr_and_Mrs_D Sep 28 '13 at 12:39
  • I did mention C++ :) The bounty is for you. However, If you or someone else wants to make it more generic, I think it would be a very useful thing. – static_rtti Sep 30 '13 at 07:26
  • See Tools->Option->Encironment->Keyboard – betontalpfa Mar 30 '16 at 11:49
2

The behavior is intentional. If the user needed a tiny temporary change to a single line that did not require the entire line to be re-written, using the Ctrl+K, Ctrl+C shortcut pair allows him/her to comment out just the change and not the entire line.

Edit:

As for question one, it's the same shortcut pair: Ctrl+K, Ctrl+C to toggle any comments on, Ctrl+K, Ctrl+U to toggle any comments off.

Edit 2:

If you are still unsatisfied, get Visual Assist X from whole tomato software: http://www.wholetomato.com/ It adds an additional comment shortcut mapping to the '/' and '*' keys when text is highlighted.

Casey
  • 10,297
  • 11
  • 59
  • 88
  • 1
    I do agree it can be useful - it should have another shortcut though -- any ideas about q1 ? – Mr_and_Mrs_D Jan 13 '11 at 01:45
  • Thanks - but toggle comment means to select some lines which are commented and some which are not and *toggle their comment status simultaneously* – Mr_and_Mrs_D Jan 24 '11 at 18:47
0

If you like to change the key shortcut of toggle comments to ctrl+/ key. You can Hot command for visual studio and install it. Once you restart Visual Studio, it works!

https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotCommandsforVisualStudio

imanzabet
  • 2,752
  • 2
  • 26
  • 19
0

In Visual Studio 2019 you can get almost exactly the functionality you are looking for by triple-clicking the first line and then dragging to the bottom line. This will select full lines.

Then use Ctrl-K Ctrl-C as usual. It will insert // and not /**/.

BitPusher16
  • 235
  • 2
  • 12
0

There is an extension that has a more "natural" toggle comment experience. It does not comment blank lines, but it does uncomment a selected block of text with blank lines therein.

Be warned that it's default key binding, Ctrl+/ conficts with a command in VS 2019, so it won't be set upon install, but the documentation shows you how to change it.

https://marketplace.visualstudio.com/items?itemName=munyabe.ToggleComment

kindrobot
  • 1,309
  • 1
  • 10
  • 19
-2

Toggle IS NOT the same as Toggle On and Toggle Off.

If I Toggle a group of lines - some of which are commented out, and others are not, then a TOGGLE would comment out the lines that were previously not commented out and Un-Coment out the the line that were previously commented out - with a SINGLE keystroke.

pg1
  • 9