The Fuck 是一款功能強大的、Python編寫的應(yīng)用程序,可用于糾正控制臺命令中的錯誤,非常強大。此外,用戶還可通過寫Python代碼的方式自定義修復(fù)規(guī)則。
修復(fù)效果如下動圖所示:
更多示例如:
自動識別沒有權(quán)限,在命令前面添加 sudo:
? 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 [enter/↑/↓/ctrl+c]
[sudo] password for nvbn:
Reading package lists... Done
...
識別到?jīng)]有推送到遠程分支,自動追加:
? 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 [enter/↑/↓/ctrl+c]
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 [enter/↑/↓/ctrl+c]
Python 3.4.2 (default, Oct 8 2014, 13:08:17)
...
如果你不擔心fuck修正的結(jié)果是錯誤的,你可以禁用require_confirmation
選項,讓fuck自動運行更正的命令:
? 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
...
在開發(fā)機上可以這么做,在生產(chǎn)機器上最好是謹慎一點,不推薦這么做。
1.安裝
在OS X上,可以通過Homebrew(或在Linux上通過Linuxbrew)安裝 The Fuck :
brew install thefuck
在Ubuntu / Mint上,使用以下命令安裝 The Fuck :
sudo apt update
sudo apt install python3-dev python3-pip python3-setuptools
sudo pip3 install thefuck
在FreeBSD上,使用以下命令安裝 The Fuck :
pkg install thefuck
在其他系統(tǒng)上, 使用pip安裝 The Fuck :
pip install thefuck
2.配置
接下來需要把這個命令寫入到啟動腳本中,根據(jù)你的終端類型,運行相應(yīng)的命令即可:
Bash
chcp.com 65001
eval "$(thefuck --alias)"
其中 chcp.com 65001 只有在windows環(huán)境下才需要運行。
Zsh:
eval "$(thefuck --alias)"
其他的可見:
https://github.com/nvbn/thefuck/wiki/Shell-aliases
3.原理
其實TheFuck的原理就是規(guī)則匹配(正則表達式),如果找到匹配規(guī)則的命令,則創(chuàng)建一個命令給用戶選擇或直接運行。
默認情況下的規(guī)則有:
cat_dir
- 當你嘗試cat
目錄的時候,用ls
替換cat
;cd_correction
– 拼寫檢查和糾正失敗的cd命令;cd_mkdir
– 在進入目錄之前創(chuàng)建目錄;cd_parent
– 更改cd..
為cd ..
;dry
– 修復(fù)類似的重復(fù)問題:git git push
;fix_alt_space
– 用空格字符代替Alt + Space;
等等,具體可以在官方文檔中找到:
https://github.com/nvbn/thefuck
4. 創(chuàng)建自己的修復(fù)規(guī)則
要添加自己的規(guī)則,在 ~/.config/thefuck/rules 文件夾中,
創(chuàng)建一個文件名為 your-rule-name.py 的規(guī)則文件,其中必須包含兩個函數(shù):
match(command: Command) - > bool
get_new_command(command: Command) - > str | list[str]
下面是簡單的 sudo 規(guī)則示例:
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 = True
def side_effect(command, fixed_command):
subprocess.call('chmod 777 .', shell=True)
priority = 1000 # Lower first, default is 1000
requires_output = True
如果命令運行結(jié)果出現(xiàn) permission denied 或者 EACCES,則執(zhí)行 sudo xxx.
此外,還可以配置side_effect,如果你配置了enabled_by_default = True,side_effect函數(shù)內(nèi)的操作將會被執(zhí)行,本例中是對當前目錄下的文件夾執(zhí)行賦權(quán)操作: chmod 777 .
-
Linux
+關(guān)注
關(guān)注
87文章
11414瀏覽量
212260 -
代碼
+關(guān)注
關(guān)注
30文章
4876瀏覽量
69962 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3312瀏覽量
58511 -
python
+關(guān)注
關(guān)注
56文章
4822瀏覽量
85804
發(fā)布評論請先 登錄
相關(guān)推薦
Python命令補全工具argcomplete簡介
cmd常用命令大全 cmd運行命令
Ubuntu常用命令大全
Memcache系統(tǒng)常用命令講解

評論