# The Most Convenient Keyboard Shortcuts Ever

Posted 2 years ago
7411 Views
|
15 Replies
|
17 Total Likes
|

Here are two simple but extremely convenient keyboard shortcuts that I've used for years.

• This post is a favor to all my friends who type slowly (like me)
• These shortcuts simply create a pair of brackets containing a template placeholder as the current new selection
• Essentially, it's just a stripped-down version of the bulky (Command + Shift + K + Up/Down Arrow + Enter) completion

Favorite Shortcut #1: Autocomplete single brackets by pressing CommandKey + [

Favorite Shortcut #2: Autocomplete part brackets by pressing CommandKey + ]

## How to set them up

On MacOS, first make a backup of the following file, in case you break it:

FileNameJoin[{$InstallationDirectory, "SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr"}];  On my machine this path resolves to: "/Applications/Mathematica.app/Contents/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr" Then, preferably in a non-Mathematica editor (e.g. Sublime), open the above file and copy-and-paste these following code into the list inside EventTranslations. For the first shortcut: Item[KeyEvent["[",Modifiers->{Command}],FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[],"[\[SelectionPlaceholder]]"],FrontEndToken["MovePreviousPlaceHolder"]}]]  And for the second (of course making sure they are separated by a comma): Item[KeyEvent["]",Modifiers->{Command}],FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[],"\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]"],FrontEndToken["MovePreviousPlaceHolder"],FrontEndToken["MovePreviousPlaceHolder"]}]]  Finally, restart Mathematica, and presto, they should both be working. And to remove the shortcuts, simply remove those items or replace the .tr file with the backup. Answer 15 Replies Sort By: Posted 2 years ago  Sadly, these shortcuts don't work on my Mac. Could that depend on the keyboard layout? I have the following custom key event translations in my file: (* Matching double quotes*) Item[KeyEvent["'",Modifiers->{Command,Shift}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "\"\[SelectionPlaceholder]\"", Placeholder]]], (* Matching curly braces*) Item[KeyEvent["[",Modifiers->{Command,Shift}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "{\[SelectionPlaceholder]}", Placeholder]]], (* Matching square braces*) Item[KeyEvent["[",Modifiers->{Command}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "[\[SelectionPlaceholder]]", Placeholder]]], (* Matching parentheses*) Item[KeyEvent["9",Modifiers->{Command,Shift}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "(\[SelectionPlaceholder])", Placeholder]]], (* Matching bracketing bars*) Item[KeyEvent["|",Modifiers->{Command,Shift}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "\[LeftBracketingBar]\[SelectionPlaceholder]\[RightBracketingBar]", Placeholder]]], (* Matching double bracketing bars*) Item[KeyEvent["|",Modifiers->{Command,Alt}], FrontEndExecute[FrontEndNotebookApply[FrontEndInputNotebook[], "\[LeftDoubleBracketingBar]\[SelectionPlaceholder]\[RightDoubleBracketingBar]", Placeholder]]] but only the double quotes and the matching parentheses work correctly. These things used to work fine in version 8, but then they degraded over time. I complained with Wolfram, but they didn't seem to care about maintaining this functionality. Answer Posted 2 years ago  Hi Gianluca,I use Control rather than Command on my Mac e.g. Item[KeyEvent["[",Modifiers->{Control}], FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[], "[\[SelectionPlaceholder]]"],FrontEndToken["MovePreviousPlaceHolder"]}]], Item[KeyEvent["]",Modifiers->{Control}], FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[], "\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]"], FrontEndToken["MovePreviousPlaceHolder"],FrontEndToken["MovePreviousPlaceHolder"]}]],  Answer Posted 2 years ago  Hi Gianluca, that's too bad! I'm using version 12.0, but I'll try your shortcuts out later and let you know if they work for me. Answer Posted 2 years ago  Cleaner to place the modified KeyEventTranslations.tr in FileNameJoin[{$UserBaseDirectory, "SystemFiles/FrontEnd/TextResources/Macintosh"}] rather than changing the installed application contents.
Posted 2 years ago
 Hi Rohit, you are correct, but the reason I'm using the system application folder is that I have a few different copies of mma, and I've encountered issues when configuring them from a common \$UserBaseDirectory.
Posted 2 years ago
 It appears that Cmd-[ will not work on my mac either, but if I use Mike's shortcut and change it to Control, it works. I suspect the OS is using CMD-[ for something else. For example, in System Preferences, CMD-[ is back in the view menu. Maybe that definition is global so Mathematica can't use it.Regards,Neil
Posted 2 years ago
 I configured the very same keyboard shortcuts Cmd-[ etc in the BBEdit text editor, and they work fine in there.
Posted 2 years ago
 - Congratulations! This post is now featured in our Staff Pick column as distinguished by a badge on your profile of a Featured Contributor! Thank you, keep it coming!
Posted 2 years ago
 Gianluca,Please clarify your post. Are you saying that it now works in Mathematica or are you saying that you can use Cmd-[ as a shortcut in another program (but not in Mathematica)?Regards Neil
Posted 2 years ago
 These shortcuts would only in Mathematica (if you configure them as above).
Posted 2 years ago
 I have set the BBEdit text editor so that Cmd-[, Cmd-{, Cmd-" Cmd-( give [] {} "" () pairs, enclosing any selected text, and they work fine.The KeyEventTranslations that I reported above were meant to reproduce the same behaviours, but among them only Cmd-" and Cmd-( perform as desired on my system. I remember that back during Mathematica version 8 (or 7, I am not sure), when I first heard of these tricks, they were working correctly.I wonder if it is an idiosincrasy of my system.
Posted 2 years ago
 FYI, check Shortcuts:https://mathematica.stackexchange.com/a/85824/5478 Get@"http://www.mertig.com/shortcuts.m" It does not support [_] but [[_]]` + many more.
Posted 2 years ago
 @kuba Cool, thanks for the link I'll check it out. Do you know if there's any overview/list of the ones Rolf supports? Also, do they work on linux and windows as well as macos?