Backup your MacOS keyboard shortcuts from command line

Backup your MacOS keyboard shortcuts from command line

Tags
Software Development
MacOS
TIL
Published
April 1, 2024
Author
Onni Hakala

Modifying keyboard shortcuts in System settings

This week I was setting up a fresh MacBook using my trusty dotfiles. One thing that has bothered me for a long time has been that I haven’t found a way to persist my keyboard shortcuts between computers. I really dislike how the default option in MacOS to cycle between windows for same application doesn’t work like it does in Windows.
So everytime I have gotten a new computer I have needed to go to System Settings → Keyboard → shortcuts → Keyboard → “Move focus to next window” and change it to Option (⌥) + TAB.
notion image
Changing the keyboard settings is easy but hard to remember if you have many of them ❓🤔

How I found out how to automate this using defaults

After reading this excellent blog post about changing Keyboard shortcuts I understood that all of the keyboard shortcuts can be changed with the magical defaults cli by manipulating the NSUserKeyEquivalents or other attributes. This lead me to search other dotfiles in Github and I finally found excellent example of this in krikchaip/dotfiles repository. Using their example I understood that com.apple.symbolichotkeys domain contains all of the keyboard shortcuts.

How to backup and import all keyboard shortcuts

♻️
This is especially useful if you are using a company provided laptop and can’t create Time machine backups to transfer your settings to new computer when switching jobs.
In case you just want to backup everything you have saved and transfer them to new computer you can do use the following steps in next script:
# Step 1: Backup MacOS keyboard settings defaults export com.apple.symbolichotkeys - > ./my-macos-keyboard-shortcuts.xml # Step 2: Save the file into iCloud drive, Dropbox, dotfiles or ... # Step 3: Download into new computer # ... # Step 4: Import the same xml file in new MacOS computer defaults import com.apple.symbolichotkeys ./my-macos-keyboard-shortcuts.xml # Step 5: Reload settings /System/Library/PrivateFrameworks/SystemAdministration.framework/Resources/activateSettings -u

How to update single keyboard shortcuts from terminal

If you want to just to override single keyboard shortcut you can run following script in your terminal. This example it will change the Move focus to next window shortcut into ⌥ + Tab.
#!/bin/zsh # Shutdown System Settings before these changes osascript -e 'tell application "System Settings" to quit' # XML template for com.apple.symbolichotkeys -> AppleSymbolicHotKeys function key-combo() { local TEMPLATE=" <dict> <key>enabled</key><true/> <key>value</key><dict> <key>type</key><string>standard</string> <key>parameters</key> <array> <integer>$1</integer> <integer>$2</integer> <integer>$3</integer> </array> </dict> </dict> " echo "$TEMPLATE" } # Keyboard > Keyboard Shortcuts... > Keyboard > Move focus to next window = option + tab defaults write com.apple.symbolichotkeys AppleSymbolicHotKeys -dict-add 27 "$(key-combo 65535 48 524288)" # Reloads the defaults without needing to logout /System/Library/PrivateFrameworks/SystemAdministration.framework/Resources/activateSettings -u

How to find other keyboard shortcuts

You can run following command to see more keys by running following command:
defaults read com.apple.symbolichotkeys AppleSymbolicHotKeys
I compared the output of this command to find that 27 is the key I’m interested and 65535 48 524288 is the key combo I want. These are pretty nasty to work with and I hope they don’t change in new MacOS releases 😠

Conclusion

Defaults provides the ability to change various settings, though its documentation is limited. You can refer to http://macos-defaults.com/ for numerous instances. However, it does not cover topics such as keyboard shortcuts.