# Add spaces to Mathematica comment delimiters?

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...?

• 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? – An old man in the sea. Jan 7 '19 at 9:56
• @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. – Julian Moore Jan 8 '19 at 19:05

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.

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_, "*)"}]]:=FEtoggleComment[];
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

You can use LiveTemplates Live code templates.

• Go to a toolbar menu CodeTemplates > Edit User templates

<|"Label" -> "(* ... *)", "Template" -> "(* sel *)", "ShortKey" -> "c"|>

• Save changes

• Use it with ctrl+1 followed by c

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.

• 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. – Julian Moore Oct 25 '18 at 19:16
• @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'. – Kuba Oct 25 '18 at 19:37
• @JulianMoore Please see the edit. – Kuba Nov 28 '18 at 7:57
• thx will check it out this w/e – Julian Moore Nov 30 '18 at 12:10
• 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.) – murray Nov 30 '18 at 20:14