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?

  • What are you trying to replicate?
    – Daniel Beck
    Feb 1, 2013 at 19:41
  • The keyboard mappings that would otherwise have to be configured through the GUI.
    – Maros
    Feb 1, 2013 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, 2013 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
    Feb 1, 2013 at 19:51

2 Answers 2


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 com.apple.Automator NSUserKeyEquivalents
    "Hide Library" = "@$l";
    "Show Library" = "@$l";
$ defaults write com.apple.Terminal 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.

  • 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, 2014 at 21:31
  • 2
    These days in Mojave, one can change modifier keys in the Keyboard preference pane. But I'm at a loss for how to do this from the command line. Anyone know?
    – krry
    Feb 27, 2019 at 0:38

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/KeyRemap4MacBook_cli.app/Contents/MacOS/KeyRemap4MacBook_cli enable private.test.

  • 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, 2014 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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