Disabling Quit and rewriting keyboard shortcuts
This is an old tip with bits and pieces spread throughout the web. I’m going to try and put all of the useful information in one place. Ever since upgrading to Mac OS X 10.4, Safari has become much more reliable. This has resulted in my becoming lazy and writing articles within forms on Safari. I’m writing this article in Safari, for example.
Every once in a while, after looking up some information elsewhere on the web, I will go to close the window with CMD-W and accidentally hit CMD-Q as well. Last night I lost about five minutes of work. It isn’t much, but it is annoying as hell, so I finally decided to learn how to disable Quit in Safari. What I found was a means of disabling or remapping the keyboard equivalents in any Cocoa application.
Mac OS X is designed with a hierarchical system of preferences and options, moving from the individual user up to system-level options. Applications such as Safari generally set their own menu equivalents, but any user can override those equivalents for their own use.
Disabling Safari’s Quit key equivalent
Try typing this in the terminal:
- defaults read com.apple.Safari NSUserKeyEquivalents
Unless you have already remapped your key equivalents, you should receive something like “The domain/default pair of (com.apple.Safari, NSUserKeyEquivalents) does not exist”.
Now, quit Safari and type:
- defaults write com.apple.Safari NSUserKeyEquivalents -dict-add "Quit Safari" "@^Q"
- defaults read com.apple.Safari NSUserKeyEquivalents
You should now see “{"Quit Safari" = "@^Q"; }”. The curly brackets indicate a list. We could easily add multiple changes to Safari’s key equivalents. The “Quit Safari” is what the menu item appears as under the menu. And “@^Q” is the new key equivalent. The next time you start Safari, you’ll need to type CMD-CTRL-Q to quit instead of CMD-Q.
Changing every application
If there is something you want to change in every application, you can change it in the “global domain” rather than in each application separately.
- defaults read -globalDomain NSUserKeyEquivalents
- defaults write -globalDomain NSUserKeyEquivalents -dict-add "Hide Others" "@$H"
- defaults read -globalDomain NSUserKeyEquivalents
The defaults command
The command is “defaults”. This command lets you read and write the defaults for many application and system defaults. Most changes will take effect the next time you start an application. It is best to make changes while an application is closed, to keep it from overwriting your changes when it saves its own preferences the next time it quits.
- defaults action domain key value
You can “read” or “write” the defaults for a domain (and I recommend always reading before writing).
The domain is likely to be the same as the name of the com file (without the .plist extension) in your Library/Preferences folder. You can find this name by typing:
- ls ~/Library/Preferences/com.* | grep -i "application name"
For example,
- ls ~/Library/Preferences/com.* | grep -i "Notes"
- /Users/USER/Library/Preferences/com.rws.Notes.plist
The domain for the Notes application is “com.rws.Notes”.
After the domain we specify the key that we are changing or adding to. The only key we’re using in this article is NSUserKeyEquivalents. Every key has a value, and for NSUserKeyEquivalents the value is a dictionary of key equivalents. A dictionary is a list of keys and values. Above, we gave a value of “@^Q" to the key named “Quit Safari”. A dictionary can have several key-value pairs, allowing us in this case to specify more than one custom key equivalent.
There is more than one way to modify a dictionary, but the easiest is probably with dict and dict-add.
dict-add | Add the next pair or pairs to the list |
---|---|
dict | Replace the current list with the next pair or pairs |
You can specify as many key-value pairs as you wish after -dict or -dict-add. There is unfortunately no -dict-remove. If you want to remove an item from the list, you will need to first read the current list and then write it back without the item you want removed.
If you want to disable a menu item completely, specify "nil" as the value in the key value pair.
If you want to remove the entire list, you can use “delete” instead of “write”:
- defaults delete com.apple.Safari NSUserKeyEquivalents
Use “man defaults” to find out more about the defaults command in Mac OS X.
Key Equivalents
Your key equivalents must begin with a special key: command, option, shift, or control. Further, for the keystroke to work, one of the special characters must be the command key. That is, if you specify “^Q” as your key equivalent, “^Q” will show up in the menu but CTRL-Q will not work to quit your application.
Key equivalents can have one to four special keys, but may only have one non-special key.
Character | Special Key | |
---|---|---|
@ | Command (Apple) | CMD |
~ | Option | OPT |
$ | Shift | SHIFT |
^ | Control | CTRL |
- November 18, 2007: Safari 3’s smart window close
-
I just installed the latest Mac OS X 10.4 update, which installs Safari 3. I’ve also re-enabled quit, because Safari 3 is a lot smarter about closing windows and quitting. If I attempt to quit, or close a window, when I’ve got unsaved text in a window, it warns me first. And if I attempt to quit when I have multiple tabs open, it warns me about that. Those were the reasons I disabled the command key for quitting.
- Mac Modding Shortcuts
- “Make repetitive tasks simpler and maximize ergonomic efficiency with this excerpted chapter from Erica Sadun’s Modding Mac OS X. Neat, concise, and easy-to-use, this excerpt helps you add, remove, and change keyboard shortcut to meet your personal computing...”
- A Script to Make Shortcuts in iTunes
- “I was playing around with iTunes’ .plist file and some Terminal commands and was able to put together a script that allows Command, Shift, Option, and Control plus a letter key to be assigned.”
More macOS tricks
- 42 Astoundingly Useful Scripts and Automations for the Macintosh
- MacOS uses Perl, Python, AppleScript, and Automator and you can write scripts in all of these. Build a talking alarm. Roll dice. Preflight your social media comments. Play music and create ASCII art. Get your retro on and bring your Macintosh into the world of tomorrow with 42 Astoundingly Useful Scripts and Automations for the Macintosh!
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- Avoiding lockFocus when drawing images in Swift on macOS
- Apple’s recommendation is to avoid lockFocus if you’re not creating images directly for the screen. Here are some examples from my own Swift scripts. You can use this to draw text into an image, and to resize images.
- What app keeps stealing focus?
- I’ve been having a problem on Mac OS X with something stealing focus. Here’s how to at least find out what that something is.
- Enable AirPrint for all connected Mac printers
- I have an iPad and an old workhorse of a printer, an HP 1012 LaserJet, connected to my iMac. I almost never need to print from the iPad, but when I do, handyPrint works.
- 14 more pages with the topic macOS tricks, and other related pages
Hey Jerry, Thanks a lot for this guide. It is more thorough than any other one I was able to find. I used it to disable cmd-Q in Firefox.
ls ~/Library/Preferences/*.plist | grep -i "firefox"
yielded:
org.mozilla.firefox.plist
defaults write org.mozilla.firefox NSUserKeyEquivalents -dict-add "Quit Firefox" "@^Q"
yielded:
{
"Quit Firefox" = "@^Q";
}
A FF reboot was necessary to activate the new combination. Now FF cannot be quit by accidentally clicking cmd-Q, only by clicking ^-cmd-Q. Nice.
PacoH at 12:07 a.m. October 18th, 2013
NcVX2
I am trying to change some of the defaults on Mavericks (10.9.2) for the Finder. Specifically, the new "Arrange By" shortcuts have usurped the old "Sort By" keys, and I want to switch the two groups. So "Arrange By->Name" would go from "^@1" (the new default) to "^@~1" and the "Sort By->Name" would do the opposite.
Since the last level of both the menus are duplicated titles, I am having some difficulties:
1.) Setting "Name" for example to 'nil' removes BOTH "Sort By->Name" and "Arrange By->Name".
2.) Setting "Name" to something else only sets "Arrange By->Name".
3.) Setting a key for "View->Sort By->Name" (using dash followed by greater-than) does nothing. Same if I omit the "View->" at the beginning. I also tried using just ">" (greater-than) to get SubMenus, but no success.
3.1) I tried various ways of quoting the string, and got it to successfully read back with 'defaults read':
4.) I could not get these shortcuts to work using the System Preferences>Keyboard Shortcut tool either; Finder seems to override those in this case.
Any help would be much appreciated.
Hark Thrice (JG) at 5:03 p.m. May 7th, 2014
hf0Vl