Skip to content

Commit

Permalink
feat(zfunctions): add search and select history
Browse files Browse the repository at this point in the history
  • Loading branch information
MoeBensu committed Aug 31, 2024
1 parent 73c16a3 commit 6857528
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions zfunctions/peco_select_history.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env zsh

# Default configurations
typeset -gA PECO_HISTORY_CONFIG=(
[layout]="bottom-up"
[prompt]="HISTORY>"
[initial_filter]="Fuzzy"
[extra_flags]=""
[keymap]="^R"
)

peco_select_history() {
local peco_flags=(
"--layout=${PECO_HISTORY_CONFIG[layout]}"
"--prompt" "${PECO_HISTORY_CONFIG[prompt]}"
"--initial-filter=${PECO_HISTORY_CONFIG[initial_filter]}"
)
[ -n "${PECO_HISTORY_CONFIG[extra_flags]}" ] && peco_flags+=("${(z)PECO_HISTORY_CONFIG[extra_flags]}")
[ $# -ne 0 ] && peco_flags+=("--query" "$1")

local selected=$(fc -rl 1 |
awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
peco "${peco_flags[@]}" |
awk '{$1=""; print substr($0,2)}')

if [ -n "$selected" ]; then
BUFFER=$selected
CURSOR=$#BUFFER
zle redisplay
else
zle reset-prompt
fi
}

zle -N peco_select_history
bindkey "${PECO_HISTORY_CONFIG[keymap]}" peco_select_history

0 comments on commit 6857528

Please sign in to comment.