Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 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 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.

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 at 21:31

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

<?xml version="1.0"?>
<root>
<item>
<name>test</name>
<identifier>private.test</identifier>
<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>
</item>
</root>

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.

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

Your Answer

 
discard

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.