In the default configuration of bash in ubuntu, when you type the
name of a software which is not installed, bash explains how to install it if
the executable if it exists, or how to install an executable with a really close name if it does not exist.
For example with emacs (which is not installed on my machine):
$ emacx No command 'emacx' found, did you mean: Command 'emacs' from package 'emacs23-lucid' (universe) Command 'emacs' from package 'e3' (universe) Command 'emacs' from package 'emacs23-nox' (main) Command 'emacs' from package 'emacs24' (main) Command 'emacs' from package 'emacs24-nox' (main) Command 'emacs' from package 'emacs23' (main) Command 'emacs' from package 'jove' (universe) Command 'emacs' from package 'emacs24-lucid' (universe) emacx: command not found $ emacs The program 'emacs' can be found in the following packages: * emacs23 * emacs23-nox * emacs24 * emacs24-nox * e3 * emacs23-lucid * emacs24-lucid * jove Try: sudo apt-get install <selected package>
With my current configuration of zsh I obtain:
$ emacx zsh: command not found: emacx $ emacs zsh: command not found: emacs
There is also a difference of behavior when making a mistake in the name of an installed software.
Say I want to launch gedit instead of kedit. With bash, I obtain:
$ kedit No command 'kedit' found, did you mean: Command 'xedit' from package 'x11-apps' (main) Command 'edit' from package 'mime-support' (main) Command 'nedit' from package 'nedit' (universe) Command 'gedit' from package 'gedit' (main) Command 'jedit' from package 'jedit' (universe) Command 'medit' from package 'medit' (universe) Command 'ledit' from package 'ledit' (main) kedit: command not found
While with zsh I obtain:
$ kedit zsh: correct 'kedit' to 'edit' [nyae]?
So, my questions are:
- Is it possible to have a similar behavior in zsh as with bash when trying to launch an executable not yet installed?
If yes, how? - Is it possible to display all the possibilities when there is a typing mistake, instead of giving
one correction which can be wrong?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The default zsh configuration for Debian (and probably Ubuntu) just doesn’t include support for the command-not-found package per default.
In order to have the same functionality you just have to source /etc/zsh_command_not_found in your ~/.zshrc per default via:
[ -f /etc/zsh_command_not_found ] && . /etc/zsh_command_not_found
This should load and enable the command-not-found functionality in zsh.
Method 2
I wanted to note that
[[ -e /etc/zsh_command_not_found ]] && source /etc/zsh_command_not_found
Is almost the correct answer. However, There is a little problem.
For some commands it returns absolutely no result. For Example try the following commands in bash:
$ muxy muxy: command not found $ mury mury: command not found
But for zsh you will get NOTHING. No error message.
$ muxy $ murez
Not to worry, the solution is in the /etc/zsh_command_not_found file.
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
Here the problem is with the --no-failure-msg. Remove it, and the problem will be solved.
What I do is, instead of using [[ -e /etc/zsh_command_not_found ]] && source /etc/zsh_command_not_found, I put the following lines in my .zshrc file.
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 -- ${1+"$1"} && :
}
fi
fi
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0