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

Re: Handling Command Not Found



On Fri, 27 Nov 2009 00:12:04 -0600
Adam Walters <ajwalters@xxxxxxxxx> wrote:
> I was wondering if zsh had a way to handle command not found by running a
> function?  Ideally, I would like to pipe the input of a command not into a
> ruby script.  Is there any way to accomplish this in zsh?

You can define a command_not_found_handler function in recent versions
of the shell (4.3.something-or-other).  See the zshmisc manual page
under COMMAND EXECUTION.

For example,

command_not_found_handler() {
  if [[ $1 = foo ]]; then
    print "I handled foo"
    return 0
  else
    return 127
   fi
}

% foo
I handled foo
% print $?
0
% zsh: command not found: not_foo
% print $?
127

-- 
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