Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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
add comment

3 Answers 3

Out of curiosity, I started digging around ~/Library/Preferences/ just to see what I could find.

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:

#!/bin/bash                                                                      

mappingplist=com.apple.keyboard.modifiermapping

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;                                  
        })'


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

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.

http://forums.macrumors.com/showthread.php?t=949280

Hope that works!

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
add comment

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
add comment

From apple.stackexchange.com/questions/13598/updating-modifier-key-mappings-through-defaults-command-tool:

defaults -currentHost write -g com.apple.keyboard.modifiermapping.1133-50475-0 -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 ~/Library/Preferences/ByHost/.GlobalPreferences.*.plist.

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
add comment

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.