如果在“bash”shell 中找不到命令,如何启用建议(“您的意思是......”功能)?

如果在“bash”shell 中找不到命令,如何启用建议(“您的意思是......”功能)?

类似这个问题: 我如何删除 shell 中的“您的意思是......”功能?

当你输入错误的命令时,你会得到类似的信息:

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:~# 

是否有适用于 linux 或 ubuntu 的 shell 插件可以推荐第一个猜测,而我只需按回车键即可?

所以它应该是这样的:

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]?

所以我只需要按下回车键,就可以执行 sudo 命令而不是 sido。

答案1

如果 zsh 不是一个选项,请查看他妈的

它可以与以下设备一起安装:

sudo pip install thefuck

自述文件中的一个示例几乎完全符合您的要求:

➜ 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)
...

它还可以做一些其他方便的事情,例如:

➜ 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
...

或者

➜ 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

如果你愿意切换 shell,zsh则无需插件即可提供:

$ setopt correct 
$ sido -i
zsh: correct 'sido' to 'sudo' [nyae]? 
y
[sudo] password for muru: 

来自文档

CORRECT打开命令的拼写校正,该 CORRECTALL选项打开所有参数的拼写校正。

% 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

y如果当 shell 询问您是否要更正某个单词时按下,该单词将被更正。如果按下n,该单词将保持不变。按下a可中止该命令,按下e可再次显示该行以供编辑,以防您同意该单词拼写错误但不喜欢更正结果。

相关内容