Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: ZSH's way to bind function to a key in a script?



On Wed, 9 Mar 2011 15:46:44 +0200
<nix@xxxxxxxxxxxxxxxx> wrote:
> Hi everyone. I was reading last night about "bindkey" etc. , and i
> could not get it working.
> 
> #!/bin/zsh
> 
> function foo () {
> 
> echo "Foo ooo"
> }
> 
> In this script, how to bind function "foo" to a key. So lets say i
> press the Space button and it should trigger foo?

The standard examples of bindkey assume you're binding editor commands
rather than normal commands, i.e. something that helps you edit a
command line.

To bind a command to a keystroke, you need the "-s" option to bindkey.
However, note it's doing something rather different --- basically just
dumping the keys you type into the command line.  One idiom for running
"foo" this way is to use ^q to ensure there's nothing else in the buffer
(if there is, you don't lose it, you get it back after running foo), and
put a newline at the end.  Luckily you can use the same abbreviations
for keys as in the keystroke part of the command:

bindkey -s '<keystroke>' '^qfoo\n'

This assumes ^q is bound to the editor command push-line.  You can
remove the ^q if you only want to use this on an empty command line.

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



Messages sorted by: Reverse Date, Date, Thread, Author