16

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.

1
  • 4
    is the zsh shell an option for you?
    – A.B.
    May 6, 2015 at 13:46

2 Answers 2

22

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.
...
2
  • This is Awesome!
    – muru
    May 6, 2015 at 17:59
  • Doesn't help, as I usually use sl to punish my-self in order to cure my bad habit of mistyping
    – kenorb
    May 7, 2015 at 9:52
14

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 the CORRECTALL 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 press n, it will be left alone. Pressing a aborts the command, and pressing e brings the line up for editing again, in case you agree the word is spelled wrong but you don't like the correction.

4
  • Nice, this looks good! Will look at it. Is there another solution using the standard /bin/bash? May 6, 2015 at 14:30
  • 1
    @SailAvid not that I know of. Bash only offers correction for paths (for use with cd: cdspell, or directories in general: dirspell).
    – muru
    May 6, 2015 at 14:56
  • sudo apt-get install sl/brew install sl
    – kenorb
    May 7, 2015 at 9:58
  • @kenorb hell no! I find sl more annoying than useful.
    – muru
    May 7, 2015 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .