Persisting Zsh Configuration
Introduction: Making Shortcuts Survive the Window
Welcome back to Homebrew and Your zsh Environment. Unit 4 ended on a deliberate cliffhanger: every alias we defined lived in the memory of one running shell, so closing the window erased all of it. Unit 5 fixes that, and it is the fifth of six units, so the finish line is in sight.
The file that makes settings permanent is ~/.zshrc: zsh reads it automatically for every new interactive shell. That convenience comes with a responsibility, which is the guiding principle of this unit: ~/.zshrc is shared territory. Homebrew, a plugin manager, or a past version of ourselves may have put lines there, so we never overwrite it blindly.
The work ahead follows four moves:
- Back up the current
~/.zshrcbefore touching it. - Store personal settings in a file we own outright,
~/.zshrc.local. - Connect the two with exactly one
sourceline. - Load the result into the shell we are already in.
Backing Up Before Touching Anything
Before making any configuration changes, prepare a workspace and take a copy.
Each command earns its place:
mkdir -pcreates theshell-setupfolder and stays quiet if it already exists, so it is safe to repeat.touch ~/.zshrcguarantees that the file exists. Not every Mac has a~/.zshrcyet, andcpfails when its source is missing;touchleaves an existing file's contents completely alone.cpthen saves the original aszshrc-backup, andlsconfirms that it landed.
The backup is the first move, not an afterthought: if anything later goes wrong, one cp in the other direction restores the original.
Giving Personal Settings Their Own File
Rather than typing aliases directly into shared configuration, we give them a file of their own, written with a heredoc.
The shell shows a continuation prompt after the first line and keeps reading until it sees EOF alone on a line. As you may recall from the text course, <<'EOF' with a quoted delimiter turns off the substitutions the shell would otherwise perform on the body: $name parameters, $(...) command substitution, and backslash escapes all reach the file as plain text.
Without the quotes, a setting containing $ would be expanded at write time instead of stored for zsh to interpret at load time. The first two aliases are the ones from Unit 4, now on disk instead of in memory; the third makes that rule visible.
Check the result:
$HOME survived as four literal characters. With an unquoted <<EOF, that line would have been frozen into your home path at the moment the file was written.
The design idea matters more than the syntax. Since ~/.zshrc.local holds only our settings, replacing it wholesale with > is safe and repeatable; doing that to ~/.zshrc would destroy lines that other tools depend on.
