Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

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

As a heavy Emacs user, I like to remap my Caps Lock key to function as the Ctrl key. There is a convenient dialog in the Keyboard Preference Pane to do that. But the problem is that this setting gets lost after someone logs into another account that hasn't this preference set.

So is there any way of doing the same as the preference pane dialog from the command line?

share|improve this question

Changing preferences and running, defaults -currentHost read -g shows you what changes are being written. I was going to write up a short bash script to automate it, but it looks like I was beaten to the punch:


if [ $1 == "emacs" ]; then
    echo "Switching to emacs modifiers"
    defaults -currentHost write -g $mappingplist '(                              
            HIDKeyboardModifierMappingDst = 4;                                   
            HIDKeyboardModifierMappingSrc = 2; },                                
            HIDKeyboardModifierMappingDst = 12;                                  
            HIDKeyboardModifierMappingSrc = 10;                                  
            HIDKeyboardModifierMappingDst = 2;                                   
            HIDKeyboardModifierMappingSrc = 4;                                   
            HIDKeyboardModifierMappingDst = 10;                                  
            HIDKeyboardModifierMappingSrc = 12;                                  

    echo "Switching to default modifiers"
    defaults -currentHost delete -g $mappingplist

The script takes one argument, if the argument is emacs, then it swaps command and control, if the argument is anything else it restores the defaults.

share|improve this answer
It doesn't seem to work from me here with Snow Leopard, but it's clearly a step in the right direction. – pantulis Dec 15 '10 at 7:01
@pith... what's the -g flag do? I didn't see that in the man page for defaults. Also, this looks like it's swapping two sets of values: 4 with 2 and 10 with 12, yet there are only two modifiers that change. Can you elaborate, or did you simply look at the output but don't know what they actually represent? (At first I suspected four key codes--two on the left, two on the right--but they don't appear to be key codes, at least not as reported by the keyboard driver.) – MarqueIV Feb 10 '12 at 1:38
I do the mappings as you suggest but it doesn't seem to take effect although in the UI it appears mapped. Only doing it via UI seems to work... Any ideas? – SimonW Aug 5 '14 at 14:45

You can use KeyRemap4MacBook with the PCKeyBoardHack. It also allows you to use Emacs keys in any Mac app, including the ones not using Cocoa such as MS apps.

share|improve this answer

Combining a few answers together I came up with this script that seems to work (mapping Caps-lock to Control).

$ keyboard_id = "$(ioreg -n IOHIDKeyboard -r | grep -e VendorID\" -e ProductID | tr -d \"\|[:blank:] | cut -d\= -f2 | tr '\n' -)"
$ defaults -currentHost write -g "${keyboard_id}0" '(
  HIDKeyboardModifierMappingDst = 2;
  HIDKeyboardModifierMappingSrc = 0;

Only problem that it doesn't seem to take any effect... :(

I'll update if it I get it to work.

share|improve this answer

From "Updating modifier key mappings through defaults command tool":

defaults -currentHost write -g -array '<dict><key>HIDKeyboardModifierMappingSrc</key><integer>0</integer><key>HIDKeyboardModifierMappingDst</key><integer>2</integer></dict>

Change 1133 and 50475 to the vendor and product IDs shown by ioreg -n IOHIDKeyboard -r. src 0 and dst -1 would disable caps lock.

defaults -currentHost write -g modifies:


You have to log out and back in to apply changes. If the values are strings (like in the answer by @pithyless), the changes are shown in System Preferences but they don't have any effect.

Values of keys:

-1 none
0 caps lock
1 left shift
2 left control
3 left option
4 left command
5 keypad 0
6 help
9 right shift
10 right control
11 right option
12 right command
share|improve this answer

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.