Skip to content

Instantly share code, notes, and snippets.

@XenHat
Last active December 26, 2019 00:23
Show Gist options
  • Save XenHat/01f51444a1ba6bd75f7b077f6e4cf6ff to your computer and use it in GitHub Desktop.
Save XenHat/01f51444a1ba6bd75f7b077f6e4cf6ff to your computer and use it in GitHub Desktop.
Ubuntu and Gentoo cross-compatible implementation of Command Not Found snippet
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ] || [ -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle() {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
elif [ -x /usr/bin/e-file ]; then # e-file is provided by app-portage/pfl and does exactly what we need.
function command_not_found_handle() {
# Command may have disappeared since the definition of this function
if [ -x /usr/bin/e-file ]; then
echo "Trying to find package providing this command..."
result=$(/usr/bin/e-file "$1")
if [ -z "$result" ]; then
echo "$0: $1: command not found"
else
echo "$result"
fi
#/usr/bin/qfile "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
#echo "$1: command not found"
return 127
fi
}
else
echo "To try to find the package providing this command, please install app-portage/pfl (Gentoo) or command-not-found (others)"
fi
# if the command-not-found package is installed, use it
if [[ -x /usr/lib/command-not-found ]] || [[ -x /usr/share/command-not-found/command-not-found ]]; then
function command_not_found_handler() {
# check because c-n-f could've been removed in the meantime
if [[ -x /usr/lib/command-not-found ]]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [[ -x /usr/share/command-not-found/command-not-found ]]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
elif [ -x /usr/bin/e-file ]; then
function command_not_found_handler() {
# Command may have disappeared since the definition of this function
if [[ -x /usr/bin/e-file ]]; then
echo "Trying to find package providing this command..."
result=$(e-file $1 2>/dev/null)
#echo "result: $result"
if [[ "$result" == "No matches found." ]]; then
echo "zsh $1: command not found"
else
echo "$result"
fi
return $?
else
echo "please re-emerge app-portage/pfl to restore this functionality"
return 127
fi
}
else
echo "To try to find the package providing this command, please install app-portage/pfl (Gentoo) or command-not-found (others)"
fi
@XenHat
Copy link
Author

XenHat commented Dec 26, 2019

Add to rc file, or source it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment