histprune is a shell history cleaner written in Go. The first version focuses on zsh history and aims to make cleanup safe, explainable, and recoverable.
- First-class zsh history support, including extended history, multiline commands, and zsh-metafied command decoding
- Dry-run by default
- Reasoned removals for every dropped entry
- Duplicate removal while keeping the latest occurrence
- Literal, regex, line-number, and timestamp-range pruning
- Automatic backups before writes
- Backup listing and restore support
- Text and JSON reports
Download a release archive from GitHub Releases and unpack it:
tar -xzf histprune_<tag>_<os>_<arch>.tar.gz
./histprune --helpOr install from source with Go:
go install github.com/walker1211/histprune/cmd/histprune@latest
histprune --helpFor local development, you can build a binary with the helper script:
bash ./build.sh
./histprune --helpAnalyze your default zsh history file:
histprune analyzePreview cleanup first:
histprune prune --dedupeApply the same cleanup after reviewing the preview:
histprune prune --dedupe --writeBy default, histprune uses $HISTFILE, then falls back to ~/.zsh_history. Use --file PATH only when targeting another history file:
histprune analyze --file /path/to/historyShow all commands and flags:
histprune --helpAll prune commands are previews unless --write is explicitly passed. Review the report first, then add --write to the same command to modify the file.
Remove duplicates while keeping the latest occurrence:
histprune prune --dedupeDrop entries containing literal text:
histprune prune --contains 'gti status'Drop entries matching a regex:
histprune prune --regex 'token=[^ ]+'Drop by line number:
histprune prune --line 1287Drop by date:
histprune prune --before 2024-01-01
histprune prune --between 2024-01-01 2024-06-30Emit a JSON report:
histprune prune --dedupe --jsonprune is a dry-run unless --write is explicitly passed.
On write, histprune will:
- read and parse the history file
- build removal decisions with reasons
- create a
*.histprune-backup-*backup first - replace the target through a temporary file and rename
After changing the history file used by your current zsh session, reload it manually:
fc -R ~/.zsh_historyIf multiple zsh terminals are open, consider writing the latest in-memory history before cleanup:
fc -W ~/.zsh_historyhistprune does not require changing your zsh configuration, but these settings make the history file easier to clean later:
# zsh history
HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000
setopt EXTENDED_HISTORY # Store timestamps
setopt APPEND_HISTORY # Append to the history file
setopt INC_APPEND_HISTORY # Write each command immediately after it runsEXTENDED_HISTORY stores timestamps, making --before / --between range rules more useful. APPEND_HISTORY and INC_APPEND_HISTORY reduce the chance of multiple terminals overwriting each other's history and keep the file closer to the current shell state.
If you want zsh to reduce future duplicate history entries on its own, you can also enable:
setopt HIST_IGNORE_DUPS # Do not record consecutive duplicate commands
setopt HIST_IGNORE_ALL_DUPS # Remove older duplicates when a new duplicate is added
setopt HIST_SAVE_NO_DUPS # Do not write duplicate entries when saving history
setopt HIST_EXPIRE_DUPS_FIRST # Expire duplicate entries first when history is full
setopt HIST_FIND_NO_DUPS # Hide duplicates when searching history
setopt HIST_REDUCE_BLANKS # Compress extra whitespace
setopt HIST_IGNORE_SPACE # Do not record commands that start with a spaceThese options do not guarantee that ~/.zsh_history will never contain duplicates. Existing entries, history written before the configuration took effect, and concurrent writes from multiple terminals may still be reported by histprune prune --dedupe. Treat the zsh configuration as a way to reduce new duplicates, while histprune provides previewable, backed-up, and recoverable cleanup for the history file on disk.
List backups:
histprune backupsRestore the latest backup:
histprune restore latestRestore an explicit backup:
histprune restore /path/to/.zsh_history.histprune-backup-20260504T120000Restoring creates a backup of the current history file first.
bash ./scripts/ci-local.sh- bash / fish history support
- config-file rules
- backup retention
- secret redaction
- typo / failed-command analysis
- Homebrew packaging
Licensed under the MIT License. See LICENSE.