zsh keybind giving error it isn't a widget

47 Views Asked by At

I am trying to create a keybind that maps to a function. whenever I call the function from the terminal it works perfectly but whenever I use my keybind it doesn't and I get this error: No such widget `fzf-cd'. this is my code

function fzf-cd() {
  local dir=$(find . -type d -print | fzf)
  if [[ -n "$dir" ]]; then
    cd "$dir"
    echo "Switched to: $dir"
  else
    echo "No directory chosen."
  fi
}

bindkey "\ef" fzf-cd

I've trying to change the keybind to something else but that doesn't really help at all.

2

There are 2 best solutions below

0
user1934428 On

Never tried it myself, but I'm pretty certain that you need to register your function as widget before you can use it in zle:

zle -N fzf-cd

However, the content of your function fcf-cd function looks a bit strange to me for the use as a zle widget. The purpose of widgets is to act as kind of "programmable macros", and I would expect your function to at least simulate other key presses.

0
Chris N On

From zshzle(1):

ZLE WIDGETS

All actions in the editor are performed by `widgets'. ... The ZLE commands that key sequences in keymaps are bound to are in fact widgets. Widgets can be user-defined or built in. ...

User-defined widgets are defined using zle -N, and implemented as shell functions. When the widget is executed, the corresponding shell function is executed, and can perform editing (or other) actions.

In other words, the “command” for bindkey must be a widget, not a function, and you turn a function into a widget using zle -N, in your case zle -N fzf-cd.