# Put parentheses around selection

Posted 7 months ago
1504 Views
|
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: (xy) 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
Sort By:
Posted 7 months ago
 I personally do that by adding this command to the file KeyEventTranslations.tr file inside EventTranslations[{ (* Matching parentheses*) Item[KeyEvent["9",Modifiers->{Command,Shift}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "(\[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.
Posted 7 months ago
 Limitations and problems with .tr files made me create live templates see community post or mm.se 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"] ; DevToolsOpenNotebookMenu["CodeTemplates"] ] , ParentList } ] and you are ready to go, with a caret just after the symbol you can hit Ctrl+1 followed by ( and wrapped:
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.
 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: Button["(\[SelectionPlaceholder])", FrontEndExecute[ 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 "[").