bashrc/.bashrc.d/12-bindings.bashrc

69 lines
2.6 KiB
Plaintext
Raw Normal View History

2023-03-18 12:49:08 +01:00
#!/usr/bin/env bash
# executed by bash(1) for non-login shells.
# ==============================
# BINDINGS
# ==============================
# ==============================
# FZF-EMOJI-WIDGET
# ==============================
# custom emojis selector with fzf, inspired by fzf shell bindings
# Require bash > v4; I did not copy other shells solutions from fzf shell bindings
# => pip --user install emoji-fzf
# => sudo apt install fzf
if [[ -f "$HOME/.local/bin/emoji-fzf" ]]; then
fzf-emoji-widget() {
Help() {
echo "FZF widget to insert or replace some emojis."
echo "Use it with bash>4 and 'bind -x'"
echo "Default: insert emojis in place."
echo
echo "Syntax: [-h] [-i]"
echo "options:"
echo "-h Print this Help."
echo "-i Replace the word before cursor, using it as a query"
echo
# READLINE(command line string) and READLINE_POINT(cursor position)
# are generated by bind.
}
local OPTIND=0
while getopts ":hi" option; do
case $option in
h) Help
return 0 ;;
i) local _REPLACE=1
local _QUERYLINE=${READLINE_LINE:0:READLINE_POINT}
local _QUERYLINE_POINT=${#_QUERYLINE}
local _QUERY="${_QUERYLINE/* /}"
local _QUERYLINE_START=$(( _QUERYLINE_POINT - "${#_QUERY}" )) ;;
\?) echo "Error: Invalid option: -$OPTARG"
Help
return 0 ;;
esac
done
shift $((OPTIND-1))
local selected=$(emoji-fzf preview --prepend | fzf-tmux -p 85% --multi --reverse --no-hscroll --header "Select multiple emojis with Tab and validate with Enter." --query "$_QUERY" | cut -d " " -f 2 | emoji-fzf get | xargs -d "\n")
if [[ $_REPLACE == 1 ]]; then
READLINE_LINE="${READLINE_LINE:0:$_QUERYLINE_START}$selected${READLINE_LINE:READLINE_POINT}"
READLINE_POINT=$(( _QUERYLINE_START + "${#selected}" ))
else
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + "${#selected}" ))
fi
}
# ALT-e - Paste emojis from a list into the command line
bind -m emacs-standard -x '"\ee": fzf-emoji-widget'
bind -m vi-command -x '"\ee": fzf-emoji-widget'
bind -m vi-insert -x '"\ee": fzf-emoji-widget'
# ALT-i - Replace the word before cursor with emojis from a list
bind -m emacs-standard -x '"\ei": fzf-emoji-widget -i'
bind -m vi-command -x '"\ei": fzf-emoji-widget -i'
bind -m vi-insert -x '"\ei": fzf-emoji-widget -i'
fi