17
$\begingroup$

Not exactly the world's most pressing problem, but one that has now exceeded my tolerance level. If I comment out a line of input such as

f[x_]:=x+1;

with Alt-/ (Windows), I get

(*f[x_]:=x+1;*)

But, as a believer in typographical as well as programmatic neatness what I really want is...

(* f[x_]:=x+1; *)

Just a space after the opening and before the closing...

How can that be done? Or would it spoil some vast eternal plan...?

$\endgroup$
  • $\begingroup$ Hi Julian, maybe it's too simple what I want to ask, but how do you (un)comment with the keyboard? "Alt+/" doesn't seem to work... which keys do you press exactly? $\endgroup$ – An old man in the sea. Jan 7 '19 at 9:56
  • $\begingroup$ @Anoldmaninthesea.Alt+/ has always worked for me (MMA 9-11.0.1.0, Windows) but care must be taken to ensure that the opening and closing brackets of (* *) are the 1st and last characters of the selection before typing Alt+/, otherwise MMA will not recognise the selection as a comment; I often end up re-commenting comments rather than uncommenting them for this very reason. $\endgroup$ – Julian Moore Jan 8 '19 at 19:05
11
$\begingroup$

New answer:

I updated my package to support not only templates but a menu with arbitrary actions so making an un/comment shortcut should be easier now.

Follow those steps:

  1. Install/update DevTools` v0.10.0+

  2. Actions are available in DevPackageDark.nb stylesheet but you can make them work in any notebook by:

    Needs @ "DevTools`";
       (*Keep in mind it hijacks "Ctrl+," which stands for NewColumn *)
    NotebookActionsEnable[] 
    
  3. Open user actions file with EditNotebookActions[]

  4. Put there a new item:

    <|"Label" -> "Un/Comment", 
      "ShortKey" -> "c", 
      "Action" :> Module[{foo, nb = EvaluationNotebook[]},
        foo[RowBox[{"(*", " ".., content_, " ".., "*)"}]]:=NotebookWrite[
          nb, content, All
        ];
        foo[RowBox[{"(*",  content_, "*)"}]]:=FE`toggleComment[];
        foo[boxes_]:= NotebookWrite[
          nb, RowBox[{"(*", " ",boxes, " ", "*)"}], All
        ];
        foo[$Failed|_CurrentValue]={};  
    
        foo @ CurrentValue[nb, "SelectionData"]
      ]
    
    |>
    
  5. Click 'Save&Test'

  6. You are ready to go with Ctrl+, (NewColumn hijacked) followed by c

enter image description here

OldAnswer:

You can use LiveTemplates Live code templates.

  • Go to a toolbar menu CodeTemplates > Edit User templates
  • Add a new template:

    <|"Label" -> "(* ... *)", "Template" -> "(* `sel` *)", "ShortKey" -> "c"|>
    
  • Save changes

  • Use it with ctrl+1 followed by c

enter image description here


Alternatively you can create a palette with something like this:

Button["c"
, NotebookApply[InputNotebook[]
  , RowBox[{"(*", RowBox[{" ", "\[SelectionPlaceholder]", " "}], "*)"}]
  ]
]

Or try to edit KeyEvenTranslations.tr file. Search around for guidelines but I find this method the least user friendly.

$\endgroup$
  • $\begingroup$ I now have devtools and some interesting extra capabilities to explore. I was reluctant to go that route at first, but I messed up the KeyEventTranslations.tr edits... I'll see if I can work out how to remove this nicer bracket pair using your tools... MMA's "uncomment" of course leaves the extra spaces :) Thank you. $\endgroup$ – Julian Moore Oct 25 '18 at 19:16
  • $\begingroup$ @JulianMoore Yes, it is tricky with .tr files. I will appreciate any feedback you have on DevTools, don't hesitate to create github issues or comment here. I am planning to add a similar feature but only with simple procedures you'd like to keep in a menu. Currently it is dedicated to applying templates so it may be quite tricky to abuse it to 'uncomment'. $\endgroup$ – Kuba Oct 25 '18 at 19:37
  • $\begingroup$ @JulianMoore Please see the edit. $\endgroup$ – Kuba Nov 28 '18 at 7:57
  • $\begingroup$ thx will check it out this w/e $\endgroup$ – Julian Moore Nov 30 '18 at 12:10
  • 1
    $\begingroup$ Is it possible to use DevTools without having to use the dark mode you implement? (Contrary to what many programmers seem to prefer, these old eyes of mine find black text on a white, or light, background, much easier to use.) $\endgroup$ – murray Nov 30 '18 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.