
Programmers love dotfiles, the configuration files that tune everything to our liking. We want to have dotfiles for everything: git
, zsh
, vim
, etc. It’s only natural that we’d also want a dotfile for our Mac system preferences.
It’s not so straightforward to set up a Mac dotfile though. There isn’t an rc file where everything goes, and you have to tweak the system bit by bit to get everything right.
However, after changing machines for the fourth time in a year, I think my compulsive efforts to set a dotfile up were well worth it. In this blog, I will walk you through how to set up one yourself.
Finding out the settings
How does one go about starting to investigate your system preferences? I learned from this 10-year-old answer on superuser from Mathias Bynens about the defaults
tool. You can use it to export all your system preferences, make some changes via the regular System Preferences GUI program, export again, and then compare the differences in the output.
defaults read > a
# Change the setting
defaults read > b
diff a b
For example, if your General > Appearance was set to Auto, and then you changed it to Dark, the diff
output would look something like this:
17c17
< AppleInterfaceStyleSwitchesAutomatically = 1;
---
> AppleInterfaceStyle = Dark;
97,98c97,98
< "_DKThrottledActivityLast_DKKnowledgeStorageLogging_DKKnowledgeStorageDidInsertEventsNotification:/app/usageActivityDate" = "2023-05-17 08:28:04 +0000";
< "_DKThrottledActivityLast_DKKnowledgeStorageLogging_DKKnowledgeStorageDidInsertLocalEventsNotification:/app/usageActivityDate" = "2023-05-17 08:28:04 +0000";
---
> "_DKThrottledActivityLast_DKKnowledgeStorageLogging_DKKnowledgeStorageDidInsertEventsNotification:/app/usageActivityDate" = "2023-05-17 08:28:11 +0000";
> "_DKThrottledActivityLast_DKKnowledgeStorageLogging_DKKnowledgeStorageDidInsertLocalEventsNotification:/app/usageActivityDate" = "2023-05-17 08:28:11 +0000";
The last two parts look obviously irrelevant to our goal. You can then do some guesswork and figure out that the first two parts are precisely what we’re after. They look self-explanatory and sensible enough:
AppleInterfaceStyleSwitchesAutomatically
sets if the UI switches automatically and AppleInterfaceStyle
sets which style is used.
If you open the files to look closer, you can see that they’re plist files. To look for the keys above, we have, before:
{
...
"Apple Global Domain" = {
...
AppleInterfaceStyleSwitchesAutomatically = 1;
...
};
...
}
and, after:
{
...
"Apple Global Domain" = {
...
AppleInterfaceStyle = Dark;
...
};
...
}
Note down these values because we’ll need them later.
Remembering the settings
We know the internal values. Next, we have to find a way to reset them accordingly. We can again use the defaults
tool for this. Run the write
command after defaults
like so:
defaults write Apple\ Global\ Domain AppleInterfaceStyle -string "Dark"
After this, we must log out and log in for this to take effect.
Since we can use commands to set our system preferences, we can store these commands in a script and use it as our dotfile, so we are done!
Not everything can be set in this way. For example, I couldn’t find anything that made sense in changing the caps lock key to the control key in the diff output.
Another thing is that some preferences need not the logout-and-login routine to take effect. According to this answer by Zameer Manji on Ask Different, you may run:
/System/Library/PrivateFrameworks/SystemAdministration.framework/Resources/activateSettings -u
Conclusion
Note that this is very much a “hack” and not an officially supported method of backing up settings, so things could break very easily. For example, the preferences setting GUI program used to be called “System Preferences” but, since Ventura, is now called “System Settings”, and this change broke my configuration.
This method is far from perfect. You have to painstakingly go through every single bit of option and note down the values. Yet it showed its value when I didn’t have to manually set everything every time I changed machines.
You can check out all my personal preferences here. I myself use Makefiles to organize my dotfiles so the preferences are written in Makefiles too, but they should be easy enough to read.