From 2b6d600879278f771e0ae909b38ecaaab62bc9c1 Mon Sep 17 00:00:00 2001 From: LangLangBart <92653266+LangLangBart@users.noreply.github.com> Date: Fri, 31 May 2024 03:43:58 +0200 Subject: [PATCH] [zsh] Enhance CTRL-R to display multi-line entires (#3823) Co-authored-by: Junegunn Choi --- shell/key-bindings.zsh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index 7243096..07f02bf 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -108,14 +108,22 @@ fi fzf-history-widget() { local selected num setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null - selected="$(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' | - FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \ - FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" + # Ensure the associative history array, which maps event numbers to the full + # history lines, is loaded, and that Perl is installed for multi-line output. + if zmodload -F zsh/parameter p:history 2>/dev/null && (( ${#commands[perl]} )); then + selected="$(printf '%1$s\t%2$s\000' "${(vk)history[@]}" | + perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++) { s/\n/\n\t/gm; print; }' | + FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \ + FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" + else + selected="$(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' | + FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \ + FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" + fi local ret=$? if [ -n "$selected" ]; then - num=$(awk '{print $1}' <<< "$selected") - if [[ "$num" =~ '^[1-9][0-9]*\*?$' ]]; then - zle vi-fetch-history -n ${num%\*} + if num=$(awk '{print $1; exit}' <<< "$selected" | grep -o '^[1-9][0-9]*'); then + zle vi-fetch-history -n $num else # selected is a custom query, not from history LBUFFER="$selected" fi