fzf icon indicating copy to clipboard operation
fzf copied to clipboard

[macOS] missing history item with bash 3.2

Open step- opened this issue 2 years ago • 0 comments

  • [x] I have read through the manual page (man fzf)
  • [x] I have the latest version of fzf
  • [x] I have searched through the existing issues

Info

  • OS
    • [ ] Linux
    • [x] Mac OS X
    • [ ] Windows
    • [ ] Etc.
  • Shell
    • [x] bash
    • [ ] zsh
    • [ ] fish

Problem / Steps to reproduce

I reproduced this issue on macOS Catalina with bash 3.2, and Linux with bash 3.2. It matters mostly for macOS, in which 3.2 is the default bash version.

On macOS (Linux) the history applet doesn't work correctly with bash 3.2. There are two issues:

  • the widget does not list the most-recently typed command;
  • the numeric index shown by the widget is off by one.

To reproduce, first add some history and make sure to take the key binding definition from the source code for bash 3.

bash-3.2$ : 1
bash-3.2$ bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
bash-3.2$ <c-r>
...
27	: 1
<esc>

You can see that the bind command wasn't shown although it should have. Now run the history widget directly instead of via the <c-r> key.

bash-3.2$ __fzf_history__
bash-3.2$ bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
...
26	: 1
27	bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
<esc>

This time bind was there but the widget showed different line numbers!

So the history index shown by <c-r> ends up being off by one, which can mislead a user recalling commands with the !<index> builtin.

bash-3.2$ : 2
bash-3.2$ : 3
bash-3.2$ : 4
bash-3.2$ <c-r>
26	bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
27	__fzf_history__
28	: 2
29	: 3
<esc>
bash-3.2$ !29
: 4

You can run similar commands with a more recent bash version -- I happened to have 4.4 -- to find out that this behavior only happens with bash 3.

bash-4.4$ : 1
bash-4.4$ bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
bash-4.4$ <c-r>
...
10	: 1
11	bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"'
<esc>
bash-4.4$ !11
: 1

I tested the builtin fc command that retrieves history in bash 3.2 and 4.4 on Linux. It outputs identical data in both bash versions for the same history list.

step- avatar May 25 '23 12:05 step-