在其他Shell(如Zsh)中启用“Command Not Found”消息

我发现了如何让在 Oh-my-zsh 中出现 "... 命令未找到但可以通过安装...来解决" 的方法,只需在我的 .zshrc 文件中添加 source /etc/zsh_command_not_found

但是现在,当我输入一个不存在的命令但没有 "...可以通过安装..." 的消息时,就没有输出,就好像我输入了一个没有输出的命令一样。

原始(期望)行为:

~$ Non_existant_command
Non_existant_command: command not found
~$

新(不需要的)行为:

~$ Non_existant_command
~$


总体而言,我希望在zsh(或者我正在使用的任何shell)中出现bash中的“命令未找到,但可以安装”的消息。
1个回答

这似乎是因为/etc/zsh_command_not_found使用/usr/lib/command-not-found并带有--no-failure-msg选项:
 % cat /etc/zsh_command_not_found
# (c) Zygmunt Krynicki 2007,
# Licensed under GPL, see COPYING for the whole text
#
# This script will look-up command in the database and suggest
# installation of packages available from the repository

if [[ -x /usr/lib/command-not-found ]] ; then
        if (( ! ${+functions[command_not_found_handler]} )) ; then
                function command_not_found_handler {
                        [[ -x /usr/lib/command-not-found ]] || return 1
                        /usr/lib/command-not-found --no-failure-msg -- ${1+"$1"} && :
                }
        fi
fi

哪里
 % /usr/lib/command-not-found --help
Usage: command-not-found [options] <command-name>

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -d DATA_DIR, --data-dir=DATA_DIR
                        use this path to locate data fields
  --ignore-installed, --ignore-installed
                        ignore local binaries and display the available
                        packages
  --no-failure-msg      don't print '<command-name>: command not found'

如果你想显示这条消息,你可以将 zsh_command_not_found 脚本复制到本地,编辑它以删除该选项,并代替源代码使用 那个

谢谢你的回答。起初它没有起作用,但我很快就让它正常工作了。 - AlexFullinator