Ctrl-R History Search
fzf_history_search is ported from joshskidmore/zsh-fzf-history-search and extended to handle multiline commands, colored aligned columns, deduplication, and clipboard export cleanly.
How It Works
- Ordering & timestamps from
fc -li -1 0: Lists history newest-first with event numbers andYYYY-MM-DD HH:MMtimestamps.SHARE_HISTORY*suffixes on foreign events are stripped and continuation lines are skipped. - Exact command text from
$history: Loadszsh/parameter(p:history) so$history[$event]yields the complete original command, including embedded newlines. - NUL framing (
--read0): Each entry is emitted asprintf '%s\0', so multiline commands remain one row in fzf instead of splitting across picker lines. - Deduplication: When
ZSH_FZF_HISTORY_SEARCH_REMOVE_DUPLICATESis non-empty (default1), each unique command is emitted once — keeping the newest occurrence becausefc -liiterates newest-first. - Aligned colored columns:
- Event numbers are right-aligned to the width of the newest (widest) event so the date column lines up without TAB gaps.
- Colors use
--ansivia SGR column variables; fzf strips escape sequences from its output and from{}inexecute-silent.
- Byte-for-byte restoration: On accept, leading alignment spaces are trimmed, the event number is extracted, and ZLE executes
zle vi-fetch-history -n "$selected_event"(followed byzle end-of-lineifZSH_FZF_HISTORY_SEARCH_END_OF_LINEis non-empty).
Initial Query
If your command line already has text ($#BUFFER > 0), the picker starts with:
zsh
-q "${ZSH_FZF_HISTORY_SEARCH_FZF_QUERY_PREFIX}${BUFFER}"Copying with Ctrl-Y
Pressing Ctrl-Y inside the Ctrl-R picker runs:
sh
printf %s '{}' | sed 's/^ *//' | cut -d' ' -f4- | <clipboard_cmd>and aborts the picker without modifying your current prompt buffer. Clipboard command selection checks:
pbcopywhen$OSTYPEisdarwin*xclip -selection clipboardwl-copy- fallback
cat >/dev/null
Safe default for FZF_CTRL_R_OPTS
The plugin exports FZF_CTRL_R_OPTS="--bind 'enter:accept'" so Enter only accepts into the command buffer and never executes immediately.