Repository: junegunn/fzf (language: go)

Issue:
History search not working 
### Checklist

- [X] I have read through the manual page (`man fzf`)
- [X] I have searched through the existing issues
- [X] For bug reports, I have checked if the bug is reproducible in the latest version of fzf

### Output of `fzf --version`

0.53.0 (c4a9ccd)

### OS

- [ ] Linux
- [X] macOS
- [ ] Windows
- [ ] Etc.

### Shell

- [ ] bash
- [X] zsh
- [ ] fish

### Problem / Steps to reproduce

Press CTRL-R to search through history. Press enter to select an entry. Nothing is entered into the terminal. See the screen recording for exactly what's happening. You can see the bug at around 00:27.


https://github.com/junegunn/fzf/assets/95193764/98c71930-e229-4766-9719-73e6e962374a




Discussion / hints:
What's the version of your zsh?

```sh
zsh --version
```

Is the problem still reproducible on zsh without any configuration?

```sh
zsh -d -f

source <(fzf --zsh)

# Or if fzf is not in your $PATH
source <(~/.fzf/bin/fzf --zsh)
```
same issue. tried with the homebrew zsh and apple zsh.
```
zsh --version
zsh 5.9 (x86_64-apple-darwin23.0)
```

```
opt/homebrew/bin/zsh --version
zsh 5.9 (arm-apple-darwin23.0.0)
```
https://github.com/junegunn/fzf/assets/95193764/06c8b911-bcc9-4ae1-990d-a444a592f3e9

Could you share the output when running the following commands one after another?

```sh
# Improved trace prompt
PS4='%B%F{0}+ %D{%T:%3.} %2N:%i%f%b '
# Clears current shell history to minimize debug output
fc -p =(<<<"")

unsetopt KSH_OPTION_PRINT
setopt
fzf --version
source <(fzf --zsh)
# Enable tracing for the function
typeset -fT fzf-history-widget
# Press Ctrl-R, then Enter
```



CTRL-R uses [Perl since 0.53.0](https://github.com/junegunn/fzf/commit/2b6d600879278f771e0ae909b38ecaaab62bc9c1). Could this be a problem with the Perl installation? `perl --version`?

here is the trace outputs

```shell
~/.fzf tags/0.53.0 45s
❯ PS4='%B%F{0}+ %D{%T:%3.} %2N:%i%f%b '

~/.fzf tags/0.53.0
❯ fc -p =(<<<"")


~/.fzf tags/0.53.0
❯ unsetopt KSH_OPTION_PRINT


~/.fzf tags/0.53.0
❯ setopt
alwaystoend
autocd
autopushd
combiningchars
completeinword
extendedhistory
noflowcontrol
histexpiredupsfirst
histignoredups
histignorespace
histverify
interactive
interactivecomments
login
longlistjobs
monitor
nopromptcr
nopromptsp
promptsubst
pushdignoredups
pushdminus
sharehistory
shinstdin
zle

~/.fzf tags/0.53.0
❯ fzf --version

0.53.0 (c4a9ccd)

~/.fzf tags/0.53.0
❯ source <(fzf --zsh)


~/.fzf tags/0.53.0
❯ typeset -fT fzf-history-widget


~/.fzf tags/0.53.0
❯ + 17:18:46:611 fzf-history-widget:1 local selected num
+ 17:18:46:611 fzf-history-widget:2 setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases
+ 17:18:46:612 fzf-history-widget:5 zmodload -F zsh/parameter p:history
+ 17:18:46:612 fzf-history-widget:5 ((  22  ))
+ 17:18:46:612 fzf-history-widget:6 selected=+ 17:18:46:616 fzf-history-widget:6 printf '%1$s\t%2$s\000' 6 $'typeset -fT fzf-history-widget\n' 5 $'source <(fzf --zsh)\n' 4 $'fzf --version\n' 3 setopt 2 $'unsetopt KSH_OPTION_PRINT\n' 1 ''
+ 17:18:46:612 fzf-history-widget:6 selected=+ 17:18:46:617 fzf-history-widget:7 perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++) { s/\n/\n\t/gm; print; }'
+ 17:18:46:612 fzf-history-widget:6 selected=+ 17:18:46:617 fzf-history-widget:8 __fzfcmd
+ 17:18:46:612 fzf-history-widget:6 selected=+ 17:18:46:619 fzf-history-widget:8 FZF_DEFAULT_OPTS=+ 17:18:46:620 fzf-history-widget:8 __fzf_defaults '' '-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line  --query="" +m --read0'
+ 17:18:46:619 fzf-history-widget:8 FZF_DEFAULT_OPTS=$'--height 40% --bind=ctrl-z:ignore \n -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --highlight-line  --query="" +m --read0' FZF_DEFAULT_OPTS_FILE='' fzf
+ 17:18:46:612 fzf-history-widget:6 selected=$'4\tfzf --version\n\t'
+ 17:18:49:857 fzf-history-widget:15 local ret=0
+ 17:18:49:857 fzf-history-widget:16 [ -n $'4\tfzf --version\n\t' ']'
+ 17:18:49:857 fzf-history-widget:17 num=+ 17:18:49:857 fzf-history-widget:17 num=+ 17:18:49:862 fzf-history-widget:17 awk '{print $1; exit}'
+ 17:18:49:862 fzf-history-widget:17 grep -o '^[1-9][0-9]*'
+ 17:18:49:857 fzf-history-widget:17 num=$'\C-[[01;31m\C-[[K4\C-[[m\C-[[K'
+ 17:18:49:868 fzf-history-widget:18 zle vi-fetch-history -n $'\C-[[01;31m\C-[[K4\C-[[m\C-[[K'
+ 17:18:49:868 fzf-history-widget:23 zle reset-prompt
~/.fzf tags/0.53.0
❯
```

I tried with apple perl (v5.34.1) and homebrew perl (v5.38.2).


It looks like your "grep" is injecting color codes to the output when it shouldn't. What is the output of `type grep`?

Fix patch:
diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh
index f59fbae4456..1490595d25f 100644
--- a/shell/key-bindings.zsh
+++ b/shell/key-bindings.zsh
@@ -106,8 +106,8 @@ fi
 
 # CTRL-R - Paste the selected command from history into the command line
 fzf-history-widget() {
-  local selected num
-  setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob 2> /dev/null
+  local selected
+  setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch 2> /dev/null
   # 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
@@ -122,8 +122,8 @@ fzf-history-widget() {
   fi
   local ret=$?
   if [ -n "$selected" ]; then
-    if num=$(awk '{print $1; exit}' <<< "$selected" | grep -o '^[1-9][0-9]*'); then
-      zle vi-fetch-history -n $num
+    if [[ $(awk '{print $1; exit}' <<< "$selected") =~ ^[1-9][0-9]* ]]; then
+      zle vi-fetch-history -n $MATCH
     else # selected is a custom query, not from history
       LBUFFER="$selected"
     fi


Test patch: