Zsh Package Suggestion
Asked Answered
B

2

26

I've just migrated to Zsh from Bash, but I have a bit of a problem in it. In bash on an Ubuntu system, when I type the name of a command which does not exist, Bash searches the apt database for a matching command name and prints out the package names that provide that command. It's a really useful feature, so I was wondering if something like that could be implemented in Zsh using a script or something?

Here's an example:

$>xmms2
The program 'xmms2' is currently not installed.  You can install it by typing:
sudo apt-get install xmms2-client-cli

Or if the command is not an exact match:

$>xmms
No command 'xmms' found, did you mean:
Command 'lmms' from package 'lmms' (universe)
Command 'xmms2' from package 'xmms2-client-cli' (universe)
Command 'xmds' from package 'xmds' (universe)
Command 'xdms' from package 'xdms' (universe)
Bream answered 23/7, 2010 at 8:36 Comment(6)
Belongs on superuser.comMima
ahh ok sorry about that, I'm new here :)Bream
@Paul R if this package does not exists than is not this a programming question? @gastly if you find the source of the script that does this trick, I can port it to zsh.Exocentric
@Exocentric I found the solution, the functionality is in the package called "command-not-found" in Ubuntu. I just needed to install the package and source in the file "/etc/zsh_command_not_found" into my zshrc :)Bream
@gastly, could you write your solution as an answer and accept it, so that the question is known to have a correct answer? Currently it still appears on various lists of unanswered questions.Tokharian
Well framed question!Xena
B
25

As suggested by Michał Politowski, I'll answer my own question and mark it as solved :)

To get this functionality in zsh install a package named "command-not-found" (I don't know about other distro's, but in Ubuntu it's in the repositories).

NOTE: In Ubuntu 12.04 this package is installed by default.

sudo apt-get install command-not-found

And then you will need to edit your .zshrc and then source in the file /etc/zsh_command_not_found by adding the following line in your .zshrc

source /etc/zsh_command_not_found

Bream answered 26/3, 2012 at 15:55 Comment(1)
Just to be clear. I already had command-not-found installed and all I needed add was the following to.zshrc source /etc/zsh_command_not_foundOnestep
B
0

On MacOs, this is the brew formula ;)

brew tap homebrew/command-not-found

And add at the bottom of ~/.zshrc

HB_CNF_HANDLER="$(brew --repository)/Library/Taps/homebrew/homebrew-command-not-found/handler.sh"
if [ -f "$HB_CNF_HANDLER" ]; then
source "$HB_CNF_HANDLER";
fi

Complete reference: https://github.com/Homebrew/homebrew-command-not-found

Bodycheck answered 17/9, 2021 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.