Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 854
dbjohn Avatar asked Apr 09 '11 11:04

dbjohn


People also ask

How do you uncomment in Mathematica?

This can be done by selecting the code and using the Toggle Comment feature of the editor popup (found under the Source choice). In addition, the Add Comment item will place the current selection inside a comment, and the Remove Comment item will remove a comment from around the current cursor position.

How do you uncomment or code out?

Comment Code Block Ctrl+K+C/Ctrl+K+U If you select a block of code and use the key sequence Ctrl+K+C, you'll comment out the section of code. Ctrl+K+U will uncomment the code.

What is shortcut key for past comment?

Select the text you want to copy and press Ctrl+C. Place your cursor where you want to paste the copied text and press Ctrl+V.


1 Answers

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.

like image 119
WReach Avatar answered Nov 02 '22 05:11

WReach