Files
dotfiles/dot_config/bash/keybinds.bash
T

62 lines
2.3 KiB
Bash

# ~/.config/bash/keybinds.bash — readline ports of the television keys in
# fish_user_key_bindings.fish / zsh keybinds.zsh:
# ctrl-o → gho (ghq-repos cable, cd) ctrl-e → tv git-files (nvim)
# ctrl-p → pj (pj cable) down → pj on an empty prompt
# ctrl-t → tv smart autocomplete (tv init bash ships no ctrl-t)
# Keys owned by kitty.conf never reach the shell: ctrl-h/j/k/l (pass_keys.py).
# bind -x runs in the shell process, so gho's cd persists — but readline keeps
# drawing the prompt it started with. Each key is therefore a macro: \C-xN
# stashes the typed line and runs the command, \C-m accepts the now-empty
# line (bash prints a fresh prompt for the new cwd; nothing reaches history),
# and \C-x0 restores the stash into the new line.
_kb_run() {
_kb_line=$READLINE_LINE _kb_point=$READLINE_POINT
READLINE_LINE='' READLINE_POINT=0
"$@" </dev/tty
}
_kb_restore() {
READLINE_LINE=${_kb_line-} READLINE_POINT=${_kb_point-0}
unset _kb_line _kb_point
}
bind -x '"\C-x0": _kb_restore'
# _kb_bind KEY SLOT COMMAND…
_kb_bind() {
local key=$1 slot=$2
shift 2
bind -x "\"\\C-x$slot\": _kb_run $*"
bind "\"$key\": \"\\C-x$slot\\C-m\\C-x0\""
}
_kb_bind '\C-o' 1 gho
_kb_bind '\C-e' 2 tv git-files
_kb_bind '\C-p' 3 pj
# down: the pj cable on an idle, empty prompt, otherwise history search
# (fish's _pj_bind_down). bind -x can't call readline functions, so _kb_down
# rebinds \C-x5, which the macro sends right after it.
_kb_down() {
if [ -n "$READLINE_LINE" ]; then
bind '"\C-x5": history-search-forward'
return
fi
PJ_QUIET=1 pj </dev/tty
bind '"\C-x5": redraw-current-line'
}
bind -x '"\C-x4": _kb_down'
bind '"\e[B": "\C-x4\C-x5"'
bind '"\eOB": "\C-x4\C-x5"'
# ctrl-t: pick with the channel tv guesses from the text left of the cursor
# (shell_integration.channel_triggers) and insert the picks there, quoted.
_kb_tv_autocomplete() {
local pre=${READLINE_LINE:0:READLINE_POINT} out item ins=''
out=$(tv --autocomplete-prompt "$pre" </dev/tty) || return
while IFS= read -r item; do
[ -n "$item" ] && ins+="$(printf '%q' "$item") "
done <<<"$out"
READLINE_LINE=$pre$ins${READLINE_LINE:READLINE_POINT}
READLINE_POINT=$((${#pre} + ${#ins}))
}
bind -x '"\C-t": _kb_tv_autocomplete'