From 7b47c0ce2110c6af925a0e11b6a1f095f3f37788 Mon Sep 17 00:00:00 2001 From: Mohammad Reza Date: Thu, 24 Mar 2022 16:26:00 +0430 Subject: [PATCH] added command_not_found_handler --- .config/zsh/.zshrc | 3 +++ .config/zsh/command-not-found.zsh | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .config/zsh/command-not-found.zsh diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index 22a166e7..eb69e185 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -75,3 +75,6 @@ bindkey '^e' edit-command-line # Load syntax highlighting; should be last. source /usr/share/zsh/plugins/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh 2>/dev/null + +# Load command_not_found_handler. +source "${XDG_CONFIG_HOME:-$HOME/.config}/zsh/command-not-found.zsh" 2>/dev/null diff --git a/.config/zsh/command-not-found.zsh b/.config/zsh/command-not-found.zsh new file mode 100644 index 00000000..b085358a --- /dev/null +++ b/.config/zsh/command-not-found.zsh @@ -0,0 +1,21 @@ +# This script was borrowed and then customized from the ArchWiki: +# https://wiki.archlinux.org/title/Zsh#pacman_-F_"command_not_found"_handler + +function command_not_found_handler { + local purple='\e[1;35m' bright='\e[0;1m' green='\e[1;32m' reset='\e[0m' + printf 'zsh: command not found: %s\n' "$1" + local entries=( + ${(f)"$(/usr/bin/pacman -F --machinereadable -- "/usr/bin/$1")"} + ) + if (( ${#entries[@]} )); then + printf "${bright}%s${reset} may be found in the following packages:\n" "$1" + local pkg + for entry in "${entries[@]}"; do + local fields=(${(0)entry}) + if [[ "$pkg" != "${fields[2]}" ]]; then + printf " ${purple}%s/${bright}%s ${green}%s${reset}\n" "${fields[1]}" "${fields[2]}" "${fields[3]}" + fi + done + fi + return 127 +}