After Editing zsh History, Check the Running Shell Too

You edit a command in the history file, inspect the saved text, and find the old command still listed by the open terminal. Checking the file again will not explain that mismatch: zsh also keeps an in-memory history list.

Before trusting a history-editing tool, check what the running shell lists after the edit and what its undo operation restores. The distinction is small enough to demonstrate without touching your real history.

A file path and a history list can disagree

This demonstration starts a separate zsh -f process, unsets HISTFILE, and creates two temporary fixture files. History persistence is disabled with SAVEHIST=0. It never loads or rewrites your real history, and fc -ln lists the fixture commands without executing them.

zsh -f <<'ZSH'
set -eu
unset HISTFILE
HISTSIZE=20
SAVEHIST=0
demo_dir=$(mktemp -d) || exit 1
trap 'rm -f -- "$demo_dir/before" "$demo_dir/after"; rmdir -- "$demo_dir"' EXIT

print -r -- 'echo old-name' > "$demo_dir/before"
print -r -- 'echo new-name' > "$demo_dir/after"
fc -p "$demo_dir/before" 20 0

HISTFILE="$demo_dir/after"
print -r -- 'File selected by HISTFILE:'
cat -- "$HISTFILE"
print -r -- 'Current history list:'
fc -ln 1

fc -p "$HISTFILE" 20 0
print -r -- 'New history level:'
fc -ln 1
fc -P
print -r -- 'Previous level:'
fc -ln 1
ZSH

On zsh 5.9, the output was:

File selected by HISTFILE:
echo new-name
Current history list:
echo old-name
New history level:
echo new-name
Previous level:
echo old-name

Changing HISTFILE did not replace the loaded list. fc -p created a new history level and read the selected file; fc -P restored the previous level, including its old text. Those stack operations are documented in the official zsh manual source.

This is a demonstration of separate state and history levels. It does not simulate concurrent terminals or establish that a replacement tool handles them safely.

What a replacement tool has to reconcile

zsh-histfix previews literal or regex replacements and supports undo. Its plugin implementation takes different paths depending on SHARE_HISTORY:

  • Without sharing, it snapshots the current shell's events and asks the helper to map them to disk records before applying a change. The mapping code refuses memory-only events, ambiguous duplicates, and incomplete snapshots.
  • With sharing, it starts a new level with fc -p after a change. The implementation explains that fc -R cannot reset zsh's shared-history read position, so a plain read would leave the wrong import state. If a lock directory remains after the write, it skips that reload and reports the condition instead.

SHARE_HISTORY imports commands from the history file and appends locally typed commands, as the zsh options documentation describes. That behavior requires verification beyond a single file comparison.

The project's shared-history regression reads the shell's list with fc -ln, checks replacement and undo, then advances through more prompts to check for duplicate imports. That is inspected test coverage, not a test run performed for this article. The temporary-file demonstration above checks only the simpler history-level behavior.

Check both views before calling it done

For a disposable history fixture, verify the rewritten file and the running shell's fc -l output separately. Check undo against both, and check again after another prompt when shared history is enabled. Include an event that exists only in memory if the tool claims to preserve those events; an all-on-disk fixture cannot test that promise.

For real use, follow the tool's instruction to close other shells sharing the file before replacement or undo. Its documented limits include old text retained in other shells, lower history levels, the undo record, temporary files after an interrupted run, and backups.

If the old text was a leaked credential, rotate it. History replacement changes stored text; it does not revoke access. The practical success condition is narrower than “the file looks right”: the file, the calling shell, and the advertised undo behavior must agree about the change you intended.