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

后端 未结 3 1420
广开言路
广开言路 2020-12-24 15:26

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 expe

相关标签:
3条回答
  • 2020-12-24 15:43

    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.

    0 讨论(0)
  • 2020-12-24 15:44

    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.

    0 讨论(0)
  • 2020-12-24 15:57

    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.
    0 讨论(0)
提交回复
热议问题