Ardrey 情報マガジン

zsh history

📅 November 7, 2026📖 Verified Publication
Zsh History: Mastering Command Recall for Faster Shell Workflows

Zsh History: The Complete Guide to Efficient Command Recall

Key Takeaways

  • ✅ Zsh stores command history in ~/.zsh_history with timestamps and exit codes by default.
  • ✅ Enabling setopt EXTENDED_HISTORY and setopt SHARE_HISTORY gives richer context and real‑time sharing across sessions.
  • ✅ HISTSIZE and SAVEHIST control in‑memory and on‑disk limits; set them high for a deep audit trail.
  • ✅ Use zshaddhistory hook or fc -R/fc -W to import/export history programmatically.
  • ✅ A well‑tuned .zshrc eliminates duplicate entries, respects case‑insensitive search, and speeds up Ctrl‑R fuzzy 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_HISTORY for timestamps.
  • ☑️ Add setopt SHARE_HISTORY to sync sessions.
  • ☑️ Add setopt HIST_IGNORE_ALL_DUPS to prune duplicates.
  • ☑️ Set HISTSIZE and SAVEHIST to 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}'
    
  • ☑️ Periodically clean up with fc -R (reload) and fc -W (write).

Frequently Asked Questions

How can I view the timestamp of a command in my history?
Enable EXTENDED_HISTORY and then run fc -l -D or history -i. The output will include the start time and duration of each command.
My Ctrl‑R search feels sluggish after many sessions. What can I do?
Reduce the history file size, or split it using HISTFILE=~/.zsh_history_small for temporary sessions. Also, ensure setopt HIST_IGNORE_SPACE to 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 set HISTFILE to that path in each user's .zshrc. Combine with setopt SHARE_HISTORY for real‑time updates.