For anyone else stumbling across this, I was looking for a way to put this in my .dotfiles when setting up new machines. All I wanted was to remap caps-lock to escape.
In Sierra 10.12 it seems the solution on this post became invalid. The documented way from Apple works for me using 10.14.6 Mojave (*with a caveat)
TL;DR;
Since Sierra 10.12, see:
TN2450
# Remap caps-lock to escape
hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}'
Before Sierra 10.12
plutil
Interestingly the solution is still correct about this file updating and saving information concerning keymapping when the user goes through the GUI to change modifier keys. However updating the keymapping info here doesn't seem to affect anything anymore.
This example uses the plutil
command which is the preferred way to update plists as defaults write
is deprecated according to the man page See filepath.
Also, see comparisons between the two commands and the library PlistBuddy.
#!/usr/bin/env bash
# Quit System Preferences so it doesn't muck with your settings
osascript -e 'tell application "System Preferences" to quit'
# Get your machine's UUID
__UUID__=$(
ioreg -ad2 -c IOPlatformExpertDevice | xmllint --xpath \
'//key[.="IOPlatformUUID"]/following-sibling::*[1]/text()' -
)
# Replace all contents of the array for the caps lock key/value pair.
plutil -replace "com\.apple\.keyboard\.modifiermapping\.1452-636-0" \
-json '[{
"HIDKeyboardModifierMappingDst": 30064771113,
"HIDKeyboardModifierMappingSrc": 30064771129
}]' \
~/Library/Preferences/ByHost/.GlobalPreferences.${__UUID__}.plist
# Pretty print file
plutil -p ~/Library/Preferences/ByHost/.GlobalPreferences.${__UUID__}.plist
Since Sierra 10.12
hidutil
The new solution is much less verbose AND doesn't rely on filepath args or giving arguments types for the not very well documented plutil
command.
You can see this Stack Exchange post for another explanation.
This technical doc TN2450 describes the strategies for handling this with hidutil
or Xcode.
#!/usr/bin/env bash
# Quit System Preferences so it doesn't muck with your settings
osascript -e 'tell application "System Preferences" to quit'
# Remap caps-lock to escape
hidutil property --set '{
"UserKeyMapping":[{
"HIDKeyboardModifierMappingSrc":0x700000039,
"HIDKeyboardModifierMappingDst":0x700000029
}]
}'
- Caveat this doesn't seem to change the GUI's display when you visit modifier keys in System Preferences > Keyboard. Nonetheless, it still works like a charm for the functionality.