fzf/shell/key-bindings.bash

137 lines
5.2 KiB
Bash
Raw Normal View History

# ____ ____
# / __/___ / __/
# / /_/_ / / /_
# / __/ / /_/ __/
# /_/ /___/_/ key-bindings.bash
#
# - $FZF_TMUX_OPTS
# - $FZF_CTRL_T_COMMAND
# - $FZF_CTRL_T_OPTS
# - $FZF_CTRL_R_OPTS
# - $FZF_ALT_C_COMMAND
# - $FZF_ALT_C_OPTS
if [[ $- =~ i ]]; then
# Key bindings
# ------------
__fzf_select__() {
local opts
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse --walker=file,dir,follow,hidden --scheme=path ${FZF_DEFAULT_OPTS-} ${FZF_CTRL_T_OPTS-} -m"
FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) "$@" |
while read -r item; do
printf '%q ' "$item" # escape special chars
done
}
__fzfcmd() {
[[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; } &&
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
}
fzf-file-widget() {
local selected="$(__fzf_select__ "$@")"
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}
__fzf_cd__() {
local opts dir
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse --walker=dir,follow,hidden --scheme=path ${FZF_DEFAULT_OPTS-} ${FZF_ALT_C_OPTS-} +m"
dir=$(
FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)
) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"
}
[bash] History, use perl if installed otherwise awk (#3313) While awk is POSIX, perl isn't pre-installed on all *nix flavors. This commit eliminates the mandatory dependency on perl by using awk when perl is not available. Related: #3295, #3309, #3310. Test suite passed: * `make error` all test sections 'PASS' * `make docker-test` 215 runs, 1884 assertions, 0 failures, 0 errors, 0 skips. Manually tested in the following environments: * Linux amd64 with bash 3.2, 4.4, 5.2; gawk -P, one true awk, mawk, busybox awk. * macOS Catalina, bash 3.2, macOS awk 20070501. **Performance comparison:** Mawk turned out the fastest, then perl. One true awk's implementation should be the closest to macOS awk. Test data: 230 KB history, 15102 entries, including multi-line and duplicates. Linux, bash 4.4. Times in milliseconds. | Command | Mean | Min | Max | Relative | | :--- | ---: | ---: | ---: | -------: | | `mawk 1.3.4` | 22.9 | 22.3 | 25.6 | **1.00** | | `perl 5.26.1` | 34.3 | 33.6 | 35.1 | 1.49 | | `one true awk 20221215` | 41.9 | 40.6 | 46.3 | 1.83 | | `gawk 5.1.0` | 46.1 | 44.4 | 50.3 | 2.01 | | `busybox awk 1.27.0` | 64.8 | 63.2 | 70.0 | 2.82 | **Other Notes** A bug affects bash, which fails restoring a saved multi-line history entry as a single entry. Bug fixed in version 5.0.[^1] While developing this PR I discovered two unsubmitted issues affecting the current perl script. The output stream ends with `$'\n\0000'` instead of `$'\0000'`. Because of this, the script does not deduplicate a duplicated entry located at the end of the history list; therefore fzf displays two identical (not necessarily adjacent) entries. A minor point about the first issue is that the top fzf entry ends with a dangling line feed symbol, which is visible in the terminal. [^1]: https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/CHANGES#L1511 To enable: `shopt -s cmdhist lithist; HISTTIMEFORMAT='%F %T '`.
2023-09-22 08:37:34 +00:00
if command -v perl > /dev/null; then
__fzf_history__() {
local output opts script
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0"
script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'
output=$(
set +o pipefail
builtin fc -lnr -2147483648 |
last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |
[bash] History, use perl if installed otherwise awk (#3313) While awk is POSIX, perl isn't pre-installed on all *nix flavors. This commit eliminates the mandatory dependency on perl by using awk when perl is not available. Related: #3295, #3309, #3310. Test suite passed: * `make error` all test sections 'PASS' * `make docker-test` 215 runs, 1884 assertions, 0 failures, 0 errors, 0 skips. Manually tested in the following environments: * Linux amd64 with bash 3.2, 4.4, 5.2; gawk -P, one true awk, mawk, busybox awk. * macOS Catalina, bash 3.2, macOS awk 20070501. **Performance comparison:** Mawk turned out the fastest, then perl. One true awk's implementation should be the closest to macOS awk. Test data: 230 KB history, 15102 entries, including multi-line and duplicates. Linux, bash 4.4. Times in milliseconds. | Command | Mean | Min | Max | Relative | | :--- | ---: | ---: | ---: | -------: | | `mawk 1.3.4` | 22.9 | 22.3 | 25.6 | **1.00** | | `perl 5.26.1` | 34.3 | 33.6 | 35.1 | 1.49 | | `one true awk 20221215` | 41.9 | 40.6 | 46.3 | 1.83 | | `gawk 5.1.0` | 46.1 | 44.4 | 50.3 | 2.01 | | `busybox awk 1.27.0` | 64.8 | 63.2 | 70.0 | 2.82 | **Other Notes** A bug affects bash, which fails restoring a saved multi-line history entry as a single entry. Bug fixed in version 5.0.[^1] While developing this PR I discovered two unsubmitted issues affecting the current perl script. The output stream ends with `$'\n\0000'` instead of `$'\0000'`. Because of this, the script does not deduplicate a duplicated entry located at the end of the history list; therefore fzf displays two identical (not necessarily adjacent) entries. A minor point about the first issue is that the top fzf entry ends with a dangling line feed symbol, which is visible in the terminal. [^1]: https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/CHANGES#L1511 To enable: `shopt -s cmdhist lithist; HISTTIMEFORMAT='%F %T '`.
2023-09-22 08:37:34 +00:00
FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE"
) || return
READLINE_LINE=${output#*$'\t'}
if [[ -z "$READLINE_POINT" ]]; then
echo "$READLINE_LINE"
else
READLINE_POINT=0x7fffffff
fi
}
else # awk - fallback for POSIX systems
[bash] History, use perl if installed otherwise awk (#3313) While awk is POSIX, perl isn't pre-installed on all *nix flavors. This commit eliminates the mandatory dependency on perl by using awk when perl is not available. Related: #3295, #3309, #3310. Test suite passed: * `make error` all test sections 'PASS' * `make docker-test` 215 runs, 1884 assertions, 0 failures, 0 errors, 0 skips. Manually tested in the following environments: * Linux amd64 with bash 3.2, 4.4, 5.2; gawk -P, one true awk, mawk, busybox awk. * macOS Catalina, bash 3.2, macOS awk 20070501. **Performance comparison:** Mawk turned out the fastest, then perl. One true awk's implementation should be the closest to macOS awk. Test data: 230 KB history, 15102 entries, including multi-line and duplicates. Linux, bash 4.4. Times in milliseconds. | Command | Mean | Min | Max | Relative | | :--- | ---: | ---: | ---: | -------: | | `mawk 1.3.4` | 22.9 | 22.3 | 25.6 | **1.00** | | `perl 5.26.1` | 34.3 | 33.6 | 35.1 | 1.49 | | `one true awk 20221215` | 41.9 | 40.6 | 46.3 | 1.83 | | `gawk 5.1.0` | 46.1 | 44.4 | 50.3 | 2.01 | | `busybox awk 1.27.0` | 64.8 | 63.2 | 70.0 | 2.82 | **Other Notes** A bug affects bash, which fails restoring a saved multi-line history entry as a single entry. Bug fixed in version 5.0.[^1] While developing this PR I discovered two unsubmitted issues affecting the current perl script. The output stream ends with `$'\n\0000'` instead of `$'\0000'`. Because of this, the script does not deduplicate a duplicated entry located at the end of the history list; therefore fzf displays two identical (not necessarily adjacent) entries. A minor point about the first issue is that the top fzf entry ends with a dangling line feed symbol, which is visible in the terminal. [^1]: https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/CHANGES#L1511 To enable: `shopt -s cmdhist lithist; HISTTIMEFORMAT='%F %T '`.
2023-09-22 08:37:34 +00:00
__fzf_history__() {
local output opts script n x y z d
[bash] History, use perl if installed otherwise awk (#3313) While awk is POSIX, perl isn't pre-installed on all *nix flavors. This commit eliminates the mandatory dependency on perl by using awk when perl is not available. Related: #3295, #3309, #3310. Test suite passed: * `make error` all test sections 'PASS' * `make docker-test` 215 runs, 1884 assertions, 0 failures, 0 errors, 0 skips. Manually tested in the following environments: * Linux amd64 with bash 3.2, 4.4, 5.2; gawk -P, one true awk, mawk, busybox awk. * macOS Catalina, bash 3.2, macOS awk 20070501. **Performance comparison:** Mawk turned out the fastest, then perl. One true awk's implementation should be the closest to macOS awk. Test data: 230 KB history, 15102 entries, including multi-line and duplicates. Linux, bash 4.4. Times in milliseconds. | Command | Mean | Min | Max | Relative | | :--- | ---: | ---: | ---: | -------: | | `mawk 1.3.4` | 22.9 | 22.3 | 25.6 | **1.00** | | `perl 5.26.1` | 34.3 | 33.6 | 35.1 | 1.49 | | `one true awk 20221215` | 41.9 | 40.6 | 46.3 | 1.83 | | `gawk 5.1.0` | 46.1 | 44.4 | 50.3 | 2.01 | | `busybox awk 1.27.0` | 64.8 | 63.2 | 70.0 | 2.82 | **Other Notes** A bug affects bash, which fails restoring a saved multi-line history entry as a single entry. Bug fixed in version 5.0.[^1] While developing this PR I discovered two unsubmitted issues affecting the current perl script. The output stream ends with `$'\n\0000'` instead of `$'\0000'`. Because of this, the script does not deduplicate a duplicated entry located at the end of the history list; therefore fzf displays two identical (not necessarily adjacent) entries. A minor point about the first issue is that the top fzf entry ends with a dangling line feed symbol, which is visible in the terminal. [^1]: https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/CHANGES#L1511 To enable: `shopt -s cmdhist lithist; HISTTIMEFORMAT='%F %T '`.
2023-09-22 08:37:34 +00:00
if [[ -z $__fzf_awk ]]; then
__fzf_awk=awk
# choose the faster mawk if: it's installed && build date >= 20230322 && version >= 1.3.4
IFS=' .' read n x y z d <<< $(command mawk -W version 2> /dev/null)
[[ $n == mawk ]] && (( d >= 20230302 && (x *1000 +y) *1000 +z >= 1003004 )) && __fzf_awk=mawk
[bash] History, use perl if installed otherwise awk (#3313) While awk is POSIX, perl isn't pre-installed on all *nix flavors. This commit eliminates the mandatory dependency on perl by using awk when perl is not available. Related: #3295, #3309, #3310. Test suite passed: * `make error` all test sections 'PASS' * `make docker-test` 215 runs, 1884 assertions, 0 failures, 0 errors, 0 skips. Manually tested in the following environments: * Linux amd64 with bash 3.2, 4.4, 5.2; gawk -P, one true awk, mawk, busybox awk. * macOS Catalina, bash 3.2, macOS awk 20070501. **Performance comparison:** Mawk turned out the fastest, then perl. One true awk's implementation should be the closest to macOS awk. Test data: 230 KB history, 15102 entries, including multi-line and duplicates. Linux, bash 4.4. Times in milliseconds. | Command | Mean | Min | Max | Relative | | :--- | ---: | ---: | ---: | -------: | | `mawk 1.3.4` | 22.9 | 22.3 | 25.6 | **1.00** | | `perl 5.26.1` | 34.3 | 33.6 | 35.1 | 1.49 | | `one true awk 20221215` | 41.9 | 40.6 | 46.3 | 1.83 | | `gawk 5.1.0` | 46.1 | 44.4 | 50.3 | 2.01 | | `busybox awk 1.27.0` | 64.8 | 63.2 | 70.0 | 2.82 | **Other Notes** A bug affects bash, which fails restoring a saved multi-line history entry as a single entry. Bug fixed in version 5.0.[^1] While developing this PR I discovered two unsubmitted issues affecting the current perl script. The output stream ends with `$'\n\0000'` instead of `$'\0000'`. Because of this, the script does not deduplicate a duplicated entry located at the end of the history list; therefore fzf displays two identical (not necessarily adjacent) entries. A minor point about the first issue is that the top fzf entry ends with a dangling line feed symbol, which is visible in the terminal. [^1]: https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/CHANGES#L1511 To enable: `shopt -s cmdhist lithist; HISTTIMEFORMAT='%F %T '`.
2023-09-22 08:37:34 +00:00
fi
opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0"
[[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries
script='function P(b) { ++n; sub(/^[ *]/, "", b); if (!seen[b]++) { printf "%d\t%s%c", '$((BASH_REMATCH + 1))' - n, b, 0 } }
NR==1 { b = substr($0, 2); next }
/^\t/ { P(b); b = substr($0, 2); next }
{ b = b RS $0 }
END { if (NR) P(b) }'
output=$(
set +o pipefail
builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*
command $__fzf_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*
[bash] History, use perl if installed otherwise awk (#3313) While awk is POSIX, perl isn't pre-installed on all *nix flavors. This commit eliminates the mandatory dependency on perl by using awk when perl is not available. Related: #3295, #3309, #3310. Test suite passed: * `make error` all test sections 'PASS' * `make docker-test` 215 runs, 1884 assertions, 0 failures, 0 errors, 0 skips. Manually tested in the following environments: * Linux amd64 with bash 3.2, 4.4, 5.2; gawk -P, one true awk, mawk, busybox awk. * macOS Catalina, bash 3.2, macOS awk 20070501. **Performance comparison:** Mawk turned out the fastest, then perl. One true awk's implementation should be the closest to macOS awk. Test data: 230 KB history, 15102 entries, including multi-line and duplicates. Linux, bash 4.4. Times in milliseconds. | Command | Mean | Min | Max | Relative | | :--- | ---: | ---: | ---: | -------: | | `mawk 1.3.4` | 22.9 | 22.3 | 25.6 | **1.00** | | `perl 5.26.1` | 34.3 | 33.6 | 35.1 | 1.49 | | `one true awk 20221215` | 41.9 | 40.6 | 46.3 | 1.83 | | `gawk 5.1.0` | 46.1 | 44.4 | 50.3 | 2.01 | | `busybox awk 1.27.0` | 64.8 | 63.2 | 70.0 | 2.82 | **Other Notes** A bug affects bash, which fails restoring a saved multi-line history entry as a single entry. Bug fixed in version 5.0.[^1] While developing this PR I discovered two unsubmitted issues affecting the current perl script. The output stream ends with `$'\n\0000'` instead of `$'\0000'`. Because of this, the script does not deduplicate a duplicated entry located at the end of the history list; therefore fzf displays two identical (not necessarily adjacent) entries. A minor point about the first issue is that the top fzf entry ends with a dangling line feed symbol, which is visible in the terminal. [^1]: https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/CHANGES#L1511 To enable: `shopt -s cmdhist lithist; HISTTIMEFORMAT='%F %T '`.
2023-09-22 08:37:34 +00:00
FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) --query "$READLINE_LINE"
) || return
READLINE_LINE=${output#*$'\t'}
if [[ -z "$READLINE_POINT" ]]; then
echo "$READLINE_LINE"
else
READLINE_POINT=0x7fffffff
fi
}
fi
# Required to refresh the prompt after fzf
bind -m emacs-standard '"\er": redraw-current-line'
bind -m vi-command '"\C-z": emacs-editing-mode'
bind -m vi-insert '"\C-z": emacs-editing-mode'
bind -m emacs-standard '"\C-z": vi-editing-mode'
2021-08-14 11:33:21 +00:00
if (( BASH_VERSINFO[0] < 4 )); then
# CTRL-T - Paste the selected file path into the command line
if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
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"'
bind -m vi-command '"\C-t": "\C-z\C-t\C-z"'
bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
fi
# CTRL-R - Paste the selected command from history into the command line
bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er"'
bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'
bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'
else
# CTRL-T - Paste the selected file path into the command line
if [[ "${FZF_CTRL_T_COMMAND-x}" != "" ]]; then
bind -m emacs-standard -x '"\C-t": fzf-file-widget'
bind -m vi-command -x '"\C-t": fzf-file-widget'
bind -m vi-insert -x '"\C-t": fzf-file-widget'
fi
# CTRL-R - Paste the selected command from history into the command line
bind -m emacs-standard -x '"\C-r": __fzf_history__'
bind -m vi-command -x '"\C-r": __fzf_history__'
bind -m vi-insert -x '"\C-r": __fzf_history__'
fi
# ALT-C - cd into the selected directory
if [[ "${FZF_ALT_C_COMMAND-x}" != "" ]]; then
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"'
bind -m vi-command '"\ec": "\C-z\ec\C-z"'
bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
fi
fi