key-bindings.bash
This file
Path
shell/key-bindings.bash
146 lines · 5.5 KiB · Shell
Last changed by
Enhance --min-height option to take number followed by +Junegunn Choibefore adoptioneb0257d
History of this fileTake it
The walk stopped after 200 commits: lines older than 3e6e052 point at it.
[install] grep -> \grepJunegunn Choibefore adoption3e6e052
lines 1–22why, one step back1# ____ ____2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/ key-bindings.bash6#7# - $FZF_TMUX_OPTS8# - $FZF_CTRL_T_COMMAND9# - $FZF_CTRL_T_OPTS10# - $FZF_CTRL_R_OPTS11# - $FZF_ALT_C_COMMAND12# - $FZF_ALT_C_OPTS1314if [[ $- =~ i ]]; then151617# Key bindings18# ------------1920__fzf_defaults() {21 # $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS22 # $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTSEnhance --min-height option to take number followed by +Junegunn Choibefore adoptioneb0257d
line 23why, one step back23 echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"[install] grep -> \grepJunegunn Choibefore adoption3e6e052
lines 24–146why, one step back24 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null25 echo "${FZF_DEFAULT_OPTS-} $2"26}2728__fzf_select__() {29 FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \30 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \31 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" |32 while read -r item; do33 printf '%q ' "$item" # escape special chars34 done35}3637__fzfcmd() {38 [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; } &&39 echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"40}4142fzf-file-widget() {43 local selected="$(__fzf_select__ "$@")"44 READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"45 READLINE_POINT=$(( READLINE_POINT + ${#selected} ))46}4748__fzf_cd__() {49 local dir50 dir=$(51 FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \52 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \53 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd)54 ) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"55}5657if command -v perl > /dev/null; then58 __fzf_history__() {59 local output script60 script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; s/\n/\n\t/gm; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'61 output=$(62 set +o pipefail63 builtin fc -lnr -2147483648 |64 last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |65 FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '"$'\t'"↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} +m --read0") \66 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"67 ) || return68 READLINE_LINE=$(command perl -pe 's/^\d*\t//' <<< "$output")69 if [[ -z "$READLINE_POINT" ]]; then70 echo "$READLINE_LINE"71 else72 READLINE_POINT=0x7fffffff73 fi74 }75else # awk - fallback for POSIX systems76 __fzf_history__() {77 local output script n x y z d78 if [[ -z $__fzf_awk ]]; then79 __fzf_awk=awk80 # choose the faster mawk if: it's installed && build date >= 20230322 && version >= 1.3.481 IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)82 [[ $n == mawk ]] && (( d >= 20230302 && (x *1000 +y) *1000 +z >= 1003004 )) && __fzf_awk=mawk83 fi84 [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries85 script='function P(b) { ++n; sub(/^[ *]/, "", b); if (!seen[b]++) { printf "%d\t%s%c", '$((BASH_REMATCH + 1))' - n, b, 0 } }86 NR==1 { b = substr($0, 2); next }87 /^\t/ { P(b); b = substr($0, 2); next }88 { b = b RS $0 }89 END { if (NR) P(b) }'90 output=$(91 set +o pipefail92 builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*93 command $__fzf_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*94 FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort --wrap-sign '"$'\t'"↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} +m --read0") \95 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"96 ) || return97 READLINE_LINE=${output#*$'\t'}98 if [[ -z "$READLINE_POINT" ]]; then99 echo "$READLINE_LINE"100 else101 READLINE_POINT=0x7fffffff102 fi103 }104fi105106# Required to refresh the prompt after fzf107bind -m emacs-standard '"\er": redraw-current-line'108109bind -m vi-command '"\C-z": emacs-editing-mode'110bind -m vi-insert '"\C-z": emacs-editing-mode'111bind -m emacs-standard '"\C-z": vi-editing-mode'112113if (( BASH_VERSINFO[0] < 4 )); then114 # CTRL-T - Paste the selected file path into the command line115 if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then116 bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'117 bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'118 bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'119 fi120121 # CTRL-R - Paste the selected command from history into the command line122 bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"'123 bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'124 bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'125else126 # CTRL-T - Paste the selected file path into the command line127 if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then128 bind -m emacs-standard -x '"\C-t": fzf-file-widget'129 bind -m vi-command -x '"\C-t": fzf-file-widget'130 bind -m vi-insert -x '"\C-t": fzf-file-widget'131 fi132133 # CTRL-R - Paste the selected command from history into the command line134 bind -m emacs-standard -x '"\C-r": __fzf_history__'135 bind -m vi-command -x '"\C-r": __fzf_history__'136 bind -m vi-insert -x '"\C-r": __fzf_history__'137fi138139# ALT-C - cd into the selected directory140if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then141 bind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\er\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d"'142 bind -m vi-command '"\ec": "\C-z\ec\C-z"'143 bind -m vi-insert '"\ec": "\C-z\ec\C-z"'144fi145146fi