# Switch actions of Shift+Enter and Enter in notebook interface?

How can I switch the functionality of the Enter and Shift+Enter key combinations in Windows?

I find Matlab's interface more intuitive sometimes. By default in Mathematica's front-end, the Shift+Enter combination processes input in the active cell, while Enter inserts a carriage-return.

Is there a standard way to re-map the actions of different key combinations?

• Have you tried KeyEventTranslations.tr? On OS X there's an explicit entry for the Evaluate command. – Szabolcs Oct 11 '16 at 19:38
• @Szabolcs no I haven't I have it open now not sure what to change exactly but it looks logical. – William Oct 11 '16 at 19:45
• Do make a backup. If you keep the backup in the same location, make sure it doesn't have the .tr extension. Not sure about how much is shared between Windows and Mac, but on Mac "Enter" and "Return" are considered different keys. "Return" is what we normally use and "Enter" is accessed either as Shift-Return or through the numpad. KeyEventTranslations uses the same names (Return vs Enter) on Mac. – Szabolcs Oct 11 '16 at 21:39
• @Kuba that's correct I would like to swap the Enter and Shift+Enter keys – William Oct 12 '16 at 15:06

First move KeyEventTranslations.tr file to your $UserBaseDirectory in order to not damage installation files. (Rebinding keys to define new keyboard shortcuts) Open the file and find those lines: ... (* Evaluation *) ... Item[KeyEvent["Return", Modifiers -> {Shift}], "HandleShiftReturn"] ... (* Input *) Item[KeyEvent["Return"], "Linebreak"] ... and swap "HandleShiftReturn" with "Linebreak". Works on Windows. • This does not work on linux ;( – William Oct 13 '16 at 21:26 • Is there a way to do this on a per cell basis easily? – William Oct 13 '16 at 23:00 • @Liam No, you can change each event separately but both with result in a loop: SetOptions[ PreviousCell[], CellEventActions :> { {"MenuCommand", "HandleShiftReturn"} :> FrontEndTokenExecute["Linebreak"], "ReturnKeyDown" :> FrontEndTokenExecute["HandleShiftReturn"] } ] – Kuba Oct 14 '16 at 6:24 • @Kuba Strangely enough, it works for PreviousCell[] but doesn't work for $FrontEndSession... – Alexey Popkov Oct 15 '16 at 19:29

Using Autohotkey the following works pretty well.

#IfWinActive ahk_class NotebookFrame
Enter::Send {blind}{shift down}{enter}{shift up}
Shift & Enter::Send {enter}
#IfWinActive


In case the internals are ever changed you can use the following to get the internal class.

Return::
WinGetClass, class, A
MsgBox, The active window's class is "%class%".
return


This piece of code should do the trick. It takes effect immediately.

SetOptions[$FrontEnd, FrontEndEventActions:>{{"MenuCommand", "HandleShiftReturn"} :> FrontEndTokenExecute["Linebreak"], "ReturnKeyDown" :> FrontEndTokenExecute["EvaluateCells"]}]  (Tested on ArchLinux/MacOS_Sierra/Win10Server2016 with MMA 11. Hopefully it will work on Ubuntu, too) If you want to run it automatically each time you open MMA, you can modify the code and insert it to the frontend configuration file $UserBaseDirectory/FrontEnd/init.m

And many thanks to Kuba. The comment under his answer inspired me a lot. Without his idea I wouldn't be able to produce this answer.

• EvaluateCells is what numpad enter does, not shift+enter. – Kuba Oct 15 '16 at 17:00
• (+1) Works with Mathematica 11.0.0 on Win7 x64. After evaluating this the usual (not numpad) Enter evaluates Cell, and Shift+Enter inserts a linebreak. – Alexey Popkov Oct 15 '16 at 17:04
• Did you take this from Kuba ;) – William Oct 15 '16 at 17:53
• @Liam Yes, Kuba is whom the credit should go to : ) – navigaid Oct 15 '16 at 17:58

For completion this technique seems to work fairly well in ubuntu.

Basically if you open /usr/share/X11/xkb/symbols/pc and edit the file you can make the following 2 lines like the following.

key <RTRN> {    [ KP_Enter      ]   };
key <CAPS> {    [ Return        ]   };


Now CapsLock functions as the return key in Mathematica and the return key functions as the cellevaluation function.

This isn't perfect but works fairly well and most applications seem to still work well.