hkumar@portfolio:~/blog/steal-my-zsh-config$
Steal my zsh config
A few weeks ago I ran an rm -rf in the wrong place and lost most of my home folder. My shell came back in about five minutes, because it lived in a dotfiles repo and not only in ~.
So here it is: the whole setup, section by section, with a note on why each line is there. Take what you like. Everything is for macOS with Homebrew, but most of it works on Linux too.
What you get
- Fuzzy everything. Ctrl+T finds files, Ctrl+R searches history, ⌥+C jumps into folders, all with live previews.
- A
cdthat learns. Typecd projand land in~/Projects, from anywhere. - Better defaults.
ls,cat,grepandfindswapped for faster, nicer tools. - Readable git diffs, side by side with syntax highlighting.
- One theme everywhere. Tokyo Night in the terminal,
bat, and the previews.
Install the tools
Everything comes from Homebrew. Put this in a Brewfile and run brew bundle:
brew "bat" # cat with syntax highlighting
brew "btop" # a nicer top
brew "eza" # ls with icons, git status and trees
brew "fd" # fast find
brew "fzf" # fuzzy finder
brew "git-delta" # pretty git diffs
brew "ripgrep" # fast grep
brew "tlrc" # tldr: man pages with examples
brew "zoxide" # cd that remembers
brew "zsh-autosuggestions"
brew "zsh-syntax-highlighting"
cask "ghostty"
cask "font-meslo-lg-nerd-font" # icons for eza
Then two things Homebrew doesn't cover, oh-my-zsh and fzf-git:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
git clone https://github.com/junegunn/fzf-git.sh.git ~/fzf-git.sh
The base
eval "$(/opt/homebrew/bin/brew shellenv)"
This goes in .zprofile, not .zshrc, so login shells get Homebrew's PATH once. It also sets $HOMEBREW_PREFIX, which the plugin lines further down rely on.
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="robbyrussell"
plugins=(git)
source $ZSH/oh-my-zsh.sh
export LANG=en_US.UTF-8
I keep oh-my-zsh deliberately thin: the default theme and the git plugin, for aliases like gst and gco. Every extra plugin costs startup time, so the rest of the setup uses standalone tools instead.
LANG matters more than it looks. CocoaPods errors out without a UTF-8 locale, which you'll hit quickly if you build iOS apps.
Navigation: zoxide
eval "$(zoxide init zsh)"
alias cd="z"
alias ci="zi"
alias ..="cd .."
alias ...="cd ../.."
zoxide remembers the folders you visit and ranks them by how often and how recently you went there. After a day, cd portfolio takes you to ~/Projects/my-portfolio from anywhere. Normal paths still work, so aliasing cd to z costs nothing.
ci opens the same list in fzf when you don't remember the name.
Fuzzy finding: fzf
This is the part I'd keep if I could only keep one.
# Use fd instead of find: faster, respects .gitignore, shows dotfiles
export FZF_DEFAULT_COMMAND="fd --hidden --strip-cwd-prefix --exclude .git"
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_ALT_C_COMMAND="fd --type=d --hidden --strip-cwd-prefix --exclude .git"
# Previews: file contents for Ctrl-T, a folder tree for Alt-C
export FZF_CTRL_T_OPTS="--preview 'bat -n --color=always --line-range :500 {}'"
export FZF_ALT_C_OPTS="--preview 'eza --tree --color=always {} | head -200'"
source <(fzf --zsh)
That gives you three shortcuts:
| Key | What it does |
|---|---|
| Ctrl+T | Pick a file and paste its path into the command you're typing |
| Ctrl+R | Search your shell history |
| ⌥+C | Pick a folder and cd into it |
⌥+C types ç on a Mac unless your terminal treats Option as Alt. The Ghostty section below fixes that.
fzf also hooks into tab completion. Type ** and press Tab:
_fzf_compgen_path() { fd --hidden --follow --exclude ".git" . "$1"; }
_fzf_compgen_dir() { fd --type d --hidden --follow --exclude ".git" . "$1"; }
# A different preview depending on the command
_fzf_comprun() {
local command=$1
shift
case "$command" in
cd) fzf --preview 'eza --tree --color=always {} | head -200' "$@" ;;
export|unset) fzf --preview "eval 'echo \$'{}" "$@" ;;
ssh) fzf --preview 'dig {}' "$@" ;;
*) fzf --preview 'bat -n --color=always --line-range :500 {}' "$@" ;;
esac
}
Now cd **<Tab> shows folder trees, export **<Tab> shows each variable's value, ssh **<Tab> resolves the host, and anything else (vim **<Tab>) previews the file.
Last, fzf-git adds pickers for branches, commits, tags and changed files. Ctrl+G then Ctrl+B lists branches, for example:
source ~/fzf-git.sh/fzf-git.sh
Better defaults
# eza
alias ls="eza --grid --color=auto --icons=auto --group-directories-first"
alias ll="eza -l --header --git --octal-permissions"
alias la="eza -l --all --header --git --group-directories-first"
alias lt="eza --tree --long --git --level=2"
alias l1="eza -1 --color=auto"
# swaps
alias cat="bat"
alias grep="rg"
alias find="fd"
alias htop="btop"
# small things
alias c="clear"
alias gshort="git shortlog -s -n | bat" # commits per author
mkcd() { mkdir -p "$1" && cd "$1"; }
ll and la show each file's git status next to it, which I use more than I expected. lt is a two-level tree, handy for a first look at an unfamiliar repo.
A warning about the swaps: rg and fd don't take the same flags as grep and find. Aliases only apply in your interactive shell, so scripts are safe, but commands pasted from Stack Overflow can surprise you. Run command grep or \grep when you need the real one.
Syntax highlighting with bat
bat and the fzf previews use Tokyo Night. bat doesn't ship that theme, so download it and rebuild bat's cache:
mkdir -p "$(bat --config-dir)/themes"
curl -fL -o "$(bat --config-dir)/themes/tokyonight_night.tmTheme" \
https://raw.githubusercontent.com/folke/tokyonight.nvim/main/extras/sublime/tokyonight_night.tmTheme
bat cache --build
export BAT_THEME="tokyonight_night"
Keep the theme file in your dotfiles repo as well. bat only reads its compiled cache, so if you lose the file, nothing changes until the next bat cache --build. Then the theme quietly falls back to the default.
History
HISTFILE="$HOME/.zhistory"
HISTSIZE=20000 # kept in memory; keep it above SAVEHIST
SAVEHIST=10000 # saved to disk
setopt append_history # add to the file instead of overwriting it
setopt hist_ignore_dups # skip a command identical to the previous one
setopt hist_expire_dups_first # when trimming, drop duplicates first
setopt hist_reduce_blanks # tidy extra spaces
setopt hist_verify # !! and !$ expand for editing instead of running
hist_verify has saved me more than once. sudo !! shows you the expanded command first, so you can check it before pressing Enter.
Plugins go last
source $HOMEBREW_PREFIX/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source $HOMEBREW_PREFIX/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
Autosuggestions show the rest of a command from your history in grey; → accepts it. Syntax highlighting turns a command red before you run it if it doesn't exist.
Order matters here: zsh-syntax-highlighting has to be sourced at the very end of .zshrc, after anything else that changes the line editor.
React Native extras
If you build Android apps, React Native needs JDK 17 and the Android SDK on your PATH:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator:$ANDROID_HOME/platform-tools:$ANDROID_HOME/cmdline-tools/latest/bin
alias studio='open -a "Android Studio"'
The JDK comes from brew install --cask zulu@17, which is the one the React Native docs recommend.
The terminal: Ghostty
theme = TokyoNight
font-family = "MesloLGS Nerd Font Mono"
font-size = 16
window-padding-x = 12
window-padding-y = 12
background-opacity = 0.96
background-blur = 1
shell-integration-features = sudo,ssh-env
# Left Option acts as Alt (for fzf's Alt-C); right Option still types ç, € and friends
macos-option-as-alt = left
Ghostty is fast and native, and its defaults are good enough that this is the whole file. Three of these lines do real work:
macos-option-as-alt = leftmakes ⌥+C work. Using only the left key keeps the right one free for typing special characters.- The font name has to match exactly. Ghostty quietly falls back to its built-in font when it can't find yours, and that font has icons too, so you may not notice.
ghostty +list-fontsshows the real names. ssh-envreports a standard terminal type over SSH, so servers that have never heard of Ghostty don't breakclearorhtop.
Git diffs: delta
[core]
pager = delta
[interactive]
diffFilter = delta --color-only
[delta]
navigate = true # n / N jump between diff sections
side-by-side = true
[merge]
conflictstyle = diff3
[diff]
colorMoved = default
git diff, git show and git log -p all become side-by-side and syntax-highlighted. diff3 adds the original version to merge conflicts, so you can see what both sides changed, not just where they ended up.
Keep it in a repo
This is the lesson from that rm -rf. Keep these files in a git repo and symlink them into ~:
~/dotfiles/
.zshrc .zprofile .gitconfig Brewfile
.config/ghostty/config
.config/bat/themes/tokyonight_night.tmTheme
A twenty-line script that runs ln -s for each file is enough; you don't need a framework. On a new Mac you clone the repo, run the script and brew bundle, and your shell is back.
Credit
Most of the CLI tool setup started from Josean Martinez's 7 Amazing CLI Tools video, which is worth watching if you want to see these tools in action. The aliases, history settings and the rest are my own.