fzf comes ready to install closely related functionality in fzf's key-bindings.bash mentioned by @sudavid4. The __fzf_history__ function searches bash history interactively using fzf.
To address the copy question directly, as pointed out by other answering contributors, you could duplicate, rename, and replace the echo for xclip/xsel -b/pbcopy in the key-bindings.bash:__fzf_history__ (MIT licensed) function.
$ dfp __fzf_history__ __fzf_history__ () { local output; output=$( builtin fc -lnr -2147483648 | last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e 'BEGIN { getc; $/ = "\n\t"; $HISTCMD = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCMD - $. . "\t$_" if !$seen{$_}++' | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS +m --read0" $(__fzfcmd) --query "$READLINE_LINE" ) || return; READLINE_LINE=${output#*' '}; if [[ -z "$READLINE_POINT" ]]; then echo "$READLINE_LINE"; else READLINE_POINT=0x7fffffff; fi }
Just use that function, replace the final echo.
Other history searching builtins & options
In default history handling (though unnecessary with fzf's function), editing after selection is enabled by setting histverify. From man bash:
histverify
If set, and readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification.
Finally, bash's builtin fc (learn more with help, not man), supports "Display[ing] or execut[ing] commands from the history list" in addition to the history builtin which focuses on "Display or manipulat[ion of] the history list."
Note also...
fzf's key-bind.bash snippet:
# CTRL-R - Paste the selected command from history into the command line bind -m emacs-standard -x '"\C-r": __fzf_history__' bind -m vi-command -x '"\C-r": __fzf_history__' bind -m vi-insert -x '"\C-r": __fzf_history__'
A neighboring bash shopt (shell option) with related but different purpose:
histreedit
If set, and readline is being used, a user is given the opportunity to re-edit a failed history substitution.
fzf niceties
--tac and --reverse (= --layout=reverse) are nice options. --height=HEIGHT[%], --prompt='> ', and --info=inline can help maximize use of terminal real-estate.