How to enable suggestion if command not found ("did you mean..." feature) in "bash" shell?
Similar to this question: How do I remove the "did you mean..." feature in the shell?
When mistyping a command you get something like:
root@pc:~# sido
No command 'sido' found, did you mean:
Command 'sudo' from package 'sudo' (main)
Command 'sudo' from package 'sudo-ldap' (universe)
sido: command not found
root@pc:~#
Is there a shell plugin for linux or ubuntu which recommends the first guess and I just have to press enter?
So it should be like:
root@pc:~# sido
No command 'sido' found, did you mean:
Command 'sudo' from package 'sudo' (main)
Command 'sudo' from package 'sudo-ldap' (universe)
sido: command not found
Did you mean 'sudo' [Y/n]?
So I just need to press enter and the sudo command is executed instead of sido.
If zsh isn't an option, take a look at thefuck
It can be installed with:
sudo pip install thefuck
One of the examples in the readme is almost exactly what you're requesting:
➜ puthon
No command 'puthon' found, did you mean:
Command 'python' from package 'python-minimal' (main)
Command 'python' from package 'python3' (main)
zsh: command not found: puthon
➜ fuck
python
Python 3.4.2 (default, Oct 8 2014, 13:08:17)
...
It also does some other handy stuff like:
➜ apt-get install vim
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
➜ fuck
sudo apt-get install vim
[sudo] password for nvbn:
Reading package lists... Done
...
or
➜ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
➜ fuck
git push --set-upstream origin master
Counting objects: 9, done.
...
If you're willing to switch shells, zsh
offers this without plugins:
$ setopt correct
$ sido -i
zsh: correct 'sido' to 'sudo' [nyae]?
y
[sudo] password for muru:
From the docs:
CORRECT
turns on spelling correction for commands, and theCORRECTALL
option turns on spelling correction for all arguments.% setopt correct % sl zsh: correct `sl' to `ls' [nyae]? y % setopt correctall % ls x.v11r4 zsh: correct `x.v11r4' to `X.V11R4' [nyae]? n /usr/princton/src/x.v11r4 not found % ls /etc/paswd zsh: correct to `/etc/paswd' to `/etc/passwd' [nyae]? y /etc/passwd
If you press
y
when the shell asks you if you want to correct a word, it will be corrected. If you pressn
, it will be left alone. Pressinga
aborts the command, and pressinge
brings the line up for editing again, in case you agree the word is spelled wrong but you don't like the correction.