key-bindings.bash
This file
Path
shell/key-bindings.bash
195 lines · 7.1 KiB · Shell
Last changed by
[bash] Persist history deletion when histappend is onJunegunn Choibefore adoptionf584cf3
History of this fileTake it
The walk stopped after 200 commits: lines older than 76efddd point at it.
bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 1–86why, one step back1# ____ ____2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/5# /_/ /___/_/ key-bindings.bash6#7# - $FZF_CTRL_T_COMMAND8# - $FZF_CTRL_T_OPTS9# - $FZF_CTRL_R_COMMAND10# - $FZF_CTRL_R_OPTS11# - $FZF_ALT_C_COMMAND12# - $FZF_ALT_C_OPTS1314if [[ $- =~ i ]]; then151617# Key bindings18# ------------1920#----BEGIN shfmt21#----BEGIN INCLUDE common.sh22# NOTE: Do not directly edit this section, which is copied from "common.sh".23# To modify it, one can edit "common.sh" and run "./update.sh" to apply24# the changes. See code comments in "common.sh" for the implementation details.2526__fzf_defaults() {27 builtin printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"28 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null29 builtin printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"30}3132__fzf_exec_awk() {33 if [[ -z ${__fzf_awk-} ]]; then34 __fzf_awk=awk35 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then36 __fzf_awk=/usr/xpg4/bin/awk37 elif command -v mawk > /dev/null 2>&1; then38 local n x y z d39 IFS=' .' read -r n x y z d <<< $(command mawk -W version 2> /dev/null)40 [[ $n == mawk ]] &&41 (((x * 1000 + y) * 1000 + z >= 1003004)) 2> /dev/null &&42 ((d >= 20230302)) 2> /dev/null &&43 __fzf_awk=mawk44 fi45 fi46 LC_ALL=C exec "$__fzf_awk" "$@"47}48#----END INCLUDE4950__fzf_select__() {51 FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \52 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \53 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" |54 while read -r item; do55 printf '%q ' "$item" # escape special chars56 done57}5859__fzfcmd() {60 [[ -n ${TMUX_PANE-} ]] && { [[ ${FZF_TMUX:-0} != 0 ]] || [[ -n ${FZF_TMUX_OPTS-} ]]; } &&61 echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"62}6364fzf-file-widget() {65 local selected="$(__fzf_select__ "$@")"66 READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}$selected${READLINE_LINE:READLINE_POINT}"67 READLINE_POINT=$((READLINE_POINT + ${#selected}))68}6970__fzf_cd__() {71 local dir72 dir=$(73 FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \74 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \75 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd)76 ) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"77}7879__fzf_history_delete() {80 [[ -s $1 ]] || return8182 local offsets83 offsets=($(sort -rnu "$1"))84 for offset in "${offsets[@]}"; do85 builtin history -d "$offset"86 done[bash] Persist history deletion when histappend is onJunegunn Choibefore adoptionf584cf3
lines 87–90why, one step back8788 if [[ ${#offsets[@]} -gt 0 ]] && shopt -q histappend; then89 builtin history -w90 fibash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 91–101why, one step back91}9293if command -v perl > /dev/null; then94 __fzf_history__() {95 local output script deletefile96 deletefile=$(mktemp)97 script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; s/\n/\n\t/gm; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'98 output=$(99 set +o pipefail100 builtin fc -lnr -2147483648 |101 last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |bash: Fix CTRL-R batch deletionJunegunn Choibefore adoption58b2855
line 102why, one step back102 FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '"$'\t'"↳ ' --highlight-line --bind 'shift-delete:execute-silent(cat {+f1} >> \"$deletefile\")+exclude-multi' --multi ${FZF_CTRL_R_OPTS-} --read0") \bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 103–128why, one step back103 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"104 )105 __fzf_history_delete "$deletefile"106 command rm -f "$deletefile"107 [[ -n $output ]] || return108 READLINE_LINE=$(command perl -pe 's/^\d*\t//' <<< "$output")109 if [[ -z $READLINE_POINT ]]; then110 echo "$READLINE_LINE"111 else112 READLINE_POINT=0x7fffffff113 fi114 }115else # awk - fallback for POSIX systems116 __fzf_history__() {117 local output script deletefile118 deletefile=$(mktemp)119 [[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries120 script='function P(b) { ++n; sub(/^[ *]/, "", b); if (!seen[b]++) { printf "%d\t%s%c", '$((BASH_REMATCH + 1))' - n, b, 0 } }121 NR==1 { b = substr($0, 2); next }122 /^\t/ { P(b); b = substr($0, 2); next }123 { b = b RS $0 }124 END { if (NR) P(b) }'125 output=$(126 set +o pipefail127 builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*128 __fzf_exec_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*bash: Fix CTRL-R batch deletionJunegunn Choibefore adoption58b2855
line 129why, one step back129 FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '"$'\t'"↳ ' --highlight-line --bind 'shift-delete:execute-silent(cat {+f1} >> \"$deletefile\")+exclude-multi' --multi ${FZF_CTRL_R_OPTS-} --read0") \bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 130–195why, one step back130 FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"131 )132 __fzf_history_delete "$deletefile"133 command rm -f "$deletefile"134 [[ -n $output ]] || return135 READLINE_LINE=${output#*$'\t'}136 if [[ -z $READLINE_POINT ]]; then137 echo "$READLINE_LINE"138 else139 READLINE_POINT=0x7fffffff140 fi141 }142fi143144# Required to refresh the prompt after fzf145bind -m emacs-standard '"\C-\e(": redraw-current-line'146147bind -m vi-command '"\C-z": emacs-editing-mode'148bind -m vi-insert '"\C-z": emacs-editing-mode'149bind -m emacs-standard '"\C-z": vi-editing-mode'150151if ((BASH_VERSINFO[0] < 4)); then152 # CTRL-T - Paste the selected file path into the command line153 if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then154 bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\C-\e(\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f\C-y\ey\C-_"'155 bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'156 bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'157 fi158159 # CTRL-R - Paste the selected command from history into the command line160 if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then161 if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then162 echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2163 fi164 bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\C-\e("'165 bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'166 bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'167 fi168else169 # CTRL-T - Paste the selected file path into the command line170 if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then171 bind -m emacs-standard -x '"\C-t": fzf-file-widget'172 bind -m vi-command -x '"\C-t": fzf-file-widget'173 bind -m vi-insert -x '"\C-t": fzf-file-widget'174 fi175176 # CTRL-R - Paste the selected command from history into the command line177 if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then178 if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then179 echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2180 fi181 bind -m emacs-standard -x '"\C-r": __fzf_history__'182 bind -m vi-command -x '"\C-r": __fzf_history__'183 bind -m vi-insert -x '"\C-r": __fzf_history__'184 fi185fi186187# ALT-C - cd into the selected directory188if [[ ${FZF_ALT_C_COMMAND-x} != "" ]]; then189 bind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\C-\e(\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d\C-y\ey\C-_"'190 bind -m vi-command '"\ec": "\C-z\ec\C-z"'191 bind -m vi-insert '"\ec": "\C-z\ec\C-z"'192fi193#----END shfmt194195fi