12

It is very handy to have shortcuts for frequently used glyphs, e.g. [[, ]], <|, |>, and ->. I know already that we can do this by modifying KeyEventTranslations.tr located in

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

Working with US keyboard layout, I added the following to the file:

...

Item[KeyEvent[">", Modifiers -> {Control}],
FrontEndExecute[{
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[AliasDelimiter]",After],
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"->",After],
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[AliasDelimiter]",After]
}]
],

Item[KeyEvent["[", Modifiers -> {Shift,Control}],
FrontEndExecute[{
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[AliasDelimiter]",After],
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"<|",After],
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[AliasDelimiter]",After]
}]
],

Item[KeyEvent["]", Modifiers -> {Shift,Control}],
FrontEndExecute[{
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[AliasDelimiter]",After],
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"|>",After],
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[AliasDelimiter]",After]
}]
],

...

This worked splendidly for the last two or three major releases of Mathematica. But now in version 12, precisely what Szabolcs anticipated here happened to me: Seemingly, new shortcuts

  • Shift+Ctrl+.,
  • Shift+Ctrl+[, and
  • Shift+Ctrl+]

have been added, ruining my established workflow. Actually, I am not sure that these shortcuts are new, but starting with version 12, my former approach does work anymore. At least Shift+Ctrl+. seems to be remapped to something related to notebook cell selection. Shift+Ctrl+,, another shortcut that I remapped and that I use often, is already present in version 11.3 -- but the remapping worked well. So maybe the order in which the shortcuts are loaded when the FrontEnd starts has changed?

I don't use these shortcuts in the new built-in way; I'd rather prefer to overwrite them with my old settings because I #!@&ing don't want to relearn typing every time a new version is realeased!

I also found the "Shortcuts`" package by Rolf Mertig, but as Rolf pointed out, it does not seem to work anymore (I have to admit that I have not tried it yet).

Has anybody found a robust, future-proof solution for this?

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • 2
    Do you have a reference for the new shortcuts that have been introduced? – mikado Apr 27 '19 at 09:56
  • I am still using version 10.1, but have you tried editing MenuSetup.tr? – Mr.Wizard Apr 27 '19 at 10:39
  • @Mr.Wizard Thanks for the pointer. In MenuSetup.tr, I was able to find the line MenuItem["Extend Selection", "ExpandSelection", MenuKey[".", Modifiers->{"Control"}]], but that should not modify Shift+Ctrl+. or should it? Neither did I find any further remappings of ">" or "." that would fit the pattern... – Henrik Schumacher Apr 27 '19 at 11:10
  • @mikado Hm. Actually I haven't found them documented yet. Shift + Ctr + , is another shortcut that I use often. It worked in version 11.3 but now I find it int the menu under "Insert" -> "Table/Matrix" -> "Add Column". And it was already there in version 11.3! I gues that WRI has changed the precendences of the loading of tr-files... – Henrik Schumacher Apr 27 '19 at 11:19
  • Why aren't you storing a modified version of KeyEventTranslations.tr in the directory FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources", "Macintosh"}] instead? I tried giving Shift+Ctrl+. a new short cut there, and it worked fine for me. – Carl Woll Apr 27 '19 at 14:16
  • @CarlWoll. Erm. I'd love to do that but that directory does not exist on my hard drive. Are you sure the path is correct? – Henrik Schumacher Apr 27 '19 at 14:27
  • If it doesn't exist, I think you just need to create it. – Carl Woll Apr 27 '19 at 14:30
  • @CarlWoll Gnah. I tried that immediately when you gave me the path. Did not work either. I also tried to erase the whole $UserBaseDirectory and had Mathematica recreate it on relaunch before creating FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources", "Macintosh"}] and copying my old KeyEventTranslations.tr there. =/ Maybe. I should consider a fresh reinstall of Mathematica on Monday... – Henrik Schumacher Apr 27 '19 at 14:39
  • Did you create the file, and then quit and relaunch Mathematica? The new file only takes affect after a relaunch of Mathematica. You shouldn't have to reinstall Mathematica. – Carl Woll Apr 27 '19 at 14:44
  • "Did you create the file, and then quit and relaunch Mathematica?" I know from experience that KeyEventTranslations.tr should be manipulated only with Mathematica shut down (and with an external editor). And that is what I did. Several times by now. =| – Henrik Schumacher Apr 27 '19 at 15:04
  • Most shortcuts still work in V12, Windows, MacOS and Linux. Minimal installation is done by calling once Import["http://www.mertig.com/s.m"] – Rolf Mertig Apr 27 '19 at 19:24
  • 1
    I tried changing my modifier, and got a shortcut to work. I had used

    Item[KeyEvent["q", Modifiers -> {Shift, Option}], FrontEndExecute[FrontEndToken[SelectedNotebook[], "EvaluatorQuit", Automatic]]],

    to force the Kernel to quit. With version 12, when I typed the key sequence instead I saw the character Œ inserted. When I switched to use Modifiers -> {Shift, Control}, the shortcut started working.

    Is there any built-in variable that describes the search path for keyboard overrides?

    – Spencer Rugaber May 01 '19 at 13:22

1 Answers1

5

KeyEventTranslations.tr started making problems in version 12, but just follow Mr. Wizard's advice. Some shortcuts you might like are already present in MenuSetup.tr, you can just add a few more in the same style. Find the line

MenuItem["Matching []", "InsertMatchingBrackets", MenuKey["]", Modifiers->{"Command", "Option"}]],

and add few more lines before or after

MenuItem["Rule \[Rule]", 
    FrontEndExecute[FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[Rule]", After]], MenuKey[">", Modifiers->{"Control"}]],
MenuItem["Double bracket \[LeftDoubleBracket]", 
    FrontEndExecute[FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[LeftDoubleBracket]", After]], MenuKey["[", Modifiers->{"Option"}]],
MenuItem["Double bracket \[RightDoubleBracket]", 
    FrontEndExecute[FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[RightDoubleBracket]", After]], MenuKey["]", Modifiers->{"Option"}]],
MenuItem["Left association \[LeftAssociation]", 
    FrontEndExecute[FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[LeftAssociation]", After]], MenuKey["[", Modifiers->{"Shift", "Control"}]],
MenuItem["Right association \[RightAssociation]", 
    FrontEndExecute[FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[RightAssociation]", After]], MenuKey["]", Modifiers->{"Shift", "Control"}]],

I would even add

MenuItem["Matching \[LeftDoubleBracket]\[RightDoubleBracket]", 
    FrontEndExecute[{
        FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[LeftDoubleBracket]", After],
        FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],"\[RightDoubleBracket]", Before],
    }], MenuKey["]", Modifiers->{"Command", "Control"}]],
Vladimir
  • 1,503
  • 1
  • 12
  • 28
  • 1
    Hah! Splendid! This works. I can even put my beloved \[LeftArrow] back onto Control+Shift+<. Have my accept, have my upvote! Thank you so much! – Henrik Schumacher May 11 '20 at 20:41
  • Glad to help! If you have a lot of shortcuts to add and want to keep your Menu a bit organised, you can introduce a new grouping like this: inside Menu["Insert", { put Menu["My", { and add your shortcuts there, then close }],. Because every shortcut added to MenuSetup.tr adds an item to your menu tabs. – Vladimir May 12 '20 at 13:11
  • Ah, now it is much clearer to my how this works. Thank you once again! – Henrik Schumacher May 12 '20 at 13:20