Repository: junegunn/fzf
File: shell/key-bindings.zsh (Shell)

Code before:
  local -a cmds
  # Avoid leaking auto assigned values when using backreferences '(#b)'
  local -a mbegin mend match
  if [ -n "$selected" ]; then
    # Heuristic to check if the selected value is from history or a custom query
    if [[ $selected == [[:blank:]]#<->\*#[[:blank:]]* ]]; then
      # Split at newlines
      for line in ${(ps:\n:)selected}; do
        if (( extracted_with_perl )); then
          if [[ $line == (#b)(<->)(#B)$'\t'* ]]; then
            (( ${+history[${match[1]}]} )) && cmds+=("${history[${match[1]}]}")
          fi
        elif [[ $line == [[:blank:]]#(#b)(<->)(#B)\*#[[:blank:]]* ]]; then
          # Avoid $history array: lags behind 'fc' on foreign commands (*)
          # https://zsh.org/mla/users/2024/msg00692.html
          # Push BUFFER onto stack; fetch and save history entry from BUFFER; restore
          zle .push-line
          zle vi-fetch-history -n ${match[1]}
          (( "${#BUFFER}" )) && cmds+=("${BUFFER}")
          BUFFER=""
          zle .get-line
        fi
      done
      if (( ${#cmds[@]} )); then
        # Join by newline after stripping trailing whitespace from each command
        BUFFER="${(pj:\n:)${(@)cmds%%[[:space:]]#}}"
        CURSOR=${#BUFFER}
      fi
    else # selected is a custom query, not from history
      LBUFFER="$selected"
    fi
  fi
  zle reset-prompt
  return $ret
}
if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then
  if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then
    echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2
  fi
  zle     -N            fzf-history-widget
  bindkey -M emacs '^R' fzf-history-widget
  bindkey -M vicmd '^R' fzf-history-widget
  bindkey -M viins '^R' fzf-history-widget
fi
fi

} always {
  eval $__fzf_key_bindings_options
  'unset' '__fzf_key_bindings_options'
}


Review comment (suggestion):
Just wondering. If I'm not mistaken, in the previous version, this code would only strip trailing new lines, but now it strips `[[:space:]]`. Could you elaborate on the change?

https://github.com/junegunn/fzf/pull/4595#discussion_r2536577262

Code after:
  local -a cmds
  # Avoid leaking auto assigned values when using backreferences '(#b)'
  local -a mbegin mend match
  if [ -n "$selected" ]; then
    # Heuristic to check if the selected value is from history or a custom query
    if ((( extracted_with_perl )) && [[ $selected == <->$'\t'* ]]) ||
    ((( ! extracted_with_perl )) && [[ $selected == [[:blank:]]#<->(  |\* )* ]]); then
      # Split at newlines
      for line in ${(ps:\n:)selected}; do
        if (( extracted_with_perl )); then
          if [[ $line == (#b)(<->)(#B)$'\t'* ]]; then
            (( ${+history[${match[1]}]} )) && cmds+=("${history[${match[1]}]}")
          fi
        elif [[ $line == [[:blank:]]#(#b)(<->)(#B)(  |\* )* ]]; then
          # Avoid $history array: lags behind 'fc' on foreign commands (*)
          # https://zsh.org/mla/users/2024/msg00692.html
          # Push BUFFER onto stack; fetch and save history entry from BUFFER; restore
          zle .push-line
          zle vi-fetch-history -n ${match[1]}
          (( ${#BUFFER} )) && cmds+=("${BUFFER}")
          BUFFER=""
          zle .get-line
        fi
      done
      if (( ${#cmds[@]} )); then
        # Join by newline after stripping trailing newlines from each command
        BUFFER="${(pj:\n:)${(@)cmds%%$'\n'#}}"
        CURSOR=${#BUFFER}
      fi
    else # selected is a custom query, not from history
      LBUFFER="$selected"
    fi
  fi
  zle reset-prompt
  return $ret
}
if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then
  if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then
    echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2
  fi
  zle     -N            fzf-history-widget
  bindkey -M emacs '^R' fzf-history-widget
  bindkey -M vicmd '^R' fzf-history-widget
  bindkey -M viins '^R' fzf-history-widget
fi
fi

} always {
  eval $__fzf_key_bindings_options
  'unset' '__fzf_key_bindings_options'
}