Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a script that will replicate my OS X setup. In particular I want to remap the arrow keys to CMD + H,J,K,L.

The technique seems to be similar to this Linux solution, except the key file to modify is found in /usr/X11/share/X11/xkb/symbols/us. It's not really working as expected though and seems to be mapping the option key.

How can I remap keys via the command line in OS X?

share|improve this question
What are you trying to replicate? – Daniel Beck Feb 1 '13 at 19:41
The keyboard mappings that would otherwise have to be configured through the GUI. – Maros Hluska Feb 1 '13 at 19:46
Which GUI? The preferences of the Terminal application? Most applications' settings are already stored as simple files and can easily be transferred to another machine. Additionally, Terminal allows exporting and importing settings. – Daniel Beck Feb 1 '13 at 19:49
I'm just looking for a system-wide way to remap keys via the command-line. By GUI I meant Keyboard settings in System Preferences. – Maros Hluska Feb 1 '13 at 19:51
up vote 3 down vote accepted

Application-specific keyboard shortcuts like those from System Preferences are stored in the preference files of the corresponding application in /Users/username/Library/Preferences/, in the top-level NSUserKeyEquivalents key.

Those files can be read and written using the /usr/libexec/PlistBuddy and defaults programs on the command line.

$ defaults read NSUserKeyEquivalents
    "Hide Library" = "@$l";
    "Show Library" = "@$l";
$ defaults write NSUserKeyEquivalents -dict-add "About Terminal" '^@$a'

Symbols and their meanings:

  • @ is Command
  • $ is Shift
  • ^ is Control
  • ~ is Option

Additionally, you can simply copy these files to other machines to take your user preferences with you.

share|improve this answer
How would I find the modifier key? I changed Caps Lock to control, how can I find this setting via command line? – SimonW Aug 4 '14 at 21:31

Using KeyRemap4MacBook, you could save this as private.xml:

<?xml version="1.0"?>
<autogen>--KeyToKey-- KeyCode::H, VK_COMMAND | ModifierFlag::NONE, KeyCode::CURSOR_LEFT</autogen>
<autogen>--KeyToKey-- KeyCode::H, VK_COMMAND | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_LEFT, ModifierFlag::SHIFT_L</autogen>
<autogen>--KeyToKey-- KeyCode::J, VK_COMMAND | ModifierFlag::NONE, KeyCode::CURSOR_DOWN</autogen>
<autogen>--KeyToKey-- KeyCode::J, VK_COMMAND | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_DOWN, ModifierFlag::SHIFT_L</autogen>
<autogen>--KeyToKey-- KeyCode::K, VK_COMMAND | ModifierFlag::NONE, KeyCode::CURSOR_UP</autogen>
<autogen>--KeyToKey-- KeyCode::K, VK_COMMAND | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_UP, ModifierFlag::SHIFT_L</autogen>
<autogen>--KeyToKey-- KeyCode::L, VK_COMMAND | ModifierFlag::NONE, KeyCode::CURSOR_RIGHT</autogen>
<autogen>--KeyToKey-- KeyCode::L, VK_COMMAND | VK_SHIFT | ModifierFlag::NONE, KeyCode::CURSOR_RIGHT, ModifierFlag::SHIFT_L</autogen>

Leaving out | ModifierFlag::NONE would also change for example ⌥⌘H to ←.

See the source for the key code constants and predefined settings.

If you want to fully automate the configuration, you can enable the setting with /Library/org.pqrs/KeyRemap4MacBook/app/ enable private.test.

share|improve this answer
This is what I'm looking for, but I didn't understand how I would do the configuration from a command line script... – SimonW Aug 4 '14 at 22:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.