16
$\begingroup$

This counts as something of a basic question, but one for which I cannot find an answer. I checked the documentation, I checked google, I searched it here, I read a bunch of different things, but nobody seems to have an answer.

Basically, I'm very lazy, and I rarely feel like dealing with wrapping things in brackets if it can be avoided. Things like @ and // are very useful, but there are times when things just must be put in suitable brackets, which can get annoying when working around already written code. If only one could highlight the text and press a keyboard command and suddenly have that text wrapped in brackets...

But wait! In Basic Math Assistant there is a button that does exactly this! It's just quite cumbersome to use given that you need to have that assistant open and click on it, you might as well just do it by hand. Is there any way to get something like this to be done via a keyboard command or something similar?

$\endgroup$
14
$\begingroup$

Here is the answer given by Rolf Mertig that includes many additional shortcuts.

One of them Ctrl+t, evaluates whatever is stored in joker.m file (invoked by Ctrl+Shift+J).

There are already two examples, coincidentally the second one is exactly what you need:

NotebookApply[SelectedNotebook[], RowBox[{"\"", "\[SelectionPlaceholder]", "\""}], Before]

Ok, so briefly:

  • Get["http://www.mertig.com/shortcuts.m"]
  • reset Mathematica
  • use Ctrl+Shift+J to open joker.m file
  • uncomment the last commented part of script (in this case but in general just put whatever procedure you are after)
  • save,Ctrl+S, and close file.

Now, whenever you use Ctrl+T it will wrap selected part of code in []. You can modify joker.m to adjust it for your needs ofc.

$\endgroup$
  • $\begingroup$ correction: the joker.m file is opened using Ctrl + Shift + J. $\endgroup$ – andandandand Dec 2 '15 at 18:57
  • $\begingroup$ @andandandand Thanks! It was changed in the newest version. $\endgroup$ – Kuba Dec 2 '15 at 19:22
  • $\begingroup$ On MacOs, the joker shortcut is mapped to control-d? In any case, I can't get the code in joker.m to run for me. I've commented it out, restarted Mathematica 11, used Needs["Shortcuts`"], verified that the new shortcuts in user: KeyEventsTranslations.tr are working. Am I doing something wrong? As a last resort, I could hard code it in KeyEvents.. but this seems to defeat the purpose. $\endgroup$ – Craig Carter Jan 2 at 17:18
  • $\begingroup$ @CraigCarter what does your joker contain now (except commets)? What if you put there Print[1], save, and invoke it with cmd+d? I don't work on Mac but I haven't heard about any problems with respec to Shortucts` on it. $\endgroup$ – Kuba Jan 2 at 17:34
  • $\begingroup$ @Kuba: Right now it contains only: NotebookApply[SelectedNotebook[], RowBox[{"[", "[SelectionPlaceholder]", "]"}], Before] . Upon changing it Print[1], Control+d just deletes the selection. (I like this code BTW: nice timesaver). It looks like the Os is grabbing the control-d before passed down to the FrontEnd? $\endgroup$ – Craig Carter Jan 2 at 18:13
4
$\begingroup$

Long comment:

You can see the code used for the button by right clicking the Basic Math Assistant and press "Generate Notebook". Then the Assistant appears in a cell in a new notebook. Now right click that cell and select Style -> Input. Add //HoldForm//FullForm to the end of the cell and evaluate it to see the code for the Assistant.

The fact that the button has a tooltip makes it easy to find the relevant code by searching for the tooltip string.

Tooltip[Button[Row[List["(\[VeryThinSpace]", "\[SelectionPlaceholder]", ")"]],
 Inherited, Rule[BaseStyle, "sbcS"], Rule[ButtonData, "(\[SelectionPlaceholder])"],
  Rule[ImageSize, List[26, 18]]], Row[List["Matching parentheses", "\n",
   "Use only for algebraic grouping."]], Rule[TooltipDelay, 0.5`], 
     Rule[TooltipStyle, "TextStyling"]]

Which boils down to

Button["(\[SelectionPlaceholder])", Inherited, ButtonData -> "(\[SelectionPlaceholder])"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.