Skip to content

Instantly share code, notes, and snippets.

@austintraver
Created August 10, 2021 15:58
Show Gist options
  • Save austintraver/50443a499e856e65f353c912dc83c2f3 to your computer and use it in GitHub Desktop.
Save austintraver/50443a499e856e65f353c912dc83c2f3 to your computer and use it in GitHub Desktop.
A fuzzy finder to search history in the Z Shell.
function fuzzy-search-history() {
# Assemble the list of commands found in history,
# such that it only contains the most recent occurrence
# of a particular command.
typeset -a entries=()
for line in ${(f)"$(fc -nl 1)"}; do
entries+=${${line//\\n/ }//\\t/ };
done
BUFFER=$(
print -l ${(Oau)entries} \
| tr -cd '[:print:]\n' \
| grep \
-a \
--invert-match \
--regexp '[[:alnum:]]\{30\}' \
--regexp '[^[:space:]]\{79\}' \
--regexp '^[[:cntrl:]]*$' \
--regexp '^[^[:alnum:]]' \
--regexp '^[^[:space:]]*$' \
| fzf \
--bind 'backward-eof:abort' \
--exact \
--height='40%' \
--min-height='5' \
--no-sort \
--no-multi \
--reverse \
--query=${LBUFFER}
)
CURSOR=${#BUFFER}
zle reset-prompt
zle vi-add-eol
}
zle -N fuzzy-search-history
bindkey -v '^O' fuzzy-search-history
bindkey -a '^O' fuzzy-search-history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment