Shell 命令行擴展:The Fuck
開發者都或多或少接觸過 linux 接觸過命令行,當然肯定也都被命令行狠狠地“fuck”過。我很多時候都是微不足道的原因導致了命令行出錯,例如將 python 輸入成 ptyhon,例如將ls -alh輸入成ls a-lh而導致出錯,這個時候我會想說:“fuck”。
起源
開發 thefuck 的這位同仁,恐怕也經常會有這種不和諧的情況。因此開發了這個軟件 thefuck。
thefuck 不僅僅能修復字符輸入順序的錯誤,在很多別的你想說“fuck”的情況下,thefuck 依然有效。
例如以下情況。
案例
任何情況下你想說“我操”,你都可以用得到 thefuck。
例如:
ls 和 sl
使用過 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.goblog Dockerfile.bak hashmap kasper php-src sss.jpg tipi zerver
忘記 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 [sudo] password for nvbn: Reading package lists... Done
操作 git
? 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 ? 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 brnchgit: 'brnch' is not a git command. See 'git --help'. Did you mean this? branch ? fuck git branch * master
其他
thefuck 還有其他很多種用法,就等著你去發現。
安裝
安裝需求
-
python (2.7+ or 3.3+)
-
pip
-
python-dev
安裝方法
-
可以使用 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_unknown_command:修復brew命令,例如將brew docto修復為brew doctor
-
cd_parent:將cd..修正為cd ..
-
cd_mkdir:cd進入不存在的目錄時嘗試創建目錄。
-
cp_omitting_directory:cp時添加-a選項
-
fix_alt_space:將所有的的Alt+Space修正為Space。
-
git_add:修正沒有git add的情況。
-
git_checkout:git checkout不存在的分支之前嘗試創建分支。
-
git_no_command:修正錯誤的選項,例如git brnch。
-
git_push:如果push失敗,嘗試將git push修正為git push --set-upstream origin $branch。
-
has_exists_script:為所有的提示不存在的命令嘗試添加./前綴。
-
lein_not_task:修正lein,例如lein rpl。
-
mkdir_p:mkdir時嘗試添加-p選項。
-
no_command:修正不存在的命令,例如vom修正為vim。
-
man_no_space:將沒有空格的man命令添加空格,例如mandiff修正為man diff
-
pacman:arch 系統上,如果使用沒有安裝的軟件,嘗試使用pacman或者yaourt安裝。
-
pip_unknown_command:修正錯誤的pip命令,例如pip isntall修正為pip install
-
python_command:當前目錄試圖執行沒有x權限的 python 程序時,修復為添加python前綴
-
sl_ls:sl修正為ls
-
rm_dir:如果刪除文件夾,為rm添加-rf選項。
-
ssh_known_hosts:如果遠程主機公鑰不匹配,嘗試將遠程主機公鑰從know_hosts中移除。
-
sudo:如果提示沒有權限,嘗試添加sudo。
-
switch_layout:將鍵盤布局修正為英語布局。
-
apt_get:如果不存在命令,嘗試使用apt-get包管理器安裝。
-
brew_install:修正brew install的格式。
-
composer_not_command:修正 composer 命令。
沒有啟用的模塊:
-
ls_lah:為ls添加-alh選項。
-
rm_root:為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,這樣可以自己決定是否執行修正后的命令。
介紹內容來自 segmentfault