14

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

Community
  • 1
  • 1
dbjohn
  • 975
  • 3
  • 15
  • 24

3 Answers3

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.

WReach
  • 18,098
  • 3
  • 49
  • 93
  • 1
    That makes sense. Also, can you explain why this can be done from `MenuSetup.tr` but not `KeyEventTranslations.tr`? – Mr.Wizard Apr 09 '11 at 13:58
  • 2
    +1 This works for me. For anyone else trying it, you will need to close and restart mathematica for the command to be available. – dbjohn Apr 09 '11 at 14:05
  • 2
    @Mr It will work in `KeyEventTranslations.tr`, but you must wrap the command in `ToExpression`, just like in `ContextMenus.tr`. – WReach Apr 09 '11 at 14:35
  • Thanks, I am learning a lot. Can you tell me about `MenuEvaluator -> Automatic, Method -> "Queued"` ? – Mr.Wizard Apr 09 '11 at 14:41
  • 2
    @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. – WReach Apr 09 '11 at 14:51
8

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.
Bruno Dermario
  • 123
  • 2
  • 5
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.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 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? – Phab Jun 24 '15 at 08:06
  • @Phab Yes, in fact it *should* be copied and edited there. See my more recent answers for example: http://mathematica.stackexchange.com/a/880/121, http://mathematica.stackexchange.com/a/57492/121. – Mr.Wizard Jun 24 '15 at 08:18