Files
TerminalPolyjuice/bash/keybindings/fzf_keybindings.sh
2026-04-03 16:15:31 +08:00

57 lines
1.6 KiB
Bash

#!/usr/bin/env bash
if ! command -v fzf >/dev/null 2>&1; then
return
fi
_pj_readline_append() {
local text="$1"
[[ -z "$text" ]] && return 0
READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}${text}${READLINE_LINE:READLINE_POINT}"
READLINE_POINT=$((READLINE_POINT + ${#text}))
}
__pj_fzf_file_widget() {
local selected_file
local command_to_run
if command -v fd >/dev/null 2>&1; then
command_to_run='fd --type f --hidden --exclude .git | fzf'
elif command -v rg >/dev/null 2>&1; then
command_to_run='rg --files --hidden --glob "!.git/*" | fzf'
elif command -v ag >/dev/null 2>&1; then
command_to_run='ag -l --hidden -g "" --ignore .git | fzf'
else
command_to_run='find . -type f 2>/dev/null | fzf'
fi
selected_file="$(eval "$command_to_run")"
_pj_readline_append "$selected_file"
}
__pj_fzf_dir_widget() {
local selected_dir
local command_to_run
if command -v fd >/dev/null 2>&1; then
command_to_run='fd --type d --hidden --no-ignore --exclude .git | fzf'
else
command_to_run='find . -type d 2>/dev/null | fzf'
fi
selected_dir="$(eval "$command_to_run")"
_pj_readline_append "$selected_dir"
}
__pj_fzf_history_widget() {
local selected_command
selected_command="$({ HISTTIMEFORMAT= history; } | sed 's/^[[:space:]]*[0-9]\+[[:space:]]*//' | fzf --height 40% --reverse --border)"
_pj_readline_append "$selected_command"
}
# Alt-f: file selector, Alt-d: directory selector, Alt-h: history selector
bind -x '"\ef":__pj_fzf_file_widget'
bind -x '"\ed":__pj_fzf_dir_widget'
bind -x '"\eh":__pj_fzf_history_widget'