Search code examples
macosshellzshcustomizationzshrc

How to customize command not found style in zsh


How to customize command not found in zsh (MacOS)

For example:

MacBook-Pro: ~ justin$ lll
zsh: command not found: lll

To

MacBook-Pro: ~ justin$ lll
zsh: WTFFFFF command not found: lll

There is a thread discuss about the case in bash, but i can't find anything like command-not-found in ~/zshrc, /private/etc/zshrc, and /private/etc/zprofile


Solution

  • zsh looking for the function command_not_found_handler in the zshrc file (instead of command_not_found_handle of bash).

    Add to your ~/.zshrc file something like:

    command_not_found_handler() {
        echo "zsh: WTFFFFF command not found: $@"
        return 127
    }
    

    or customize it further. Notice the return 127 line: the default command not found handler function returns the exit code 127 so this way you can keep this behavior. You can test the exit code with echo $?.