Skip to content

Instantly share code, notes, and snippets.

@ganeshkumar269
Last active March 4, 2025 18:55
Show Gist options
  • Save ganeshkumar269/d20b912e34f860ed0f6a88075640d4b0 to your computer and use it in GitHub Desktop.
Save ganeshkumar269/d20b912e34f860ed0f6a88075640d4b0 to your computer and use it in GitHub Desktop.
Better CLI history navigation
fzf_history() {
local selected
setopt localoptions pipefail no_aliases 2> /dev/null
selected=$(fc -l -n -t "%s" 1 | awk '{cmd=$0; sub(/^[0-9]+ /, "", cmd); print $1 "|" cmd}' | awk -F '|' '!seen[$2]++ {print $0}' | cut -d '|' -f2- | \
fzf --tac --tiebreak=index --height 50% --layout=reverse --preview 'echo {}' --preview-window=up:3:wrap \
--bind 'ctrl-y:execute-silent(echo -n {+} | pbcopy)' \
--header 'Press CTRL-Y to copy command into clipboard')
if [[ -n "$selected" ]]; then
BUFFER=${selected}
zle reset-prompt
# uncomment the below to execute the command instead of just selecting
# zle accept-line
fi
}
zle -N fzf_history
# Bind to Ctrl+R
bindkey '^R' fzf_history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment