27

Can the zsh shell be configured to show what bash shows when a command does not exist, similarly to this:

kahless:~$ sysv-rc-conf  
The program 'sysv-rc-conf' is currently not installed.  You can install it by typing:  
sudo apt-get install sysv-rc-conf

rather than the ZSH prompt:

[kahless /home/teward]% sysv-rc-conf  
zsh: command not found: sysv-rc-conf

Note I do not want to change the prompt itself, but I want to change the result from zsh: command not found to a bash-like output of The program 'progname' is currently not installed. You can install it by typing: or similar.

Is this possible with ZSH?

Braiam
  • 69,112
Thomas Ward
  • 78,878

5 Answers5

27

This feature is provided by the command-not-found Install command-not-found package. Ubuntu installs it by default, and makes it active by default in bash but not in zsh. Just add this line to your ~/.zshrc:

. /etc/zsh_command_not_found
Seth
  • 59,332
4

Note that you might want to add a check if the file exists if you are sharing your .zshrc across distributions that do not have a /etc/zsh_command_not_found file:

[[ -a "/etc/zsh_command_not_found" ]] && . /etc/zsh_command_not_found

Also, in case you're using oh-my-zsh, there already is a plugin, command-not-found, that you can add to your plugins variable that does the same thing.

zgerd
  • 141
3

If you are using oh-my-zsh, you can just instead look for "plugins" inside your .zshrc.

Add the command-not-found plugin to the list of plugins to autoload (this plugin is already installed by default).

Like this:

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git command-not-found)
Pablo Bianchi
  • 17,371
qaisjp
  • 131
0

The way it works is through the command_not_found_handle() function in bash. bash provides a hook which is basically a function that is invoked when a command is not found. Ubuntu's bash implementation traps it and does a package search, while Debian's bash does not (and same with zsh). You may want to check out zsh's manpage to see if that has a similar function.

Here is how you can get started, hoping zsh is similar.

mpandit@mpandit-ubuntu:~$ command_not_found_handle() {echo 'Handler: Command not found!';}
mpandit@mpandit-ubuntu:~$ ddaadada
'Handler: Command not found!
mpandit@mpandit-ubuntu:~$
0

Also if you don't want zsh to have this behavior 100% of the time you can just manually do something like:

/usr/lib/command-not-found urxvt

or add command-not-found to your $PATH