terminal: Add fzf file insertion and zoxide cd key binds

The following is added to all three shells bash, zsh, nushell:

Pressing c-t at any time lets you insert a file/dir at the current
cursor location using fzf. Same for 'T' in vicmd mode.

Pressing alt-c at any time lets you jump to that directory using zoxide,
with the zle editor content intact. Same for 'C' in vicmd mode.

Zsh implementation from: https://github.com/ajeetdsouza/zoxide/issues/357
Bash implementation: https://github.com/ajeetdsouza/zoxide/issues/516

Nushell implementation taken from: https://github.com/junegunn/fzf/issues/4122

TODO: Nushell fzf mapping has one problem in that it does not quote the
selected file in any way. So any file with e.g. a space in it will have
to be manually fixed afterwards.
This commit is contained in:
Marty Oehme 2025-10-12 13:23:42 +02:00
parent 40e8287213
commit fa7e740249
Signed by: Marty
GPG key ID: 4E535BC19C61886E
3 changed files with 101 additions and 14 deletions

View file

@ -49,6 +49,9 @@ unset PLUG_FOLDER
# simple fzf-tab settings
zstyle ":fzf-tab:*" fzf-flags "--ansi" "--expect='$continuous_trigger,$print_query'" "--color=hl:$(($#headers == 0 ? 108 : 255))" "--nth=2,3" "--layout=reverse" "--height=${FZF_TMUX_HEIGHT:-75%}" "--tiebreak=begin" "-m" "--bind=tab:down,btab:up,change:top,ctrl-space:toggle" "--cycle" "--query=$query" "--header-lines=$#headers" "--print-query"
# enable inserting files or dirs using fzf with ctrl-t
FZF_CTRL_R_COMMAND="" FZF_ALT_C_COMMAND="" source <(fzf --zsh)
zstyle ':fzf-tab:*' fzf-command fzf
# format colorful groups for different completion actions
zstyle ':completion:*:descriptions' format '[%d]'
@ -208,6 +211,29 @@ _fix_cursor() {
}
precmd_functions+=(_fix_cursor)
_run-cdi() {
local dir="$(eval "zoxide query -i")"
if [[ -z "$dir" ]]; then
zle redisplay
return 0
fi
zle push-line
BUFFER="builtin cd -- ${(q)dir}"
zle accept-line
local ret=$?
unset dir
zle reset-prompt
return $ret
}
zle -N _run-cdi
# use alt-c or C in cmd mode to change dir using zoxide
bindkey '\ec' _run-cdi
bindkey -M vicmd 'C' _run-cdi
# add file insertion with fzf to T in cmd mode (c-t in insert)
# insert-mode keybind is above, using regular fzf sourcing
bindkey -M vicmd 'T' fzf-file-widget
# space puts a space, even in cmd mode
bindkey -a ' ' magic-space
# always allow backspace/delete to remove letters