Last active
March 4, 2025 18:55
-
-
Save ganeshkumar269/d20b912e34f860ed0f6a88075640d4b0 to your computer and use it in GitHub Desktop.
Better CLI history navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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