zsh history
Zsh History: The Complete Guide to Efficient Command Recall
Key Takeaways
- ✅ Zsh stores command history in
~/.zsh_historywith timestamps and exit codes by default. - ✅ Enabling
setopt EXTENDED_HISTORYandsetopt SHARE_HISTORYgives richer context and real‑time sharing across sessions. - ✅
HISTSIZEandSAVEHISTcontrol in‑memory and on‑disk limits; set them high for a deep audit trail. - ✅ Use
zshaddhistoryhook orfc -R/fc -Wto import/export history programmatically. - ✅ A well‑tuned
.zshrceliminates duplicate entries, respects case‑insensitive search, and speeds upCtrl‑Rfuzzy lookup.
Common Pain Points & Solutions
1. History Truncates Too Quickly
By default Zsh keeps only 30 000 lines in memory and writes 30 000 lines to disk. If you need a longer audit trail, increase HISTSIZE (in‑memory) and SAVEHIST (on‑disk) in .zshrc:
HISTSIZE=100000
SAVEHIST=100000
2. Duplicate Commands Flood the List
Enable setopt HIST_IGNORE_ALL_DUPS to keep only the most recent instance of a command.
3. Inconsistent History Across Multiple Terminals
Activate setopt SHARE_HISTORY so each session writes to the history file immediately and reads new entries from other sessions.
4. Searching is Slow or Case‑Sensitive
Combine setopt HIST_FIND_NO_DUPS with bindkey '^R' history-incremental-search-backward and zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' for case‑insensitive, incremental search.
Configuration Comparison
| Feature | Default Zsh | Enhanced Setup (Recommended) |
|---|---|---|
| History Size (memory) | 30 000 | 100 000 (HISTSIZE=100000) |
| History Size (file) | 30 000 | 100 000 (SAVEHIST=100000) |
| Timestamp & Exit Code | Disabled | Enabled (setopt EXTENDED_HISTORY) |
| Duplicate Removal | None | Enabled (setopt HIST_IGNORE_ALL_DUPS) |
| Cross‑session Sync | None | Enabled (setopt SHARE_HISTORY) |
| Case‑Insensitive Search | Disabled | Enabled via zstyle matcher |
Quick‑Start Checklist for Optimized Zsh History
- ☑️ Add
setopt EXTENDED_HISTORYfor timestamps. - ☑️ Add
setopt SHARE_HISTORYto sync sessions. - ☑️ Add
setopt HIST_IGNORE_ALL_DUPSto prune duplicates. - ☑️ Set
HISTSIZEandSAVEHISTto values that match your workflow. - ☑️ Configure case‑insensitive incremental search:
bindkey '^R' history-incremental-search-backward
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
fc -R (reload) and fc -W (write).Frequently Asked Questions
- How can I view the timestamp of a command in my history?
- Enable
EXTENDED_HISTORYand then runfc -l -Dorhistory -i. The output will include the start time and duration of each command. - My
Ctrl‑Rsearch feels sluggish after many sessions. What can I do? - Reduce the history file size, or split it using
HISTFILE=~/.zsh_history_smallfor temporary sessions. Also, ensuresetopt HIST_IGNORE_SPACEto skip accidental leading‑space entries that bloat the file. - Can I share my Zsh history with other users on the same machine?
- Yes. Place a shared history file in a common directory (e.g.,
/var/shared_zsh_history) and setHISTFILEto that path in each user's.zshrc. Combine withsetopt SHARE_HISTORYfor real‑time updates.