Zsh – How to Intercept ‘Command Not Found’ Error in Zsh

shelltrap:zsh

Is there a way to intercept the "command not found" error in ZSH? I've seen this is possible in bash apparently, but I couldn't find anything about doing it in zsh.

Best Answer

There is; it's the same as in bash, you make a function named command_not_found_handler. It'll be passed all the arguments that were given in the shell

$ foobar baz
zsh: command not found: foobar
$ function command_not_found_handler() {echo "You tried to run $1 with arg $2";}
$ foobar baz
You tried to run foobar with arg baz
Related Question