Keyboard shortcut to Un/Comment out code in Mathematica 7?
Asked Answered
N

3

15

A keyboard shortcut to comment/uncomment out a piece of code is common in other programming IDE's for languages like Java, .Net. I find it a very useful technique when experimenting through trial and error to temporarily comment out and uncomment lines, words and parts of the code to find out what is and isn't working.

I cannot find any such keyboard shortcut on the Mathematica front end in version 7. I know that it is possible to comment out code by selecting the code, right mouse click and select Un/Comment from the menu that appears but this is too slow while coding.

I tried to access this using the menu key Menu on the keyboard but Mathematica frontend doesn't respond to or recognise this key unlike other applications, this could have allowed a key combination for commenting. Can someone else verify that this isn't unique to my machine and that the key isn't recognised by mathematica. I looked at this question and looked in the KeyEventTranslations.tr file but I don't think there is any way to create a shortcut to do this(?). Should I just live with it?

Any other suggestions?

(I have seen there is an Emacs version of mathematica, I have never tried Emacs or this Mma version and imagine that it would have this ability but would prefer not to go to the trouble and uncertainty of installing it. Also I would guess that the Wolfram Workbench could do this, but that may not be worth the investment just for this.)

Noblesse answered 9/4, 2011 at 11:47 Comment(0)
P
15

You can install the shortcut in Mathematica 7.0.x if you are willing to edit the Mathematica system file MenuSetup.tr. You can find it in the same location as the KeyEventTranslations.tr file (i.e. in the installation under "SystemFiles/FrontEnd/TextResources/platform"). In MenuSetup.tr, locate the following line under the definition of the Edit menu:

MenuItem["Check &Balance", "Balance", MenuKey["B", Modifiers->{"Control", "Shift"}]],

Immediately below that line, insert the following:

MenuItem["Un/C&omment Selection", KernelExecute[FE`toggleComment[]], MenuKey["/", Modifiers->{"Command"}], MenuEvaluator -> Automatic, Method -> "Queued"],

The Un/Comment Selection command is now available under the Edit menu, with the keyboard shortcut Cmd-/ or Alt-/ depending on your platform -- just like in Mathematica 8 where this command comes pre-installed.

Please take as read the usual disclaimers about hacking the Mathematica installation files -- no warranty is offered :)

I do not know of any way to map this function to some shortcut involving the Menu key.

Shortcut Key, No Menu

The preceding steps mimic what Mathematica 8 does by installing a new menu item. If you prefer to leave the menus unchanged, then you can install the shortcut in KeyEventTranslations.tr instead. Add the following line:

Item[KeyEvent["/", Modifiers->{Command}], KernelExecute[ToExpression["FE`toggleComment[]"]], MenuEvaluator -> Automatic, Method -> "Queued"]

You'll need a comma on the end of that line if it is not the last item in the list.

Puckery answered 9/4, 2011 at 13:49 Comment(5)
That makes sense. Also, can you explain why this can be done from MenuSetup.tr but not KeyEventTranslations.tr?Barite
+1 This works for me. For anyone else trying it, you will need to close and restart mathematica for the command to be available.Noblesse
@Mr It will work in KeyEventTranslations.tr, but you must wrap the command in ToExpression, just like in ContextMenus.tr.Puckery
Thanks, I am learning a lot. Can you tell me about MenuEvaluator -> Automatic, Method -> "Queued" ?Barite
@Mr MenuEvaluator and Method appear to function like the Evaluator and Method options to Button. Unlike Button, the default evaluator in the context of these mapping files appears to be the front-end so MenuEvaluator -> Automatic appears to push the expression to the kernel. Method -> "Queued" seems to be a defensive measure in case commenting out a large block of code takes a long time. I say "seems" a lot because this is all guesswork.Puckery
E
9

Select the code and press one of the following:

  • Alt+/
  • Alt-E-O
  • Alt+E+O

Notes:

  • Tested only on Windows, using Mathematica 8.
  • On my non-US keyboard (ABNT Brazil), the shortcut Alt+/ doesn't work, instead I use Alt+E+O.
Empurple answered 31/8, 2012 at 21:8 Comment(0)
B
7

Here is code for your KeyEventTranslations.tr file that will comment out code. I am still working on the other half.

Item[KeyEvent["/", Modifiers -> {Command}],
    FrontEndExecute[{
        NotebookApply[FrontEnd`InputNotebook[],
            "(*\[SelectionPlaceholder]*)"
        ]
    }]
], 

This binds it to Alt+/ as it is in Mathematica 8.

Barite answered 9/4, 2011 at 12:58 Comment(2)
Is it possible (or inadvisable) to create this file/direction under "[user dir]\AppData\Roaming\Mathematica\SystemFiles\FrontEnd" so I don't have to change the file for every new version?Ventose
@Ventose Yes, in fact it should be copied and edited there. See my more recent answers for example: mathematica.stackexchange.com/a/880/121, mathematica.stackexchange.com/a/57492/121.Barite

© 2022 - 2024 — McMap. All rights reserved.