Messing with the zsh not-found handler

Last week, I saw this post on reddit, which features a nice chad ASCII art.

chadsay

I thought it was pretty cool, so of course I decided to try it out.

chadsay

Then I thought, why not make it a little more useful?

Zsh not-found handler

An idea came to me, I could make chad say something when the user typed the wrong command.

So after some goggling, it turns out that zsh already has support for executing custom code when the user types a wrong command.

By defining the following function in .zshrc, we can override the message when the user types a wrong command.

command_not_found_handler() {
  echo "Oops! I don't know what you mean by $1."
}

command-not-found

Now we have every piece of the puzzle. It is time to put it all together.

Chad roasting the user

I "borrowed" the chadsay line from the reddit post.

command_not_found_handler() {
    chadsay "'$1' not working?"$'\n'"Not my problem."
}

chad-roasts

More chaos

To improve the experience, we can create an array of sentences to say, and choose one randomly when the user types a wrong command.

command_not_found_handler() {
    quotes=(
        "'$1' not working? Not my problem."
        "'$1' is lost? Not my circus."
        "'$1' failed? Not my fault!"
        "'$1' gone? Oh well!"
    )
    random_index=$((RANDOM % ${#quotes[@]} + 1))
    random_quote=${quotes[random_index]}
    chadsay "$random_quote"
}
Jeffrey Cheung

Backend Software Engineer at Stransa. Obsessed to learn and build things. Occasionally, I write blogs about neovim related stuffs and articles I read lately.