开发者都或多或少接触过 linux 接触过命令行,当然肯定也都被命令行狠狠地“fuck”过。我很多时候都是微不足道的原因导致了命令行出错,例如将 python 输入成 ptyhon,例如将 ls -alh
输入成 ls a-lh
而导致出错,这个时候我会想说:“fuck”。
开发 thefuck 的这位同仁,恐怕也经常会有这种不和谐的情况。因此开发了这个软件 thefuck。
thefuck 不仅仅能修复字符输入顺序的错误,在很多别的你想说“fuck”的情况下,thefuck 依然有效。
例如以下情况。
任何情况下你想说“我操”,你都可以用得到 thefuck。
例如:
使用过 ubuntu 的开发者很多都知道有这么一条命令:sl。这条命令是在你将 ls 错误地输成 sl 时,会在屏幕上快速驶过一辆火车。当然,有了 thefuck,sl 似乎也可以退休了。
➜ sl
The program 'sl' is currently not installed. You can install it by typing:
sudo apt-get install sl
➜ fuck
ls #修正
a.go b.php dockerui jekyll-casper main.go PureBlog sss.png typecho
a.php c-hash Font-Awesome jekyll-pure mili signal.c TeamTalk upload
bii-ubuntu-64_3_0.deb Dockerfile harmony JekyllPure ngircd signal.c~ tingyun-agent-php-latest.x86_64.deb web.go
blog Dockerfile.bak hashmap kasper php-src sss.jpg tipi zerver
➜ 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.
...
➜ 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)
...
➜ git brnch
git: 'brnch' is not a git command. See 'git --help'.
Did you mean this?
branch
➜ fuck
git branch
* master
thefuck 还有其他很多种用法,就等着你去发现。
可以使用 pip 安装:sudo pip install thefuck
或者使用 OS X、Ubuntu、Arch 的包管理器安装。
安装后配置 bash:
alias fuck='eval $(thefuck $(fc -ln -1)); history -r
Zsh、Fish 和 PowerShell 的 配置方法见这里。
更新到最新的 thefuck 的方法非常简单:
sudo pip install thefuck --upgrade
前面说了那么多用法, 那么有人觉得 thefuck 可能只是一个用来 thefuck 进行命令修复的模块名和对应的修复原理如下:
brew
命令,例如将 brew docto
修复为 brew doctor
cd..
修正为 cd ..
cd
进入不存在的目录时尝试创建目录。cp
时添加 -a
选项git add
的情况。git checkout
不存在的分支之前尝试创建分支。git brnch
。push
失败,尝试将 git push
修正为 git push --set-upstream origin $branch
。./
前缀。lein
,例如 lein rpl
。mkdir
时尝试添加 -p
选项。vom
修正为 vim
。man
命令添加空格,例如 mandiff
修正为 man diff
pacman
或者 yaourt
安装。pip
命令,例如 pip isntall
修正为 pip install
x
权限的 python 程序时,修复为添加 python
前缀sl
修正为 ls
rm
添加 -rf
选项。know_hosts
中移除。sudo
。apt-get
包管理器安装。brew install
的格式。没有启用的模块:
ls
添加 -alh
选项。rm -rf /
添加 --no-preserve-root
选项。当然,你也可以自定义修正规则: 一个自定义规则的案例:
def match(command, settings):
return ('permission denied' in command.stderr.lower()
or 'EACCES' in command.stderr)
# 获取 stderr 中的字符串和程序返回值(errno),
# 修正规则(命令前添加 sudo)
def get_new_command(command, settings):
return 'sudo {}'.format(command.script)
# 以下为可选项
# 默认是否开启
enabled_by_default = True
# 附加命令
def side_effect(command, settings):
subprocess.call('chmod 777 .', shell=True)
# 优先级,数字越大优先级越低。
priority = 1000
虽然 thefuck 带来了很多便利,不过仍然建议注意这件事情:
thefuck 会给出的修正后的命令,但是默认情况下你看到它们的时候,命令都已经默认执行了。
大部分情况下,thefuck 可能给出的是正确的修正,但是小部分情况下,给出的修正并不是你想要的。
所以建议将 ~/.thefuck/settings.py
中的设置改为 require_confirmation = True
,这样可以自己决定是否执行修正后的命令。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有