Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers 4

up vote 10 down vote accepted

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
share|improve this answer
    
This solution worked. Is there a way to get rid of a line in the output, then? (note that it was included in the output, as was the The program 'progname' is currently not installed. You can install it by typing: part) zsh: command not found: sysv-rc-conf –  Thomas W. Apr 14 '11 at 23:01
    
@EvilPhoenix: Anywhere would do (except inside a function or something obviously). I don't know about removing the zsh: command not found line. –  Gilles Apr 14 '11 at 23:05
    
Okay, i can probably ignore the "command not found part". Because the suggest installation part is nice, and its nice to knokw that it came up because a command didnt exist ;) –  Thomas W. Apr 14 '11 at 23:56
    
to turn of the command not found message you can add exit at the end of the function command_not_found_handler in /etc/zsh_command_not_found. –  jenson_bo Jan 22 '13 at 8:12
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
2  
when i asked this, i wanted it to work all the time :P –  Thomas W. Jan 10 '12 at 14:41
add comment

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:~$
share|improve this answer
    
This isn't a difference in Debian and Ubuntu's bash. The difference is that Ubuntu installs command-not-found by default and Debian doesn't. The feature already exists for zsh as well, it's just not activated by default. –  Gilles Apr 14 '11 at 22:49
    
This assumes that command_not_found is not included in the system. However it is not what I'm looking for. Having said that, the answer from @Gilles is more along the lines of what i'm looking for, i'm just trying to figure out where to include the line he specified. –  Thomas W. Apr 14 '11 at 22:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.