Message Boards Message Boards

The Most Convenient Keyboard Shortcuts Ever

Posted 4 years ago

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 + [

enter image description here

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

enter image description here

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[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"[\[SelectionPlaceholder]]"],FrontEndToken["MovePreviousPlaceHolder"]}]]

And for the second (of course making sure they are separated by a comma):

Item[KeyEvent["]",Modifiers->{Command}],FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[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.

POSTED BY: Michael Sollami
15 Replies

All my keyboard shortcuts have stopped working with version 12.1. Oh well.

POSTED BY: Gianluca Gorni

@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?

POSTED BY: Michael Sollami

There is an image from Shortcuts documentation attached to the SE post I linked. And it suggests they work on Linux and macOS.

p.s. you may also be interested in: https://mathematica.stackexchange.com/questions/164653/live-code-templates

POSTED BY: Kuba Podkalicki

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 BY: Kuba Podkalicki

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 BY: Gianluca Gorni

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 BY: Neil Singer

These shortcuts would only in Mathematica (if you configure them as above).

POSTED BY: Michael Sollami

enter image description here - 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 BY: Moderation Team

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 BY: Neil Singer

I configured the very same keyboard shortcuts Cmd-[ etc in the BBEdit text editor, and they work fine in there.

POSTED BY: Gianluca Gorni
Posted 4 years ago

Cleaner to place the modified KeyEventTranslations.tr in

FileNameJoin[{$UserBaseDirectory, "SystemFiles/FrontEnd/TextResources/Macintosh"}]

rather than changing the installed application contents.

POSTED BY: Rohit Namjoshi

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 BY: Michael Sollami

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[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
      "\"\[SelectionPlaceholder]\"", Placeholder]]],
(* Matching curly braces*)
Item[KeyEvent["[",Modifiers->{Command,Shift}],
    FrontEndExecute[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
      "{\[SelectionPlaceholder]}", Placeholder]]],
(* Matching square braces*)
Item[KeyEvent["[",Modifiers->{Command}],
    FrontEndExecute[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
      "[\[SelectionPlaceholder]]", Placeholder]]],
(* Matching parentheses*)
Item[KeyEvent["9",Modifiers->{Command,Shift}],
    FrontEndExecute[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
      "(\[SelectionPlaceholder])", Placeholder]]],
(* Matching bracketing bars*)
Item[KeyEvent["|",Modifiers->{Command,Shift}],
    FrontEndExecute[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
     "\[LeftBracketingBar]\[SelectionPlaceholder]\[RightBracketingBar]", 
     Placeholder]]],
(* Matching double bracketing bars*)
Item[KeyEvent["|",Modifiers->{Command,Alt}],
    FrontEndExecute[FrontEnd`NotebookApply[FrontEnd`InputNotebook[],
     "\[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.

POSTED BY: Gianluca Gorni
Posted 4 years ago

Hi Gianluca,

I use Control rather than Command on my Mac e.g.

Item[KeyEvent["[",Modifiers->{Control}],
    FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
    "[\[SelectionPlaceholder]]"],FrontEndToken["MovePreviousPlaceHolder"]}]],

Item[KeyEvent["]",Modifiers->{Control}],
    FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
    "\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]"],
    FrontEndToken["MovePreviousPlaceHolder"],FrontEndToken["MovePreviousPlaceHolder"]}]],
POSTED BY: Rohit Namjoshi

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.

POSTED BY: Michael Sollami
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract