MacOS – How to set system keyboard shortcuts via command line

catalinadefaultsmacossystem-prefs

I'm used to have some keyboard shortcut remapping for menu items via Apple's system preferences -> keyboard pane. Some globals, most of them application specific.

What was working in the past (High Sierra if I'm not mistaken) was something like this:

defaults write com.googlecode.iterm2 NSUserKeyEquivalents -dict-add "Advanced Paste…" -string "@~^v"

Running

defaults read com.googlecode.iterm2 NSUserKeyEquivalents

shows that it has been set, but neither System Preferences (after restart) shows it nor the menu item in iTerm2.

If I enter it manually in System Preferences, the read command reveals the same (after deleting it before) and the new shortcut is visible in the menu.

This seems to be the case on Catalina for every application and shortcut I previously added via a setup script, e.g. for apps like Safari, Google Chrome, etc.

I also tried

defaults write -app iTerm ...

but it has the exact same effect.

Did anything change on (at least) Catalina? What's the correct way nowadays to do it?

Best Answer

Figured it out. You have to add the application you want to modify keys for to com.apple.universalaccess.

defaults write com.apple.universalaccess com.apple.custommenu.apps -array-add "com.googlecode.iterm2"

or if all keyboard equivalents are set via one file, you can also write the full list at once

defaults write com.apple.universalaccess com.apple.custommenu.apps -array "com.googlecode.iterm2" "com.apple.Safari" "com.google.Chrome"

The other thing that was tricky was, that my iterm2 prefs file is symlinked and therefore doesn't appear in the list of domains nor is read with the defaults tool. The keyboard however works, since it's listed in the prefs file already.