Binding key shortcuts to shell functions in zsh

keyboard shortcutszsh

In zsh, how do I bind a keyboard shortcut to a function?

In other words, how do I translate:

bash:

hw(){ echo "hello world"; }
bind -x '"\C-h": hw;'

to zsh?

Best Answer

It won't take the functions raw. They need to be wrapped in a "widget" by doing

zle -N widgetname funcname

The two can have the same name:

 zle -N hw{,}

Then it's possible to do:

bindkey ^h hw

, causing Ctrl+h to run the hw widget which runs the hw function.

Related Question