From 0af6c9fb24293c4b3dc8eb3bd0c277cb7d8e7ee4 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Wed, 6 Aug 2025 13:15:01 +0200 Subject: [PATCH] zsh:keys: Fix override of fzf-cd-widget This commit addresses a couple of issues: 1. fzf's `key-bindings.zsh` was sourced twice: Once in keys.zsh and once via the symlink in `external-plugins/`. Fix this by removing the source in `keys.zsh`. 2. The section about the fzf bindings in keys.zsh was a mess. Reorder and rewrite some comments to make it nicer. 3. My just added custom `fzf-cd-inplace-widget` does not work like this. Since `key-bindings.zsh` is sourced (was sourced again) after `keys.zsh`, my override is overridden itself by the default. I noticed and fixed this when I originally wrote and tested the function, but unfortunately I forgot the old `bindkey` commands in `keys.zsh`, which is why I now forgot about it and committed this erroneously. To fix this, bind the keys after sourcing `key-binding.zsh` in the newly added `fzf.key-bindings.config.zsh`. Additionally, move the widget definition as well to have everything at one place. Fixes: 3cf445e73923 (zsh:keys: Modify fzf's cd widget to be "in-prompt", 2025-08-06) --- .config/zsh/zshrc.d/60-keys.zsh | 41 +++++-------------- .../55-fzf.key-bindings.config.zsh | 25 +++++++++++ 2 files changed, 35 insertions(+), 31 deletions(-) create mode 100644 .config/zsh/zshrc.d/90-external-plugins/55-fzf.key-bindings.config.zsh diff --git a/.config/zsh/zshrc.d/60-keys.zsh b/.config/zsh/zshrc.d/60-keys.zsh index 44116f6..f329d67 100644 --- a/.config/zsh/zshrc.d/60-keys.zsh +++ b/.config/zsh/zshrc.d/60-keys.zsh @@ -126,27 +126,6 @@ function cd-up { } zle -N cd-up -# Modified version of -# https://github.com/junegunn/fzf/blame/f864f8b5f7ab/shell/key-bindings.zsh#L81-L99 -# that changes the directory "in-prompt" like cd-{forward,backward,up}; meaning -# that the `cd` command is executed in the background with a prompt redraw -# instead of via `$BUFFER` and `accept-line`. -fzf-cd-inprompt-widget() { - setopt localoptions pipefail no_aliases 2>/dev/null - local dir="$( - FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \ - FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \ - FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) < /dev/tty)" - if [[ -z "$dir" ]]; then - zle redisplay - return 0 - fi - # --- modifications start here --- - pushd -q "$dir" - redraw-prompt -} -zle -N fzf-cd-inprompt-widget - # cycle through `dirs` with ^o and ^i similar to the jumplist in vim. # Need AUTO_PUSHD (see options.zsh) bindkey '^O' cd-backward @@ -269,15 +248,15 @@ bindkey '^[[1;5A' fzf-history-widget bindkey -M vicmd '^K' fzf-history-widget # Fuzzy finder bindings: -# ^T fzf-file-widget -# \ec (Alt-C) fzf-cd-widget -# ^R fzf-history-widget -# TODO: ^R should insert the history line in BUFFER to differ from ctrl-up +# +# - ^T fzf-file-widget +# - \ec (Alt-C) fzf-cd-widget +# - ^R fzf-history-widget +# +# are sourced via 90-external-plugins/50-fzf.key-bindings.zsh, but see also +# 55-fzf.key-bindings.config.zsh + +# Discard stderr (i.e. permission denied) FZF_CTRL_T_COMMAND="${FZF_DEFAULT_COMMAND} 2>/dev/null" -# See .zprofile for FZF_ALT_C_COMMAND -comp-source "$ZDOTDIR/plugins/fzf/shell/key-bindings.zsh" - -# Overwrite fzf's Alt-C binding to be "in-prompt" (see above) -bindkey -M vicmd '\ec' fzf-cd-inprompt-widget -bindkey -M viins '\ec' fzf-cd-inprompt-widget +# TODO: ^R should insert the history line in BUFFER to differ from ctrl-up diff --git a/.config/zsh/zshrc.d/90-external-plugins/55-fzf.key-bindings.config.zsh b/.config/zsh/zshrc.d/90-external-plugins/55-fzf.key-bindings.config.zsh new file mode 100644 index 0000000..e88c942 --- /dev/null +++ b/.config/zsh/zshrc.d/90-external-plugins/55-fzf.key-bindings.config.zsh @@ -0,0 +1,25 @@ +# Overwrite fzf's Alt-C binding to be "in-prompt" + +# Modified version of +# https://github.com/junegunn/fzf/blame/f864f8b5f7ab/shell/key-bindings.zsh#L81-L99 +# that changes the directory "in-prompt", meaning that the `cd` command is +# executed in the background with a prompt redraw instead of via `$BUFFER` and +# `accept-line`. This behavior is inspired by cd-{forward,backward} written by +# romkatv (see keys.zsh). +fzf-cd-inprompt-widget() { + setopt localoptions pipefail no_aliases 2>/dev/null + local dir="$( + FZF_DEFAULT_COMMAND=${FZF_ALT_C_COMMAND:-} \ + FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --walker=dir,follow,hidden --scheme=path" "${FZF_ALT_C_OPTS-} +m") \ + FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd) < /dev/tty)" + if [[ -z "$dir" ]]; then + zle redisplay + return 0 + fi + # --- modifications start here --- + pushd -q "$dir" + redraw-prompt +} +zle -N fzf-cd-inprompt-widget +bindkey -M vicmd '\ec' fzf-cd-inprompt-widget +bindkey -M viins '\ec' fzf-cd-inprompt-widget