From 0e9026b817696baed3ff48be8ecf2b6352586c7b Mon Sep 17 00:00:00 2001 From: Jacobo de Vera Date: Sat, 11 Oct 2025 17:57:31 +0100 Subject: [PATCH] feat: Allow disabling Ctrl-R binding in shell integration (#4535) Close #4417 --- CHANGELOG.md | 12 ++++++++++++ README.md | 15 +++++++++------ shell/key-bindings.bash | 23 +++++++++++++++++------ shell/key-bindings.fish | 10 ++++++++-- shell/key-bindings.zsh | 14 ++++++++++---- 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61ea3a28..db5be813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ This version introduces many new features centered around the new "raw" mode. | Enhancement | Key | `CTRL-N` | `down` -> `down-match` | | Enhancement | Key | `CTRL-P` | `up` -> `up-match` | | Enhancement | Shell | `CTRL-R` binding | Toggle raw mode with `ALT-R` | +| Enhancement | Shell | `CTRL-R` binding | Opt-out with an empty `FZF_CTRL_R_COMMAND` | ### Introducing "raw" mode @@ -231,6 +232,17 @@ As described above, `$FZF_RAW` is now exported to child processes in raw mode, indicating whether the current item is a match (`1`) or not (`0`). It is not defined when not in raw mode. +#### `$FZF_CTRL_R_COMMAND` + +You can opt-out `CTRL-R` binding from the shell integration by setting +`FZF_CTRL_R_COMMAND` to an empty string. Setting it to any other value is not +supported and will result in a warning. + +```sh +# Disable the CTRL-R binding from the shell integration +FZF_CTRL_R_COMMAND= eval "$(fzf --bash)" +``` + ### Added key support for `--bind` Pull request #3996 added support for many additional keys for `--bind` option, diff --git a/README.md b/README.md index 312a99c3..d0dd5c05 100644 --- a/README.md +++ b/README.md @@ -218,13 +218,13 @@ Add the following line to your shell configuration file. > (e.g. `apt show fzf`) > [!TIP] -> You can disable CTRL-T or ALT-C binding by setting `FZF_CTRL_T_COMMAND` or -> `FZF_ALT_C_COMMAND` to an empty string when sourcing the script. -> For example, to disable ALT-C binding: +> You can disable CTRL-T, CTRL-R, or ALT-C bindings by setting the +> corresponding `*_COMMAND` variable to an empty string when sourcing the +> script. For example, to disable CTRL-R and ALT-C: > -> * bash: `FZF_ALT_C_COMMAND= eval "$(fzf --bash)"` -> * zsh: `FZF_ALT_C_COMMAND= source <(fzf --zsh)` -> * fish: `fzf --fish | FZF_ALT_C_COMMAND= source` +> * bash: `FZF_CTRL_R_COMMAND= FZF_ALT_C_COMMAND= eval "$(fzf --bash)"` +> * zsh: `FZF_CTRL_R_COMMAND= FZF_ALT_C_COMMAND= source <(fzf --zsh)` +> * fish: `fzf --fish | FZF_CTRL_R_COMMAND= FZF_ALT_C_COMMAND= source` > > Setting the variables after sourcing the script will have no effect. @@ -534,6 +534,9 @@ the following key bindings in bash, zsh, and fish. --color header:italic --header 'Press CTRL-Y to copy command into clipboard'" ``` + - Can be disabled by setting `FZF_CTRL_R_COMMAND` to an empty string when + sourcing the script + - Custom override via a non-empty `FZF_CTRL_R_COMMAND` is not yet supported and will emit a warning - `ALT-C` - cd into the selected directory - The list is generated using `--walker dir,follow,hidden` option - Set `FZF_ALT_C_COMMAND` to override the default command diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index b335e6de..30b776a5 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -7,6 +7,7 @@ # - $FZF_TMUX_OPTS # - $FZF_CTRL_T_COMMAND # - $FZF_CTRL_T_OPTS +# - $FZF_CTRL_R_COMMAND # - $FZF_CTRL_R_OPTS # - $FZF_ALT_C_COMMAND # - $FZF_ALT_C_OPTS @@ -132,9 +133,14 @@ if ((BASH_VERSINFO[0] < 4)); then 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"' + 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 '"\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"' + fi else # CTRL-T - Paste the selected file path into the command line if [[ ${FZF_CTRL_T_COMMAND-x} != "" ]]; then @@ -144,9 +150,14 @@ else 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__' + 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 fi # ALT-C - cd into the selected directory diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish index 1bebaf30..2b44dfd0 100644 --- a/shell/key-bindings.fish +++ b/shell/key-bindings.fish @@ -7,6 +7,7 @@ # - $FZF_TMUX_OPTS # - $FZF_CTRL_T_COMMAND # - $FZF_CTRL_T_OPTS +# - $FZF_CTRL_R_COMMAND # - $FZF_CTRL_R_OPTS # - $FZF_ALT_C_COMMAND # - $FZF_ALT_C_OPTS @@ -214,8 +215,13 @@ function fzf_key_bindings commandline -f repaint end - bind \cr fzf-history-widget - bind -M insert \cr fzf-history-widget + if not set -q FZF_CTRL_R_COMMAND; or test -n "$FZF_CTRL_R_COMMAND" + if test -n "$FZF_CTRL_R_COMMAND" + echo "warning: FZF_CTRL_R_COMMAND is set to a custom command, but custom commands are not yet supported for CTRL-R" >&2 + end + bind \cr fzf-history-widget + bind -M insert \cr fzf-history-widget + end if not set -q FZF_CTRL_T_COMMAND; or test -n "$FZF_CTRL_T_COMMAND" bind \ct fzf-file-widget diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index 15fb5ef0..36c03499 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -7,6 +7,7 @@ # - $FZF_TMUX_OPTS # - $FZF_CTRL_T_COMMAND # - $FZF_CTRL_T_OPTS +# - $FZF_CTRL_R_COMMAND # - $FZF_CTRL_R_OPTS # - $FZF_ALT_C_COMMAND # - $FZF_ALT_C_OPTS @@ -150,10 +151,15 @@ fzf-history-widget() { zle reset-prompt return $ret } -zle -N fzf-history-widget -bindkey -M emacs '^R' fzf-history-widget -bindkey -M vicmd '^R' fzf-history-widget -bindkey -M viins '^R' fzf-history-widget +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 + zle -N fzf-history-widget + bindkey -M emacs '^R' fzf-history-widget + bindkey -M vicmd '^R' fzf-history-widget + bindkey -M viins '^R' fzf-history-widget +fi fi } always {