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?
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?
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.
This is working for me on zsh 5.8.1 on xterm terminal. I have the following defined inside my zshrc to quickly create templates for scripting languages/tests. For instance, a template (of a template) for a shell script:
# Function and keybind defined in .zshrc:
create-template (){
cat << EOF > template.sh
#!/usr/bin/bash
# Your template goes here ...
EOF
echo "template.sh created"
}
bindkey -s "^E" 'create-template^M'