[bash] use fzf or fzy bindings when available

This commit is contained in:
odrling 2024-04-15 10:43:01 +02:00
parent 5737760f84
commit 8b365e49c3
No known key found for this signature in database
GPG key ID: EC907F69A27A90C5
2 changed files with 13 additions and 9 deletions

View file

@ -13,6 +13,14 @@
[[ $- =~ i ]] || return 0
if command -v fzf > /dev/null 2>&1; then
__fzfcmd=fzf
elif command -v fzy > /dev/null 2>&1; then
__fzfcmd=fzy
else
return 1
fi
# Key bindings
# ------------
@ -24,16 +32,12 @@ __fzf_select__() {
-o -type l -print 2> /dev/null | command cut -b3-"}"
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse --scheme=path ${FZF_DEFAULT_OPTS-} ${FZF_CTRL_T_OPTS-} -m"
eval "$cmd" |
FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) "$@" |
FZF_DEFAULT_OPTS="$opts" ${__fzfcmd} "$@" |
while read -r item; do
printf '%q ' "$item" # escape special chars
done
}
__fzfcmd() {
echo fzy
}
fzf-file-widget() {
local selected="$(__fzf_select__ "$@")"
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
@ -45,13 +49,13 @@ __fzf_cd__() {
cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type d -print 2> /dev/null | command cut -b3-"}"
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse --scheme=path ${FZF_DEFAULT_OPTS-} ${FZF_ALT_C_OPTS-} +m"
dir=$(set +o pipefail; eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)) && printf 'builtin cd -- %q' "$dir"
dir=$(set +o pipefail; eval "$cmd" | FZF_DEFAULT_OPTS="$opts" ${__fzfcmd}) && printf 'builtin cd -- %q' "$dir"
}
__fzf_history__() {
local output
output=$(command fc -l -n -r 1 | sed 's/^[\t ]*//' | $(__fzfcmd) --query="$READLINE_LINE")
output=$(command fc -l -n -r 1 | sed 's/^[\t ]*//' | ${__fzfcmd} --query="$READLINE_LINE")
READLINE_LINE=${output#*$'\t'}
if [[ -z "$READLINE_POINT" ]]; then
echo "$READLINE_LINE"

View file

@ -35,8 +35,8 @@ export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
[ -f /etc/bash_completion ] && . /etc/bash_completion
. ~/.bash/completions
# fzf
command -v fzy 2>&1 > /dev/null && source ~/.bash/fzy.bash
# fzf/fzy
source ~/.bash/fzy.bash
source ~/.bash/bash-preexec/bash-preexec.sh