Selecting text in brackets in Mathematica

Is there a way to select some text in a Mathematica notebook and then copy the selection into brackets?

For example, if I typed in the following notepad:

1, 2, 3, 4 

I want to be able to select all the text and then enter a command to insert the corresponding curly braces ( alt-} in linux) and it will wrap the selection in curly braces.

 {1, 2, 3, 4} 
+4
source share
1 answer

The following function adds a command that basically does what you requested.

As written, it coordinates the keyboard shortcut Control + U (usually this is the underline). You can change this quite simply. It also adds a Make List item to the Insert menu, but I assume you are just using a keyboard shortcut.

This modification is saved only for the current session, but you can add this function to the initialization file for loading at startup. There are other ways to continuously add functionality, for example, by editing the KeyEventsTranslations file, for example).

After starting the implementation function, it can be executed using Control + U.

 FrontEndExecute[ FrontEnd`AddMenuCommands["DuplicatePreviousOutput", {Delimiter, MenuItem["Make List", FrontEnd`KernelExecute[ nb = SelectedNotebook[]; sel = NotebookRead[nb]; NotebookWrite[nb, Cell[BoxData[RowBox[{"{", sel, "}"}]]]]], MenuKey["u", Modifiers -> {"Control"}], System`MenuEvaluator -> Automatic]}]] 

Typing and selecting: 1, 2, 3, 4

Control + u

{1, 2, 3, 4}

Adding

Here is a version that you could use instead of your MenuSetup modification. It is configured to be activated by pressing the "{" key and completes the selection or aligns the curly braces. Including this in MenuSetup is not so simple; I would do this by calling an external program from MenuSetup using KernelExecute . It would be just as efficient to put code in an init file.

 FrontEndExecute[ FrontEnd`AddMenuCommands[ "DuplicatePreviousOutput", {Delimiter, MenuItem["Make List", FrontEnd`KernelExecute[ nb = SelectedNotebook[]; sel = NotebookRead[nb]; If[sel === {}, FrontEndExecute[FrontEndToken["InsertMatchingBraces"]], NotebookWrite[nb, Cell[BoxData[RowBox[{"{", sel, "}"}]]]]]], MenuKey["{", Modifiers -> {}], System`MenuEvaluator -> Automatic]}]] 
+5
source

All Articles