注意
本文翻譯並簡化了原文檔水文,如有衝突,請以原文檔為準!
Tip
如無特殊聲明,本文所有指令在 Debian/Ubuntu 系 Linux 發行版中運行最佳
需求與痛點#
Linux 系統的最佳用法自然是 CLI,然而 CLI 的一大痛點就是 —— 沒有圖形介面!這不是廢話嘛
沒有圖形介面,就需要用戶一個字符一個字符地去敲命令來。由於沒有圖形介面這麼直觀,無論是大神還是新手都會時不時犯下一些拼錯字符或打錯字的低級錯誤,但大神和新手不同的是,大神會用命令行快捷鍵來快速糾錯,這篇文章 “Linux 命令行編輯快捷鍵” 詳細地介紹了命令行中一些快捷鍵來輔助 Linuxer 們操作命令。
但,我還是覺得麻煩,輸了一長串命令結果要我去慢慢糾錯,實在是想罵出聲:“Fuck!”
於是,一個極具戲劇性的命令誕生了:TheFuck
功能#
舉例以下內容引用自原開發者nvbn#
➜ 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
...
➜ 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)
...
➜ git brnch
git: 'brnch' is not a git command. See 'git --help'.
Did you mean this?
branch
➜ fuck
git branch [enter/↑/↓/ctrl+c]
* master
➜ lein rpl
'rpl' is not a task. See 'lein help'.
Did you mean this?
repl
➜ fuck
lein repl [enter/↑/↓/ctrl+c]
nREPL server started on port 54848 on host 127.0.0.1 - nrepl://127.0.0.1:54848
REPL-y 0.3.1
...
如果你不怕盲目地運行更正的命令,可以禁用 require_confirmation 設置項:
➜ 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
...
安裝#
依賴#
The Fuck 依賴 Python 運行,nvbn 給出的依賴項為
- Python (3.4+)
- pip
- Python-dev
安裝依賴與本體#
通過以下指令安裝 (其他發行版 Linux請見原文安裝部分)
sudo apt update
sudo apt install python3-dev python3-pip python3-setuptools
sudo pip3 install thefuck
原文處的安裝命令在最後一個命令的地方加了--user
參數,這意味著 The Fuck 被安裝在當前登錄用戶目錄的.local/bin
,需要添加環境變量才能運行,此處進行了修改,直接 root 或 sudo 下安裝該包可跳過該過程。
添加別名#
建議在.bash_profile
, .bashrc
, .zshrc
或其他終端啟動腳本中添加以下命令:eval $(thefuck --alias)
The Fuck 已經支持自動添加,只需要在安裝完畢後輸入兩次fuck
即可。
更新#
pip3 install thefuck --upgrade
使用#
Fuck!#
命令執行錯誤後,高呼一句 Fuck輸入fuck
,再用上下鍵控制選項,回車鍵確認修正即可
參數#
- --yeah
fuck --yeah
或者fuck -y
跳過確認,直接執行修正後的命令
- -r
fuck -r
使 TheFuck 遞歸修正並執行命令,直到命令執行成功
設置#
配置項#
TheFuck 的配置文件默認是~/.config/thefuck/settings.py
rules
– 啟用的規則列表,默認為thefuck.const.DEFAULT_RULES
exclude_rules
– 禁用規則列表,默認為[]
require_confirmation
– 在運行修正後命令之前是否需要確認,默認為True
wait_command
–獲取上個命令輸出的最長時間(以秒為單位)
no_colors
– 禁用彩色輸出
priority
– 具有規則優先級的字典,優先級priority
較低的規則將先行匹配
debug
– 是否啟用調試輸出,默認為False
history_limit
– 掃描多少條歷史命令,如2000
alter_history
– 是否將修正後命令加入到歷史記錄,默認為True
wait_slow_command
– 如果它在slow_commands
列表中,則獲取上個命令輸出的最大時間(以秒為單位)
slow_commands
–慢命令列表
num_close_matches
– 命令修正建議的最大匹配數,默認為 3。
excluded_search_path_prefixes
– 搜索命令時需要忽略的路徑前綴,默認為[]
。
即時模式 (實驗性)#
The Fuck 的默認行為需要時間來重新運行以前的命令。 在即時模式下,The Fuck 通過使用腳本的記錄輸出接著讀取日誌來節省時間。
目前,即時模式僅支持帶有 bash 或 zsh 的 Python 3。 zsh 的自動更正功能也需要被禁用,以便 thefuck 正常工作。
要啟用即時模式,請將
--enable-experimental-instant-mode
添加到.bashrc
、.bash_profile
、.zshrc
等終端啟動腳本中的別名參數內。
舉例如下:
eval $(thefuck --alias --enable-experimental-instant-mode)