Skip to content

Instantly share code, notes, and snippets.

@C-Loftus
Created February 20, 2025 15:08
Show Gist options
  • Select an option

  • Save C-Loftus/2b1c4682cbcaa4f27ea92b702d0e4b2a to your computer and use it in GitHub Desktop.

Select an option

Save C-Loftus/2b1c4682cbcaa4f27ea92b702d0e4b2a to your computer and use it in GitHub Desktop.
Swap capslock and escape on macos
#!/bin/bash
# Check current mapping and extract relevant content
CURRENT_MAPPING=$(hidutil property --get "UserKeyMapping" | grep "HIDKeyboardModifierMappingSrc")
# Define the swap mapping
SWAP_MAPPING='{
"UserKeyMapping": [
{
"HIDKeyboardModifierMappingSrc": 0x700000029,
"HIDKeyboardModifierMappingDst": 0x700000039
},
{
"HIDKeyboardModifierMappingSrc": 0x700000039,
"HIDKeyboardModifierMappingDst": 0x700000029
}
]
}'
# If CURRENT_MAPPING is not empty, that means a remap is currently active, so we clear it
if [[ -n "$CURRENT_MAPPING" ]]; then
hidutil property --set '{"UserKeyMapping": []}'
echo "Caps Lock and Escape keys have been restored to default."
else
hidutil property --set "$SWAP_MAPPING"
echo "Caps Lock and Escape keys have been swapped."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment