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

走远了吗. 提交于 2019-11-30 02:11:28

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.

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.

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!