ZSH Ctrl-R - any way to multi-select and combine lines? #4591
-
|
I think it would be useful to be able to multi-select items in fzf from my Ctrl-R history and combine them with In this way I could assemble longer commands from my old history. I'm using oh-my-zsh with both I tried adding Is there any way to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
The last flag wins; Lines 136 to 141 in 2ab923f Even with the Below is a modified 'fzf-history-widget' version, add it after your
# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
local selected
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch extendedglob 2> /dev/null
# Ensure the module is loaded if not already, and the required features, such
# as the associative 'history' array, which maps event numbers to full history
# lines, are set. Also, make sure Perl is installed for multi-line output.
if zmodload -F zsh/parameter p:{commands,history} 2>/dev/null && (( ${+commands[perl]} )); then
selected="$(printf '%s\t%s\000' "${(kv)history[@]}" |
perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\t(.*)/s, $1)}++) { s/\n/\n\t/g; print; }' |
FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} -m --read0") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
else
selected="$(fc -rl 1 | __fzf_exec_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,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} -m") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
fi
local ret=$?
local -a cmds
if [ -n "$selected" ]; then
if [[ $(__fzf_exec_awk '{print $1; exit}' <<< "$selected") =~ ^[1-9][0-9]* ]]; then
for line in ${(f)selected}; do
if (( ${+commands[perl]} )); then
[[ $line == (#b)(<->)$'\t'* ]] && cmds+=("${history[${match[1]}]}")
elif [[ $(__fzf_exec_awk '{print $1; exit}' <<< "$line") =~ ^[1-9][0-9]* ]]; then
cmds+=("${history[${MATCH}]}")
fi
done
LBUFFER+="${(j:; :)cmds}"
else # selected is a custom query, not from history
LBUFFER="$selected"
fi
fi
zle reset-prompt
return $ret
}diff--- a/shell/key-bindings.zsh
+++ b/shell/key-bindings.zsh
@@ -130,5 +130,5 @@ fi
fzf-history-widget() {
local selected
- setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch 2> /dev/null
+ setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch extendedglob 2> /dev/null
# Ensure the module is loaded if not already, and the required features, such
# as the associative 'history' array, which maps event numbers to full history
@@ -137,15 +137,23 @@ fzf-history-widget() {
selected="$(printf '%s\t%s\000' "${(kv)history[@]}" |
perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\t(.*)/s, $1)}++) { s/\n/\n\t/g; print; }' |
- FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \
+ FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} -m --read0") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
else
selected="$(fc -rl 1 | __fzf_exec_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,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \
+ FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} -m") \
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))"
fi
local ret=$?
+ local -a cmds
if [ -n "$selected" ]; then
if [[ $(__fzf_exec_awk '{print $1; exit}' <<< "$selected") =~ ^[1-9][0-9]* ]]; then
- zle vi-fetch-history -n $MATCH
+ for line in ${(f)selected}; do
+ if (( ${+commands[perl]} )); then
+ [[ $line == (#b)(<->)$'\t'* ]] && cmds+=("${history[${match[1]}]}")
+ elif [[ $(__fzf_exec_awk '{print $1; exit}' <<< "$line") =~ ^[1-9][0-9]* ]]; then
+ cmds+=("${history[${MATCH}]}")
+ fi
+ done
+ LBUFFER+="${(j:; :)cmds}"
else # selected is a custom query, not from history
LBUFFER="$selected"EDIT1/2: shorten diff |
Beta Was this translation helpful? Give feedback.
thx, I can reproduce. can u try adding
extendedglobtosetopt localoptions ...?