Message Boards Message Boards


Put parentheses around selection

Posted 7 months ago
5 Replies
17 Total Likes

Hello Suppose I wrote an expression, such as xy. Pressing Ctrl+. , I can select the expression. Now I want to put this expression in parentheses:


This option works for putting a squre-root sign around an expression, by pressing Ctrl+2. What I am looking for is an equivalent shortcut for bracket, or the parentheses, punctuation mark.

5 Replies

I personally do that by adding this command to the file file inside EventTranslations[{

(* Matching parentheses*)
      "(\[SelectionPlaceholder])", Placeholder]]]

However, this is not documented or recommended by Wolfram, and the functionality may not be maintained in future releases. The analogous command for adding [] or {} or "" used to work some years ago but has deteriorated over time. The file is in the directory ~/Library/Mathematica/SystemFiles/FrontEnd/TextResources/Macintosh/ on my system, and was copied there from the parent unmodified file in the main Mathematica directory.

Limitations and problems with .tr files made me create live templates see community post or post.

Once installed you can set package's stylesheet or add templates menu to all notebooks by:

SetOptions[$FrontEnd, NotebookEventActions -> {
     {"MenuCommand", "InsertNewGraphic"} :> Block[{$ContextPath}
        , Needs["DevTools`"]    ; DevTools`OpenNotebookMenu["CodeTemplates"]
      , ParentList

and you are ready to go, with a caret just after the symbol you can hit Ctrl+1 followed by ( and wrapped:

enter image description here

Posted 7 months ago

This is pretty simple to do without modifying any existing settings. After you've selected what you want wrapped in parentheses, just hit Ctrl-6 then Delete (or Backspace). It's not a single shortcut, but I've been using it for years and don't find it cumbersome.

enter image description here

I am pretty sure this was available in Mathematica 2.2, but it was removed from the GUI. This is still available as a FrontEndToken. I have a palette that activates it:

  FrontEndToken[SelectedNotebook[], "SelectionParenthesize"]]]

In modern text editors like Sublime Text and Atom, when you have someting selected and type "(" the selection is parentized instead of replaced. I wish this behabior was adepted by Mathematica (and also for "{" and "[").


Posted 7 months ago

Thanks a lot for all of the comments. I am sure about to adopt one of them.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract