Zsh History

History Configuration

Essential history settings
HISTFILE=~/.zsh_history
HISTSIZE=50000                           # in-memory history
SAVEHIST=50000                           # saved to file

# Key options
setopt EXTENDED_HISTORY                  # timestamp + duration
setopt HIST_EXPIRE_DUPS_FIRST           # expire dupes first when trimming
setopt HIST_IGNORE_DUPS                 # don't store consecutive dupes
setopt HIST_IGNORE_ALL_DUPS             # remove older dupe when new added
setopt HIST_IGNORE_SPACE                # commands starting with space not saved
setopt HIST_FIND_NO_DUPS                # skip dupes in search
setopt HIST_REDUCE_BLANKS               # remove extra whitespace
setopt HIST_VERIFY                      # show expanded history before executing
setopt SHARE_HISTORY                    # share history across sessions
setopt INC_APPEND_HISTORY               # append immediately (not on exit)

History Expansion

Bang commands
!!                                       # repeat last command
sudo !!                                  # run last command with sudo

!$                                       # last argument of previous command
!^                                       # first argument of previous command
!*                                       # all arguments of previous command

!-2                                      # two commands ago
!ssh                                     # last command starting with "ssh"
!?error                                  # last command containing "error"

# Modifiers
!!:s/old/new                             # substitute in last command
!!:gs/old/new                            # global substitute
!$:h                                     # head (dirname) of last arg
!$:t                                     # tail (basename) of last arg
!$:r                                     # remove extension
!$:e                                     # extension only

# Examples
vim /etc/ssh/sshd_config
# !$:h  → /etc/ssh
# !$:t  → sshd_config
Searching history
# Interactive search
# Ctrl-r                                 # reverse incremental search
# Ctrl-s                                 # forward incremental search

# History command
history                                  # show recent history
history -10                              # last 10 entries
history 1                                # all history from entry 1
fc -l -20                                # last 20 with timestamps

# Grep history
history | grep ssh                       # basic search
fc -l 1 | awk '/ssh/ && /vault/'         # complex search

# fzf history search (if installed)
# Ctrl-r with fzf integration gives fuzzy search

History Tricks

Advanced history usage
# Prevent specific commands from being saved
# Prefix with space (requires HIST_IGNORE_SPACE)
 export SECRET_TOKEN="abc123"            # note leading space

# Edit and re-execute
fc                                       # open last command in $EDITOR
fc -e nvim -5                            # edit last 5 commands

# Per-directory history (plugin-based)
# Many oh-my-zsh plugins and zinit provide this

# History statistics
awk '{print $1}' ~/.zsh_history | sort | uniq -c | sort -rn | head -20

# Clean history file
# Remove specific entries
grep -v "SECRET" ~/.zsh_history > /tmp/clean_hist
mv /tmp/clean_hist ~/.zsh_history
fc -R                                    # reload history
Up/down arrow prefix search
# Type partial command, then up-arrow searches history
# matching what you've typed

autoload -Uz up-line-or-beginning-search
autoload -Uz down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search

bindkey "^[[A" up-line-or-beginning-search    # Up arrow
bindkey "^[[B" down-line-or-beginning-search  # Down arrow

# Example: type "git" then Up → cycles through git commands only