the fuck 是一款功能强大的、python编写的应用程序,可用于纠正控制台命令中的错误,非常强大。此外,用户还可通过写python代码的方式自定义修复规则。
更多示例如:
自动识别没有权限,在命令前面添加 sudo:
➜ apt-get install vime: 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?➜ fucksudo apt-get install vim [enter/↑/↓/ctrl+c][sudo] password for nvbn:reading package lists... done...识别到没有推送到远程分支,自动追加:
➜ git pushfatal: 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➜ fuckgit push --set-upstream origin master [enter/↑/↓/ctrl+c]counting objects: 9, done....识别到拼写错误:
➜ puthonno command 'puthon' found, did you mean: command 'python' from package 'python-minimal' (main) command 'python' from package 'python3' (main)zsh: command not found: puthon➜ fuckpython [enter/↑/↓/ctrl+c]python 3.4.2 (default, oct 8 2014, 13:08:17)...如果你不担心fuck修正的结果是错误的,你可以禁用require_confirmation 选项,让fuck自动运行更正的命令:
➜ apt-get install vime: 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?➜ fucksudo apt-get install vim[sudo] password for nvbn:reading package lists... done...在开发机上可以这么做,在生产机器上最好是谨慎一点,不推荐这么做。
1.安装在os x上,可以通过homebrew(或在linux上通过linuxbrew)安装 the fuck :
brew install thefuck在ubuntu / mint上,使用以下命令安装 the fuck :
sudo apt updatesudo apt install python3-dev python3-pip python3-setuptoolssudo pip3 install thefuck在freebsd上,使用以下命令安装 the fuck :
pkg install thefuck在其他系统上, 使用pip安装 the fuck :
pip install thefuck2.配置接下来需要把这个命令写入到启动脚本中,根据你的终端类型,运行相应的命令即可:
bash
chcp.com 65001 eval $(thefuck --alias)其中 chcp.com 65001 只有在windows环境下才需要运行。
zsh:
eval $(thefuck --alias)其他的可见:
https://github.com/nvbn/thefuck/wiki/shell-aliases
3.原理其实thefuck的原理就是规则匹配(正则表达式),如果找到匹配规则的命令,则创建一个命令给用户选择或直接运行。
默认情况下的规则有:
cat_dir - 当你尝试cat目录的时候,用ls替换cat;cd_correction – 拼写检查和纠正失败的cd命令;cd_mkdir – 在进入目录之前创建目录;cd_parent – 更改 cd.. 为cd ..;dry – 修复类似的重复问题:git git push;fix_alt_space – 用空格字符代替alt + space;等等,具体可以在官方文档中找到:
https://github.com/nvbn/thefuck
4. 创建自己的修复规则要添加自己的规则,在 ~/.config/thefuck/rules 文件夹中,
创建一个文件名为 your-rule-name.py 的规则文件,其中必须包含两个函数:
match(command: command) - > boolget_new_command(command: command) - > str | list[str]下面是简单的 sudo 规则示例:
def match(command): return ('permission denied' in command.output.lower() or 'eacces' in command.output)def get_new_command(command): return 'sudo {}'.format(command.script)# optional:enabled_by_default = truedef side_effect(command, fixed_command): subprocess.call('chmod 777 .', shell=true)priority = 1000 # lower first, default is 1000requires_output = true如果命令运行结果出现 permission denied 或者 eacces,则执行 sudo xxx.
此外,还可以配置side_effect,如果你配置了enabled_by_default = true,side_effect函数内的操作将会被执行,本例中是对当前目录下的文件夹执行赋权操作: chmod 777 .
大家可以动手试试自己配一个修复命令,还是相当有意思的。
盘点2018年PCB行业的11大并购案
iphone7或配置A10处理器 采用最新PoP工艺
at89c52引脚图及管脚功能
移动互联网给前端工程师带来了什么
ARM CEO:上网本将占PC市场90%份额
TheFuck:实用的命令自动纠正工具
智能制造日报:华为宣布获得首张5G CE证书,移动所携南京联通推5G产业技术创新服务基地
Linear推出双输入同步降压-升压型DC/DC转换器LTC3118
EVG与DELO合作为晶圆级光学元件和纳米压印光刻技术开发材料并提升工艺能力
费城半导体指数上涨65%,迎2009年以来最大年涨幅
MAX14921:12/16节电池高精度测量模拟前端的PCB布局指南
爆料天王展示初代iPad原型机 系统界面功能超乎你想像!
电力智能运维服务的内容介绍
功率放大器的三种类型是什么意思
从标准到操作,深入了解万能试验机对医用注射针刚性测试
卫星/RedCap /高算力/解决方案/创新金奖……移远通信为IOTE 2023再添新活力
豪威科技发布第二代近红外Nyxel 2技术,可实现70%的量子效率
智能手表泄压孔防水方案,平衡内腔与外界压力!
《凡亿4个月EMC线上实战特训营》开班倒计时第3天......
电压源换流器(VSC)并比较两种拓扑结构