“defaults” command tricks

“defaults” command tricks

If you use the defaults command to set preferences, it’s easy to forget that you ever did. Some implications of this:

  • You could be filling up your drive with logs or slowing your system down
  • You could run into other bugs later that are caused by the default itself
  • An update to an app might not benefit you because the default you set is overriding behavior

Finding defaults

The defaults command has a find subcommand, but I’ve found that people generally aren’t aware of it. Let’s say you set some defaults relating to your trackpad, but don’t know what domain they are in:

defaults find trackpad

Found 3 keys in domain 'Apple Global Domain': {
    "com.apple.trackpad.forceClick" = 0;
    "com.apple.trackpad.scaling" = 3;
    "com.apple.trackpad.scrolling" = "0.75";
}
Found 3 keys in domain 'com.apple.AppleMultitouchTrackpad': {
    Clicking = 0;
    DragLock = 0;
    Dragging = 0;
    FirstClickThreshold = 2;
    ForceSuppressed = 1;
}
Found 1 keys in domain 'com.apple.preference.trackpad': {
    ForceClickSavedState = 0;
}
Found 1 keys in domain 'com.apple.systempreferences': {
    "trackpad.lastselectedtab" = 0;
}

Now you can use “defaults delete” to delete the offending keys or “defaults write” to change them. For the global domain, use “-g” as the domain.

Disabling but not deleting a default

Sometimes you have a default that you want to remember, but you want to disable it for now. One nice way to do this is to use therename command.

defaults rename com.apple.screencapture location _disabled_location

Then, you can just rename it back later rather than deleting it and having to find the default you need to set again. Then, you can usedefaults find _disabled_ and you can find anything you’ve disabled, provide you use a unique prefix.

If it’s a boolean default, you can just set it to -bool false or whatever the opposite setting is.

Keeping track of all defaults

This might be kind of a dumb idea, but add some functions to my .bashrc to track my history of adding defaults.

function defaults-write { defaults write com.my-defaults all -array-add "defaults write $*"; }
export -f defaults-write
function defaults-read { defaults read com.my-defaults all; }
export -f defaults-read
function defaults-delete { defaults delete com.my-defaults; }
export -f defaults-delete

Then I use defaults-write instead of “defaults write” when I want to set a default. This sets the default and writes the command to a domain called com.my-defaults — this can be read easily with defaults-read and the whole domain can be deleted with defaults-delete

Here’s how it works:

defaults-write defaults write com.apple.screencapture foo bar

then:

defaults-read

This outputs:

(     
"defaults write com.apple.screencapture foo bar"
)

This is very primitive but you could imagine making it more powerful. Perhaps it could work like darwinup and keep a track of all defaults commands and then they could all be reversible, since it could keep track of the setting before the defaults command was run.