Setting Keyboard Shortcuts from Terminal in macOS
Posted on 2017-01-05
It's been a few months since my last post. I've spent a lot of time working on my blog, but all things behind the scenes that most people wouldn't even notice.
Setting keyboard shortcuts on Mac is actually fairly easy, but it requires a lot of clicking around. Fortunately there's a way to do this from the terminal that's faster and easier.
defaults command in MacOS is nothing short of a mystery. It does some powerful things, but the documentation is sparse and half of the time I don't know what I'm doing. That being said, I've had a script written for a long time called
new_computer.sh where I set all of my favorite global and application-specific shortcuts when getting a new computer.
Let's take an example of a shortcut everyone should have: Print As PDF from within a print dialog. I've always set it to ⌘ ⇧ P. To do this within System Preferences, the steps are:
- Open the Keyboard preference Pane
- Click the Shortcuts tab
- Click App Shortcuts
- Click the + symbol
- Fill out the prompts
- Leave All Applications Selected
- Menu Title is "Save as PDF…" (it's an elipsis, not three periods. Type
option ;to get the symbol)
- Choose your shortcut
It's almost too many steps for one shortcut, let alone multiple. Let's try this in Terminal:
defaults write -globalDomain NSUserKeyEquivalents -dict-add "Save as PDF\\U2026" "@\$p";
Easy, right? Sort of. The syntax for writing global shortcuts is fairly straight forward. If you're not creating a shortcut for a specific application, you can use the command above and simply change the title and shortcut. Here are how to represent all of the modifier keys:
- @ is command
- ^ is control
- ~ is option
- $ is shift
So command-shift p becomes
The reason this came up was that Omnifocus recently added tabs. This is great except that there's no shortcut for cycling through the tabs. This makes the feature almost pointless for me. So to add shortcuts, I ended up using the command above, but I need to target Omnifocus only.
defaults write com.omnigroup.OmniFocus2 NSUserKeyEquivalents -dict-add "Show Next Tab" "^\\U005D" defaults write com.omnigroup.OmniFocus2 NSUserKeyEquivalents -dict-add "Show Previous Tab" "^\\U005B"
Here I'm setting show next/previous tab to control [ and control ]. Once you've set your keyboard shortcuts, you'll need to quit and re-launch the application in order for the new preferences to be read.
Tags: automation bash