Skip to content

A macOS .zshrc example, annotated line by line

This is the .zshrc I use on my Mac, all 62 lines of it: Oh My Zsh, paths for Homebrew and the SDKs I build with, a few git aliases and shared history. The file below is read from the repository when this site builds, so it is always the current version. The notes beside it say what each block does and why you might want it.

Use this fileSource on GitHub

~/.zshrc

62 lines, zshOpen on GitHub

Oh My Zsh, the agnoster theme and one plugin

ZSH tells Oh My Zsh where it is installed. Everything above the source line is configuration that the framework reads when it loads, so the theme and plugin list have to come first.

agnoster draws the prompt as coloured segments with the current git branch. It needs a Powerline-patched font in the terminal, otherwise the segment separators show up as boxes.

The plugin list has a single entry, git. It ships with Oh My Zsh and adds git aliases and completion. There are no third-party plugins here, so nothing extra has to be cloned.

export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="agnoster"
plugins=(git)
source $ZSH/oh-my-zsh.sh

Locale

Pins the shell to US English with UTF-8. LC_ALL overrides every other LC_* setting, so command output, sorting and date formats stay the same whatever the system language is. UTF-8 also keeps the prompt glyphs intact.

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

PATH for Homebrew, Cargo and local binaries

Each line puts a directory at the front of PATH, so the last line wins when two directories hold a command with the same name.

/opt/homebrew/bin is where Homebrew installs on Apple Silicon Macs. ~/.cargo/bin holds binaries installed with Rust's cargo install. ~/.local/bin is a common home for tools installed for one user.

# brew
export PATH="/opt/homebrew/bin:$PATH"
export PATH="$HOME/.cargo/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"

pnpm, added once

PNPM_HOME is the directory pnpm uses for its own binary and for globally installed packages.

The case statement checks whether that directory is already in PATH and only prepends it when it is missing. Without the check, every reload would add the same entry again.

# pnpm — guard against double-adding to PATH on reload
export PNPM_HOME="$HOME/Library/pnpm"
case ":$PATH:" in
*":$PNPM_HOME:"*) ;;
*) export PATH="$PNPM_HOME:$PATH" ;;
esac

Android SDK

Points at the SDK that Android Studio installs under ~/Library on a Mac. ANDROID_HOME is the current variable name. ANDROID_SDK_ROOT is the older one, kept because some build tools still read it.

Adding platform-tools to PATH makes adb and fastboot available in every shell.

# android
export ANDROID_HOME="$HOME/Library/Android/sdk"
export ANDROID_SDK_ROOT="$ANDROID_HOME"
export PATH="$ANDROID_HOME/platform-tools:$PATH"

Java

/usr/libexec/java_home is a macOS tool that prints the path of the default installed JDK. Build tools such as Gradle read JAVA_HOME to find Java.

That tool only exists on macOS, so on Linux this line prints an error at every shell start. The PATH line uses /usr/local, the Homebrew prefix on Intel Macs. On Apple Silicon, Homebrew keeps OpenJDK under /opt/homebrew/opt/openjdk.

# java from brew openjdk
export PATH="/usr/local/opt/openjdk/bin:$PATH"
export JAVA_HOME=$(/usr/libexec/java_home)

pyenv, only when installed

command -v pyenv succeeds only when pyenv is on PATH. On a machine without it the block is skipped and the shell starts with no error.

pyenv init - prints shell code that puts pyenv's shims first in PATH, so python resolves to the version pyenv selected. eval runs that code in the current shell.

# only init pyenv if it's actually installed (skips on fresh machines)
if command -v pyenv >/dev/null 2>&1; then
eval "$(pyenv init -)"
fi

gvm for Go versions

gvm is the Go Version Manager. [ -s file ] is true when the file exists and is not empty, so the script is sourced only on machines where gvm is installed. It is the same guard as the pyenv block, written on one line.

# gvm
[ -s "$HOME/.gvm/scripts/gvm" ] && source "$HOME/.gvm/scripts/gvm"

Shell shortcuts

cls clears the screen, a habit carried over from Windows. zshconfig opens this file in nano. reload sources it again, so an edit takes effect without opening a new terminal tab.

# shortcuts
alias cls="clear"
alias zshconfig="nano ~/.zshrc"
alias reload="source ~/.zshrc"

Git aliases

Four short names for the commands typed most often. gc ends with -m, so the message follows directly: gc "fix typo".

These lines come after Oh My Zsh has loaded, so they replace the git plugin's own ga, gc and gp. The aliases page lists what each one expands to.

alias gs="git status"
alias ga="git add ."
alias gc="git commit -m"
alias gp="git push"

neofetch on startup

Prints a system summary with the OS logo each time a terminal opens. It uses the same command -v guard as pyenv, so a Mac without neofetch stays quiet.

# neofetch on startup if available
if command -v neofetch >/dev/null 2>&1; then
neofetch
fi

Shared history

zsh keeps 5000 commands in memory and writes 5000 to ~/.zsh_history.

HIST_IGNORE_DUPS skips a command when it repeats the one just before it. SHARE_HISTORY makes every open terminal read and write the same history file as commands run, so a command typed in one tab can be recalled in another.

HISTSIZE=5000
SAVEHIST=5000
HISTFILE=~/.zsh_history
setopt HIST_IGNORE_DUPS
setopt SHARE_HISTORY

Completion

compinit starts zsh's completion system, the thing that fills in commands, flags and file names when you press Tab. autoload -Uz loads the function without applying any aliases to it.

autoload -Uz compinit
compinit

Use it on your Mac

Install zsh and Oh My Zsh first. The paths in the file assume macOS, with Homebrew in /opt/homebrew and SDKs under ~/Library. pyenv, gvm and neofetch are skipped when they are not installed.

Back up your own file first. ln -sf replaces ~/.zshrc without asking. The second command saves a copy as ~/.zshrc.backup. Check that the copy exists before you run the third. If you have no ~/.zshrc yet, cp reports that there is nothing to copy and you can carry on.

To go back, remove the symlink and restore the copy: rm ~/.zshrc && mv ~/.zshrc.backup ~/.zshrc.

Terminal
  1. git clone https://github.com/halitsever/dotfiles.git ~/dotfiles

    Clones the repository to ~/dotfiles.

  2. cp ~/.zshrc ~/.zshrc.backup

    Copies your current ~/.zshrc to ~/.zshrc.backup.

  3. ln -sf ~/dotfiles/.zshrc ~/.zshrc

    Replaces ~/.zshrc with a symlink to the file in the clone, so a git pull updates your shell config.

  4. source ~/.zshrc

    Loads the new config into the shell you already have open.

Background reading

Released under the MIT License. Oh My Zsh, Homebrew and the other tools named here belong to their own projects.