I've stumbled upon this question while trying to achieve something similar. I believe the following solves your problem:
Use a file to record all executed commands. Thanks to Eli Bendersky for his post, I use his code as it is, the only change is setting the filename in $PERSISTENT_HISTORY_FILE
.
Install fzf with key bindings (I'm unsure if installation via package manager sets key bindings, but via git and install script does).
Change __fzf_history
function in fzf/shell/key-bindings.bash
to:
__fzf_history__() {
local output opts script
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0"
script='BEGIN { $/ = "\n"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'
output=$(
tac $PERSISTENT_HISTORY_FILE |
last_hist=$(HISTTIMEFORMAT='%F %T ' builtin history 1) perl -n -l0 -e "$script" |
FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE"
) || return
READLINE_LINE=${output#*| }
if [[ -z "$READLINE_POINT" ]]; then
echo "$READLINE_LINE"
else
READLINE_POINT=0x7fffffff
fi
}
Obviously, you can make your own function stripping the code above from fzf specifics and binding it to ctrl+r.
bash
, you can share one history file, but each session manages it's own history in the memory. You can merge the history from the file to the session, but then it would also affect up/down keys. What I suggest is to replace ctrl+r with a different script/alias that would search through the file, maybe with grep. It wouldn't be the same, of course, but that's what I could come up with. – aviro Jan 04 '22 at 07:30