# Rebinding keys to define new keyboard shortcuts

I've checked MathematicaPreferences, and poked around the options on the toolbar. However, I don't see a way to define new keyboard shortcuts in Mathematica.

I've used VIM for 10+ years, and I've rebound Ctrl+F to be Esc for the last decade or so. I would like to also use this shortcut in Mathematica, to have Ctrl+F be Esc, so hitting something like:

Ctrl+F S Ctrl+F

should give me capital Sigma.

Question: how do I define new keybindings in Mathematica?

(I have a CS background, so I'd prefer things that involve editing config files / writing scripts -- that would give me more power -- over clicking things in a GUI).

• why not rebind it at the OS level if you're married to it? Also see this related question: stackoverflow.com/q/5808695 Looks like vim users are pretty anal about their bindings... ;) – rm -rf Jun 29 '12 at 0:46
• There are want to rebind caps-lock to esc; but I only want to rebind ctrl-f to esc (my caps-lock is bound to ctrl). Truth be told, all I need is a turing-complete keyboard mapping language, and I'd be happy. – user1602 Jun 29 '12 at 0:53
• Changed title to reflect a more general problem, and added Q to customization wiki as perfect example. – István Zachar Jun 29 '12 at 8:32
• You can use Autohotkey for a systemwise remap. It is easy as hell, and the script can be turned on/off on the fly anytime with a click (which is not true for changes made to KeyEventsTranslation.tr). – István Zachar Jun 29 '12 at 8:39

Since you want this functionality to be able to insert aliases using the EscshortcutEsc syntax, you can edit the KeyEventsTranslation.tr file to achieve this. Copy the following file:

$InstallationDirectory/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr  or its equivalent on your system to $UserBaseDirectory/ (with the same directory tree) and add the following modifications after EventTranslations[{ in the file:

Item[KeyEvent["f", Modifiers -> {Control}],
FrontEndExecute[{
FrontEndNotebookWrite[FrontEndInputNotebook[],
"\[AliasDelimiter]", After]
}]],

• Also, since you're editing your .tr file, you might as well include these shortcuts for 〚〛 (or Part) too, while you're at it, as these are very useful and commonly used – rm -rf Jun 29 '12 at 1:06
• I wonder, how easy is to maintain such changed system files as KeyEventTranslations.tr when someone changes to a new version of Mathematica and/or to a new computer? Can these customized files handled in a bunch? Would a software update revert these to default? – István Zachar Jun 29 '12 at 8:35
• @IstvánZachar You can put a version of KeyEventTranslations.tr with your changes in $UserBaseDirectory/SystemFiles/FrontEnd/TextResources/Macintosh/ because this file is read before the KeyEventTranslations.tr under $InstallationDirectory an so survives new Versions. – Peter Breitfeld Jun 29 '12 at 9:33
• @IstvánZachar As Peter said, the files in $UserBaseDirectory supersedes the installation files. In my answer too, I suggest modifying this file and not the installation file – rm -rf Jun 29 '12 at 15:11 • @PeterBreitfeld Like you suggested, I cpoied KeyEventTranslations.trand MenuSetup.trin $UserBaseDirectory\SystemFiles\FrontEnd\TextResources\Windows. These were modified Mma10 files. But since then I'm not able to start Mm9 correctly. Where is my fault? – Phab Nov 25 '14 at 12:16

@rm-rf and @PeterBreitfeldh described a way to define custom key bindings in \$UserBaseDirectory/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr. This technique worked great until Mathematica version 11, but it stopped working in version 12, at least on macOS.

I talked to Wolfram Technical Support, and they say that the Mathematica Notebook code is rewritten with macOS' Cocoa UI framework (instead of Carbon, I guess) in Version 12, as described in https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn12.html. This made KeyEventTranslations.tr stopped functioning in Mathematica for macOS.

In the Cocoa-based macOS apps, there is a universal way of defining custom key bindings, as described in https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/TextDefaultsBindings/TextDefaultsBindings.html. Surprisingly this technique does not work, either, even though Mathematica Notebook is now written with the Cocoa framework. Wolfram Technical Support says that not all Cocoa features are supported in the new Mathematica Notebook, but they are working on it.

• Probably because you used "KeyEventsTranslation.tr" and not the correct "KeyEventTranslations.tr". – Carl Woll May 30 at 16:09
• @CarlWoll: no, I am using the correct file name KeyEventTranslations.tr. Sorry about the typo in the main text. Some of the earlier postings used the wrong file name, and I just copy-and-pasted it instead of copying the correct file name from Finder. – dashmile May 31 at 18:03
• I use M12 on MacOS Mojave 10.14.4, and editing the KeyEventTranslations.tr works as expected. Perhaps you tried to add a key event that already existed? Or maybe the key event you tried to add conflicts with the key shortcuts present in the menu system, in which case the menu system wins? – Carl Woll May 31 at 18:17
• That's weird, because Wolfram Technical Support tried to add new key bindings to KeyEventTranslations.tr and confirmed that it didn't work.Could you try to add Item[KeyEvent["f", Modifiers->{Option}], "MoveNextWord"] in KeyEventTranslations.tr and see if Option-F moves the cursor to the next word? – dashmile Jun 2 at 1:42
• It looks like KeyEvent[letter, Modifiers->{Option}]` doesn't work in M12. If you use a non-letter key (e.g., "Up", "Escape", "Tab") it will work. Or, you can add another modifier key, e.g., Command. – Carl Woll Jun 2 at 2:26