2020-03-31 22:18:09 +09:00
|
|
|
# ____ ____
|
|
|
|
|
# / __/___ / __/
|
|
|
|
|
# / /_/_ / / /_
|
|
|
|
|
# / __/ / /_/ __/
|
|
|
|
|
# /_/ /___/_/ key-bindings.bash
|
|
|
|
|
#
|
|
|
|
|
# - $FZF_CTRL_T_COMMAND
|
|
|
|
|
# - $FZF_CTRL_T_OPTS
|
2025-10-11 17:57:31 +01:00
|
|
|
# - $FZF_CTRL_R_COMMAND
|
2020-03-31 22:18:09 +09:00
|
|
|
# - $FZF_CTRL_R_OPTS
|
|
|
|
|
# - $FZF_ALT_C_COMMAND
|
|
|
|
|
# - $FZF_ALT_C_OPTS
|
|
|
|
|
|
2024-04-10 00:46:09 +09:00
|
|
|
if [[ $- =~ i ]]; then
|
2023-10-08 18:19:28 +02:00
|
|
|
|
|
|
|
|
|
2015-03-13 13:08:42 +09:00
|
|
|
# Key bindings
|
|
|
|
|
# ------------
|
2024-04-19 22:40:38 +09:00
|
|
|
|
2025-09-24 20:47:22 +09:00
|
|
|
#----BEGIN shfmt
|
2025-06-06 20:08:03 +09:00
|
|
|
#----BEGIN INCLUDE common.sh
|
|
|
|
|
# NOTE: Do not directly edit this section, which is copied from "common.sh".
|
2025-09-24 20:47:22 +09:00
|
|
|
# To modify it, one can edit "common.sh" and run "./update.sh" to apply
|
2025-06-06 20:08:03 +09:00
|
|
|
# the changes. See code comments in "common.sh" for the implementation details.
|
|
|
|
|
|
2024-04-19 22:40:38 +09:00
|
|
|
__fzf_defaults() {
|
2026-03-07 18:13:23 +09:00
|
|
|
builtin printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
|
2024-04-19 22:40:38 +09:00
|
|
|
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
|
2026-03-07 18:13:23 +09:00
|
|
|
builtin printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
|
2024-04-19 22:40:38 +09:00
|
|
|
}
|
|
|
|
|
|
2025-06-04 13:11:00 +09:00
|
|
|
__fzf_exec_awk() {
|
|
|
|
|
if [[ -z ${__fzf_awk-} ]]; then
|
|
|
|
|
__fzf_awk=awk
|
2025-06-04 14:07:01 +09:00
|
|
|
if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
|
|
|
|
|
__fzf_awk=/usr/xpg4/bin/awk
|
2025-09-24 22:09:37 +09:00
|
|
|
elif command -v mawk > /dev/null 2>&1; then
|
2025-06-04 14:07:01 +09:00
|
|
|
local n x y z d
|
2025-07-25 10:33:18 +02:00
|
|
|
IFS=' .' read -r n x y z d <<< $(command mawk -W version 2> /dev/null)
|
[bash,zsh] Fix the version check for mawk (#4574)
We have been checking the mawk version by extracting <x>, <y>, <z>,
and <d> part from "mawk <x>.<y>.<z> <d>" in the output of the "mawk -W
version" and testing <x>, <y>, <z>, and <d> using an arithmetic
evalaution. However, <d> is ensured to be an integer only in "x.y.z
>= 1.3.4". Otherwise, it may cause a syntax error in the arithmetic
evaluation. The mawk started to include the date as an integer in the
<d> position only from mawk-1.3.3-20090721. We should first check
that "x.y.z >= 1.3.4" and then check the value of "d". In case, "mawk
-W version" produces a completely different text, we should also
redirect stderr of the arithmetic commands to /dev/null.
2025-10-31 21:14:41 +09:00
|
|
|
[[ $n == mawk ]] &&
|
|
|
|
|
(((x * 1000 + y) * 1000 + z >= 1003004)) 2> /dev/null &&
|
|
|
|
|
((d >= 20230302)) 2> /dev/null &&
|
|
|
|
|
__fzf_awk=mawk
|
2025-06-04 14:07:01 +09:00
|
|
|
fi
|
2025-06-04 13:11:00 +09:00
|
|
|
fi
|
|
|
|
|
LC_ALL=C exec "$__fzf_awk" "$@"
|
|
|
|
|
}
|
2025-06-06 20:08:03 +09:00
|
|
|
#----END INCLUDE
|
2025-06-04 13:11:00 +09:00
|
|
|
|
2015-04-22 00:30:09 +09:00
|
|
|
__fzf_select__() {
|
2024-04-19 22:40:38 +09:00
|
|
|
FZF_DEFAULT_COMMAND=${FZF_CTRL_T_COMMAND:-} \
|
2025-09-24 22:09:37 +09:00
|
|
|
FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=file,dir,follow,hidden --scheme=path" "${FZF_CTRL_T_OPTS-} -m") \
|
|
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) "$@" |
|
2022-04-29 12:04:16 +02:00
|
|
|
while read -r item; do
|
2025-09-24 22:09:37 +09:00
|
|
|
printf '%q ' "$item" # escape special chars
|
2022-04-29 12:04:16 +02:00
|
|
|
done
|
2015-03-13 13:08:42 +09:00
|
|
|
}
|
|
|
|
|
|
2015-04-22 00:55:39 +09:00
|
|
|
__fzfcmd() {
|
2025-09-24 22:09:37 +09:00
|
|
|
[[ -n ${TMUX_PANE-} ]] && { [[ ${FZF_TMUX:-0} != 0 ]] || [[ -n ${FZF_TMUX_OPTS-} ]]; } &&
|
2020-03-31 22:18:09 +09:00
|
|
|
echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"
|
2015-03-13 13:08:42 +09:00
|
|
|
}
|
|
|
|
|
|
2016-04-24 14:04:15 +09:00
|
|
|
fzf-file-widget() {
|
2022-04-06 04:29:01 +02:00
|
|
|
local selected="$(__fzf_select__ "$@")"
|
2025-09-24 22:09:37 +09:00
|
|
|
READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}$selected${READLINE_LINE:READLINE_POINT}"
|
|
|
|
|
READLINE_POINT=$((READLINE_POINT + ${#selected}))
|
2015-12-10 19:32:19 -08:00
|
|
|
}
|
|
|
|
|
|
2015-04-22 00:30:09 +09:00
|
|
|
__fzf_cd__() {
|
2024-04-19 22:40:38 +09:00
|
|
|
local dir
|
2024-03-13 20:56:31 +09:00
|
|
|
dir=$(
|
2024-04-19 22:40:38 +09:00
|
|
|
FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \
|
2025-09-24 22:09:37 +09:00
|
|
|
FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \
|
|
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd)
|
2024-03-30 13:13:15 -03:00
|
|
|
) && printf 'builtin cd -- %q' "$(builtin unset CDPATH && builtin cd -- "$dir" && builtin pwd)"
|
2015-04-22 00:30:09 +09:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 21:10:43 +09:00
|
|
|
__fzf_history_delete() {
|
|
|
|
|
[[ -s $1 ]] || return
|
|
|
|
|
|
|
|
|
|
local offsets
|
|
|
|
|
offsets=($(sort -rnu "$1"))
|
|
|
|
|
for offset in "${offsets[@]}"; do
|
|
|
|
|
builtin history -d "$offset"
|
|
|
|
|
done
|
2026-04-11 17:02:34 +09:00
|
|
|
|
|
|
|
|
if [[ ${#offsets[@]} -gt 0 ]] && shopt -q histappend; then
|
|
|
|
|
builtin history -w
|
|
|
|
|
fi
|
2026-03-21 21:10:43 +09:00
|
|
|
}
|
|
|
|
|
|
[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 10:37:34 +02:00
|
|
|
if command -v perl > /dev/null; then
|
|
|
|
|
__fzf_history__() {
|
2026-03-21 21:10:43 +09:00
|
|
|
local output script deletefile
|
|
|
|
|
deletefile=$(mktemp)
|
2024-05-30 10:23:45 +09:00
|
|
|
script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; s/\n/\n\t/gm; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++'
|
[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 10:37:34 +02:00
|
|
|
output=$(
|
|
|
|
|
set +o pipefail
|
|
|
|
|
builtin fc -lnr -2147483648 |
|
[bash] Improve mawk detection (#3463)
* Use the all-compatible mawk `-W version` option.
https://github.com/junegunn/fzf/pull/3313#issuecomment-1747934690.
* Run the command and not a function consistently with #3462.
The version check bash code relies on the following mawk source code,
extracted from mawk 1.3.4 20230322.
```
version.c:
18- #include "init.h"
19- #include "patchlev.h"
20-
21: #define VERSION_STRING \
22- "mawk %d.%d%s %s\n\
23- Copyright 2008-2022,2023, Thomas E. Dickey\n\
24- Copyright 1991-1996,2014, Michael D. Brennan\n\n"
....
30- void
31- print_version(FILE *fp)
32- {
33: fprintf(fp, VERSION_STRING, PATCH_BASE, PATCH_LEVEL, PATCH_STRING, DATE_STRING);
34- fflush(fp);
35-
36- #define SHOW_RANDOM "random-funcs:"
patchlev.h:
13- /*
14- * $MawkId: patchlev.h,v 1.128 2023/03/23 00:23:57 tom Exp $
15- */
16: #define PATCH_BASE 1
17- #define PATCH_LEVEL 3
18- #define PATCH_STRING ".4"
19- #define DATE_STRING "20230322"
```
Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
2023-10-07 10:26:46 +02:00
|
|
|
last_hist=$(HISTTIMEFORMAT='' builtin history 1) command perl -n -l0 -e "$script" |
|
2026-03-22 09:19:54 +09:00
|
|
|
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") \
|
2024-04-19 22:40:38 +09:00
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
|
2026-03-21 21:10:43 +09:00
|
|
|
)
|
|
|
|
|
__fzf_history_delete "$deletefile"
|
|
|
|
|
command rm -f "$deletefile"
|
|
|
|
|
[[ -n $output ]] || return
|
2024-05-30 10:23:45 +09:00
|
|
|
READLINE_LINE=$(command perl -pe 's/^\d*\t//' <<< "$output")
|
2025-09-24 22:09:37 +09:00
|
|
|
if [[ -z $READLINE_POINT ]]; then
|
[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 10:37:34 +02:00
|
|
|
echo "$READLINE_LINE"
|
|
|
|
|
else
|
|
|
|
|
READLINE_POINT=0x7fffffff
|
|
|
|
|
fi
|
|
|
|
|
}
|
[bash] Improve mawk detection (#3463)
* Use the all-compatible mawk `-W version` option.
https://github.com/junegunn/fzf/pull/3313#issuecomment-1747934690.
* Run the command and not a function consistently with #3462.
The version check bash code relies on the following mawk source code,
extracted from mawk 1.3.4 20230322.
```
version.c:
18- #include "init.h"
19- #include "patchlev.h"
20-
21: #define VERSION_STRING \
22- "mawk %d.%d%s %s\n\
23- Copyright 2008-2022,2023, Thomas E. Dickey\n\
24- Copyright 1991-1996,2014, Michael D. Brennan\n\n"
....
30- void
31- print_version(FILE *fp)
32- {
33: fprintf(fp, VERSION_STRING, PATCH_BASE, PATCH_LEVEL, PATCH_STRING, DATE_STRING);
34- fflush(fp);
35-
36- #define SHOW_RANDOM "random-funcs:"
patchlev.h:
13- /*
14- * $MawkId: patchlev.h,v 1.128 2023/03/23 00:23:57 tom Exp $
15- */
16: #define PATCH_BASE 1
17- #define PATCH_LEVEL 3
18- #define PATCH_STRING ".4"
19- #define DATE_STRING "20230322"
```
Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
2023-10-07 10:26:46 +02:00
|
|
|
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 10:37:34 +02:00
|
|
|
__fzf_history__() {
|
2026-03-21 21:10:43 +09:00
|
|
|
local output script deletefile
|
|
|
|
|
deletefile=$(mktemp)
|
2025-09-24 22:09:37 +09:00
|
|
|
[[ $(HISTTIMEFORMAT='' builtin history 1) =~ [[:digit:]]+ ]] # how many history entries
|
[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 10:37:34 +02:00
|
|
|
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
|
2025-09-24 22:09:37 +09:00
|
|
|
builtin fc -lnr -2147483648 2> /dev/null | # ( $'\t '<lines>$'\n' )* ; <lines> ::= [^\n]* ( $'\n'<lines> )*
|
|
|
|
|
__fzf_exec_awk "$script" | # ( <counter>$'\t'<lines>$'\000' )*
|
2026-03-22 09:19:54 +09:00
|
|
|
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") \
|
2024-04-19 22:40:38 +09:00
|
|
|
FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) --query "$READLINE_LINE"
|
2026-03-21 21:10:43 +09:00
|
|
|
)
|
|
|
|
|
__fzf_history_delete "$deletefile"
|
|
|
|
|
command rm -f "$deletefile"
|
|
|
|
|
[[ -n $output ]] || return
|
[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 10:37:34 +02:00
|
|
|
READLINE_LINE=${output#*$'\t'}
|
2025-09-24 22:09:37 +09:00
|
|
|
if [[ -z $READLINE_POINT ]]; then
|
[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 10:37:34 +02:00
|
|
|
echo "$READLINE_LINE"
|
|
|
|
|
else
|
|
|
|
|
READLINE_POINT=0x7fffffff
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
fi
|
2015-03-13 13:08:42 +09:00
|
|
|
|
2020-01-08 02:35:43 -07:00
|
|
|
# Required to refresh the prompt after fzf
|
2026-01-02 09:44:49 -07:00
|
|
|
bind -m emacs-standard '"\C-\e(": redraw-current-line'
|
2020-01-08 02:35:43 -07:00
|
|
|
|
2020-02-20 17:51:34 -07:00
|
|
|
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'
|
2020-01-08 02:35:43 -07:00
|
|
|
|
2025-09-24 22:09:37 +09:00
|
|
|
if ((BASH_VERSINFO[0] < 4)); then
|
2020-02-28 02:47:13 -07:00
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
2025-09-24 22:09:37 +09:00
|
|
|
if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then
|
2026-01-02 09:44:49 -07:00
|
|
|
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-_"'
|
2024-03-17 03:06:48 -04:00
|
|
|
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
|
2015-03-13 13:08:42 +09:00
|
|
|
|
2020-02-28 02:47:13 -07:00
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
2025-10-11 17:57:31 +01:00
|
|
|
if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then
|
|
|
|
|
if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then
|
|
|
|
|
echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2
|
|
|
|
|
fi
|
2026-01-02 09:44:49 -07:00
|
|
|
bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\C-\e("'
|
2025-10-11 17:57:31 +01:00
|
|
|
bind -m vi-command '"\C-r": "\C-z\C-r\C-z"'
|
|
|
|
|
bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'
|
|
|
|
|
fi
|
2020-02-28 02:47:13 -07:00
|
|
|
else
|
|
|
|
|
# CTRL-T - Paste the selected file path into the command line
|
2025-09-24 22:09:37 +09:00
|
|
|
if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then
|
2024-03-17 03:06:48 -04:00
|
|
|
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
|
2020-02-28 02:47:13 -07:00
|
|
|
|
|
|
|
|
# CTRL-R - Paste the selected command from history into the command line
|
2025-10-11 17:57:31 +01:00
|
|
|
if [[ ${FZF_CTRL_R_COMMAND-x} != "" ]]; then
|
|
|
|
|
if [[ -n ${FZF_CTRL_R_COMMAND-} ]]; then
|
|
|
|
|
echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2
|
|
|
|
|
fi
|
|
|
|
|
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
|
2020-02-28 02:47:13 -07:00
|
|
|
fi
|
2015-03-13 13:08:42 +09:00
|
|
|
|
2020-01-08 02:35:43 -07:00
|
|
|
# ALT-C - cd into the selected directory
|
2025-09-24 22:09:37 +09:00
|
|
|
if [[ ${FZF_ALT_C_COMMAND-x} != "" ]]; then
|
2026-01-02 09:44:49 -07:00
|
|
|
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-_"'
|
2024-03-17 03:06:48 -04:00
|
|
|
bind -m vi-command '"\ec": "\C-z\ec\C-z"'
|
|
|
|
|
bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
|
|
|
|
|
fi
|
2025-09-24 20:47:22 +09:00
|
|
|
#----END shfmt
|
2024-04-10 00:46:09 +09:00
|
|
|
|
|
|
|
|
fi
|