Repository: junegunn/fzf (language: go)

Issue:
ctrl+r no longer pulling from shared history until a command is run succesfully
### 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.55.0 (brew)

### OS

- [X] Linux
- [ ] macOS
- [ ] Windows
- [ ] Etc.

### Shell

- [ ] bash
- [X] zsh
- [ ] fish

### Problem / Steps to reproduce

This is very similar to #1649, except `fc -rl 1` is behaving as expected

I have 2 terminals, I'm using zsh with `setopt share_history`. Outside of fzf, it works fine, if I go to a second terminal and type in a command, it is available on my first terminal after hitting a newline. However history is not being shared with fzf triggered via control+R until a command is run sucessfully.

- Have 2 terminals with shared history
- In terminal 2, input any command
- In terminal 1, control+r to enter fzf search and observe that the last command isn't present (nor is it if you search (assuming you haven't run the same command in the past)
- control + c out of fzf search
- input a blank new line and use control+r to observe that the input from terminal 2 still is not present
- control + c out of fzf search
- run a command, like `fc -rl 1` or `echo` and use control+r to observe that now the input from terminal 2 is present

This feels like a regression, I feel it used to either get updated without having to invoke a command first (but I could be mistaken)


Discussion / hints:
> I feel it used to either get updated without having to invoke a command first

Your assessment is correct. The issue came with #3823; the problem is with the associative `history` array, which doesn't update on a simple return as `fc -rl 1` does, but only, as you pointed out, on some command.

https://github.com/junegunn/fzf/blob/3c40b1bd51a895a907dbae5d30f6d9e99bdc1f90/shell/key-bindings.zsh#L113-L122

**Workaround A:** Modifies the sourcing by disabling the `perl` part, which also disables multiline command display.

```sh
source <(fzf --zsh | sed '/zmodload/s/perl/perl_off/')
```

**Workaround B:** Prepend `fc -RI` before `${(kv)history[@]}` is called.

```sh
source <(fzf --zsh | sed '/history\[@\]/s/^/fc -RI; /')
```

---

It is unfortunate that `zsh` behaves differently between `$history` and `fc -rl 1`, and hopefully, this is just a bug in `zsh`.

In the meantime, you could share your enabled options by running the command below.


```sh
unsetopt KSH_OPTION_PRINT
print -r -- $(setopt)
```

And verify if the proposed workarounds give you back your previous experience. Enabling tracing for the `fzf-history-widget` lets you see what it does. No need to share the output.

```sh
# Verbose execution trace prompt (default: '+%N:%i> ')
PS4=$'\n%B%F{0}+ %D{%T:%3.} %2N:%I%f%b '
typeset -fT fzf-history-widget
# press ctrl-r
```


Fix patch:
diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh
index a4d7055c819..9d483921e74 100644
--- a/shell/key-bindings.zsh
+++ b/shell/key-bindings.zsh
@@ -108,9 +108,13 @@ fi
 fzf-history-widget() {
   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
+  # 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,options} 2>/dev/null && (( ${#commands[perl]} )); then
+    # Import commands from other shells if SHARE_HISTORY is enabled, as the
+    # 'history' array only updates after executing a non-empty command.
+    [[ "${options[sharehistory]}" == "on" ]] && fc -RI
     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 --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \


Test patch: