一款出色的应用程序,可纠正您之前的控制台命令
项目描述
The Fuck 是一个由 [@liamosaur](https://twitter.com/liamosaur/) 推文 启发而创建的出色应用程序,可纠正之前的控制台命令中的错误。
如果 The 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 [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
...
目录
要求
python (3.4+)
pip
python-dev
返回目录
安装
在 macOS 或 Linux 上,您可以通过 Homebrew 安装 The Fuck
brew install thefuck
在 Ubuntu / Mint 上,使用以下命令安装 The Fuck
sudo apt update
sudo apt install python3-dev python3-pip python3-setuptools
pip3 install thefuck --user
在 FreeBSD 上,使用以下命令安装 The Fuck
pkg install thefuck
在 ChromeOS 上,使用 chromebrew 的以下命令安装 The Fuck
crew install thefuck
在其他系统上,通过 pip 安装 The Fuck
pip install thefuck
或者,您可以使用操作系统包管理器(OS X,Ubuntu,Arch)。
建议您将此命令放置在 .bash_profile,.bashrc,.zshrc 或其他启动脚本中
eval $(thefuck --alias)
# You can use whatever you want as an alias, like for Mondays:
eval $(thefuck --alias FUCK)
或者在您的shell配置(Bash,Zsh,Fish,Powershell,tcsh)中。
更改仅在新的shell会话中可用。要立即使更改生效,请运行 source ~/.bashrc(或您的shell配置文件,如.zshrc)。
要运行无需确认的固定命令,请使用--yeah选项(或简写为-y,如果您特别沮丧,可以使用--hard)。
fuck --yeah
要递归地修复命令直到成功,请使用-r选项。
fuck -r
返回目录
更新
pip3 install thefuck --upgrade
注意:《The Fuck》的别名功能在v1.34版本中已更改。
卸载
要卸载《The Fuck》,请逆转安装过程:- 从Bash、Zsh、Fish、Powershell、tcsh等shell配置中删除或注释掉thefuck别名行 - 使用您的包管理器(brew、pip3、pkg、crew、pip)卸载二进制文件
工作原理
The Fuck会尝试将上一个命令与规则相匹配。如果找到匹配项,将使用匹配的规则创建新命令并执行。以下规则默认启用:
adb_unknown_command - 修复拼写错误的命令,如adb logcta;
ag_literal - 在建议时向ag添加-Q;
aws_cli - 修复拼写错误的命令,如aws dynamdb scan;
az_cli - 修复拼写错误的命令,如az providers;
cargo - 运行cargo build而不是cargo;
cargo_no_command - 修复错误的命令,如cargo buid;
cat_dir - 当您尝试cat目录时,将cat替换为ls;
cd_correction - 检查并纠正失败的cd命令;
cd_cs - 将cs更改为cd;
cd_mkdir - 在cd进入目录之前创建目录;
cd_parent - 将cd..更改为cd ..;
chmod_x - 添加执行位;
choco_install - 为chocolatey包添加常用后缀;
composer_not_command - 修复composer命令名称;
conda_mistype - 修复conda命令;
cp_create_destination - 当您尝试将cp或mv到不存在的目录时创建新目录;
cp_omitting_directory - 当您cp目录时添加-a;
cpp11 - 向g++或clang++添加缺少的-std=c++11;
dirty_untar - 修复在当前目录中解包的tar x命令;
dirty_unzip - 修复在当前目录中解压的unzip命令;
django_south_ghost - 为因ghost django south迁移而失败的添加--delete-ghost-migrations;
django_south_merge - 为不一致的django south迁移添加--merge;
docker_login - 执行docker login并重复上一个命令;
docker_not_command – 修复错误的docker命令,例如 docker tags;
docker_image_being_used_by_container – 在删除镜像之前,删除使用该镜像的容器;
dry – 修复重复输入,如 git git push;
fab_command_not_found – 修复拼写错误的fabric命令;
fix_alt_space – 将Alt+Space替换为空格字符;
fix_file – 使用您的 $EDITOR 打开包含错误的文件;
gem_unknown_command – 修复错误的 gem 命令;
git_add – 修复“pathspec ‘foo’ 没有匹配任何git已知的文件。”;
git_add_force – 当路径被 .gitignore 排除时,在 git add <pathspec>... 中添加 --force;
git_bisect_usage – 修复在 bisect 过程中出现的 git bisect strt、git bisect goood、git bisect rset 等错误;
git_branch_delete – 将 git branch -d 更改为 git branch -D;
git_branch_delete_checked_out – 在尝试删除已签出的分支时,将 git branch -d 更改为 git checkout master && git branch -D;
git_branch_exists – 当创建已存在的分支时,提供 git branch -d foo、git branch -D foo 或 git checkout foo;
git_branch_list – 用 git branch list 替换 git branch 并删除创建的分支;
git_branch_0flag – 修复如 git branch 0v 和 git branch 0r 等命令,删除创建的分支;
git_checkout – 修复分支名称或创建新分支;
git_clone_git_clone – 将 git clone git clone ... 替换为 git clone ...;
git_commit_add – 在之前的提交失败且未进行暂存后,提供 git commit -a ... 或 git commit -p ...;
git_commit_amend – 在之前的提交后提供 git commit --amend;
git_commit_reset – 在之前的提交后提供 git reset HEAD~;
git_diff_no_index – 在未跟踪文件上对之前的 git diff 添加 --no-index;
git_diff_staged – 在之前的 git diff 添加 --staged,并出现意外的输出;
git_fix_stash – 修复 git stash 命令(子命令拼写错误和缺少 save);
git_flag_after_filename – 修复 fatal: bad flag '...' after filename 错误;
git_help_aliased – 修复 git help <alias> 命令,用别名命令替换;
git_hook_bypass – 在 git am、git commit 或 git push 命令之前添加 --no-verify 标志;
git_lfs_mistype – 修复拼写错误的 git lfs <command> 命令;
git_main_master – 修复 main 和 master 之间的错误分支名称;
git_merge – 在分支名称中添加远程仓库;
git_merge_unrelated – 当需要时,添加 --allow-unrelated-histories;
git_not_command – 修复错误的 git 命令,例如 git brnch;
git_pull – 在执行之前的 git pull 命令之前设置上游;
git_pull_clone – 当仓库不存在时,克隆而不是拉取;
git_pull_uncommitted_changes – 在拉取之前暂存更改,并在之后弹出;
git_push – 在之前的失败 git push 中添加 --set-upstream origin $branch;
git_push_different_branch_names – 当本地分支名称与远程分支名称不匹配时修复推送操作;
git_push_pull – 当推送被拒绝时运行 git pull;
git_push_without_commits – 在设置新项目时,如果您忘记并只执行 git add .,则创建初始提交;
git_rebase_no_changes – 当没有更改时,运行 git rebase --skip 而不是 git rebase --continue;
git_remote_delete – 将 git remote delete remote_name 替换为 git remote remove remote_name;
git_rm_local_modifications – 当您尝试删除本地修改的文件时添加 -f 或 --cached;
git_rm_recursive – 当您尝试删除目录时添加 -r;
git_rm_staged – 当您尝试删除具有暂存更改的文件时添加 -f 或 --cached;
git_rebase_merge_dir – 在进行变基操作时,提供 git rebase (--continue | --abort | --skip) 或删除 .git/rebase-merge 目录;
git_remote_seturl_add – 在不存在远程仓库上运行 git remote set_url 时运行 git remote add;
git_stash – 在进行变基或切换分支之前暂存本地修改;
git_stash_pop – 在弹出暂存之前添加本地修改,然后重置;
git_tag_force – 当标签已存在时,向 git tag <tagname> 添加 --force;
git_two_dashes – 向如 git commit -amend 或 git rebase -continue 的命令添加缺失的短横线;
go_run – 在编译/运行 Go 程序时附加 .go 扩展名;
go_unknown_command – 修复错误的 go 命令,例如 go bulid;
gradle_no_task – 修复找不到或模糊的 gradle 任务;
gradle_wrapper – 将 gradle 替换为 ./gradlew;
grep_arguments_order – 修复 grep 参数顺序,例如 grep -lir . test 的场景;
grep_recursive – 当尝试在目录中执行 grep 时,添加 -r;
grunt_task_not_found – 修复拼写错误的 grunt 命令;
gulp_not_task – 修复拼写错误的 gulp 任务;
has_exists_script – 当脚本/二进制文件存在时,添加 ./;
heroku_multiple_apps – 在类似 heroku pg 的 heroku 命令中添加 --app <app>;
heroku_not_command – 修复错误的 heroku 命令,例如 heroku log;
history – 尝试将命令替换为历史记录中最相似的命令;
hostscli – 尝试修复 hostscli 的使用;
ifconfig_device_not_found – 修复错误的设备名称,例如将 wlan0 修复为 wlp2s0;
java – 运行 Java 程序时移除 .java 扩展名;
javac – 编译 Java 文件时,添加缺失的 .java 扩展名;
lein_not_task – 修复错误的 lein 任务,例如 lein rpl;
long_form_help – 当不支持简短形式时,将 -h 更改为 --help;
ln_no_hard_link – 在目录上创建硬链接时捕获错误,建议使用符号链接;
ln_s_order – 修复 ln -s 参数顺序;
ls_all – 当输出为空时,向 ls 添加 -A;
ls_lah – 向 ls 添加 -lah;
man – 更改手册章节;
man_no_space – 修复没有空格的手册命令,例如 mandiff;
mercurial – 修复错误的 hg 命令;
missing_space_before_subcommand – 修复缺少空格的命令,如 npminstall;
mkdir_p – 当尝试在没有父目录的情况下创建目录时,添加 -p;
mvn_no_command – 向 mvn 添加 clean package;
mvn_unknown_lifecycle_phase – 修复 mvn 中的拼写错误的生命周期阶段;
npm_missing_script – 修复 npm 自定义脚本的名称在 npm run-script <script> 中;
npm_run_script – 为自定义 npm 脚本添加缺失的 run-script;
npm_wrong_command – 修复错误的 npm 命令,例如 npm urgrade;
no_command – 修复错误的控制台命令,例如 vom/vim;
no_such_file – 使用 mv 和 cp 命令创建缺失的目录;
omnienv_no_such_command – 修复 goenv、nodenv、pyenv 和 rbenv 的错误命令(例如:pyenv install 或 goenv list);
open – 将 http:// 添加到传递给 open 的地址,或者创建一个新的文件或目录并将其传递给 open;
pip_install – 通过添加 --user 或在必要时添加 sudo 修复 pip install 命令的权限问题;
pip_unknown_command – 修复错误的 pip 命令,例如 pip instatl/pip install;
php_s – 当尝试运行本地 php 服务器时,将 -s 替换为 -S;
port_already_in_use – 杀死绑定端口的进程;
prove_recursively – 当使用目录调用时,添加 -r;
python_command – 当尝试运行非可执行/无 ./ 的 python 脚本时,添加 python;
python_execute – 在执行 Python 文件时,添加缺少的 .py;
python_module_error – 通过尝试 pip install 那个模块来修复 ModuleNotFoundError;
quotation_marks – 修复在包含参数时 ' 和 " 的不均匀使用;
path_from_history – 用历史记录中的类似绝对路径替换未找到的路径;
rails_migrations_pending – 运行挂起的迁移;
react_native_command_unrecognized – 修复未识别的 react-native 命令;
remove_shell_prompt_literal – 删除命令文档中常见的开头的 shell 提示符号 $;
remove_trailing_cedilla – 删除尾部带点的字母 ç,这是欧洲键盘布局中常见的错误;
rm_dir – 当尝试删除目录时,添加 -rf;
scm_correction – 将错误的 scm(如 hg log)更正为 git log;
sed_unterminated_s – 为 sed 的 s 命令添加缺少的 ‘/’;
sl_ls – 将 sl 更改为 ls;
ssh_known_hosts – 在警告时从 known_hosts 中删除主机;
sudo – 如果之前的命令由于权限问题而失败,则将 sudo 添加到之前的命令;
sudo_command_from_user_path – 使用 sudo 运行用户 $PATH 中的命令;
switch_lang – 将命令从您的本地布局切换到 en;
systemctl – 正确排序混乱的 systemctl 参数;
terraform_init.py – 在 plan 或 apply 之前运行 terraform init;
test.py – 运行 py.test 而不是 test.py;
touch – 在“touch”之前创建缺失的目录;
tsuru_login – 如果未认证或会话过期,则运行 tsuru login;
tsuru_not_command – 修正错误的 tsuru 命令,例如修正 tsuru shell;
tmux – 修正 tmux 命令;
unknown_command – 修正 hadoop hdfs-style 的“未知命令”,例如在 hdfs dfs ls 上添加缺失的‘-’;
unsudo – 如果进程拒绝以超级用户权限运行,则从先前命令中删除 sudo;
vagrant_up – 启动 vagrant 实例;
whois – 修正 whois 命令;
workon_doesnt_exists – 修正 virtualenvwrapper 环境名不存在时建议创建新环境的错误;
wrong_hyphen_before_subcommand – 删除不正确放置的连字符(例如 apt-install -> apt install,git-log -> git log 等);
yarn_alias – 修正别名的 yarn 命令,例如 yarn ls;
yarn_command_not_found – 修正拼写错误的 yarn 命令;
yarn_command_replaced – 修正替换的 yarn 命令;
yarn_help – 使打开 yarn 文档更容易;
返回目录
以下规则仅在特定平台上默认启用
apt_get – 如果未安装,则使用 apt 安装应用程序(需要 python-commandnotfound / python3-commandnotfound);
apt_get_search – 将尝试使用 apt-get 搜索更改为使用 apt-cache 搜索;
apt_invalid_operation – 修正无效的 apt 和 apt-get 调用,例如 apt-get isntall vim;
apt_list_upgradable – 帮助您在 apt update 之后运行 apt list --upgradable;
apt_upgrade – 帮助您在 apt list --upgradable 之后运行 apt upgrade;
brew_cask_dependency – 安装 cask 依赖项;
brew_install – 修正 brew install 的公式名称;
brew_reinstall – 将 brew install <formula> 转换为 brew reinstall <formula>;
brew_link – 如果链接失败,则添加 --overwrite --dry-run;
brew_uninstall – 如果安装了多个版本,则向 brew uninstall 添加 --force;
brew_unknown_command – 修正错误的 brew 命令,例如 brew docto/brew doctor;
brew_update_formula – 将 brew update <formula> 转换为 brew upgrade <formula>;
dnf_no_such_command – 修复误拼的 DNF 命令;
nixos_cmd_not_found – 在 NixOS 上安装应用;
pacman – 如果未安装,则使用 pacman 安装应用(如果可用,则使用 yay 或 yaourt);
pacman_invalid_option – 将 pacman 选项的小写字母替换为大写。
pacman_not_found – 使用 pacman、yay 或 yaourt 修复包名。
yum_invalid_operation – 修复无效的 yum 调用,如 yum isntall vim;
以下命令包含在 The Fuck 中,但默认情况下未启用
git_push_force – 在 git push 中添加 --force-with-lease(可能与 git_push_pull 冲突);
rm_root – 在 rm -rf / 命令中添加 --no-preserve-root。
返回目录
创建自己的规则
要添加自己的规则,请将名为 your-rule-name.py 的文件创建在 ~/.config/thefuck/rules 中。规则文件必须包含两个函数
match(command: Command) -> bool
get_new_command(command: Command) -> str | list[str]
此外,规则还可以包含可选函数
side_effect(old_command: Command, fixed_command: str) -> None
规则还可以包含可选变量 enabled_by_default、requires_output 和 priority。
Command 有三个属性: script、output 和 script_parts。您的规则不应更改 Command。
规则 API 在 3.0 中已更改:要访问规则的设置,使用 from thefuck.conf import settings 导入它
settings 是一个特殊对象,由 ~/.config/thefuck/settings.py 组成,并包含来自 env 的值(见下文)。
一个简单的运行带有 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 = True
def side_effect(command, fixed_command):
subprocess.call('chmod 777 .', shell=True)
priority = 1000 # Lower first, default is 1000
requires_output = True
更多规则示例、规则的实用函数、应用/操作系统特定的辅助工具。
返回目录
设置
可以在文件 $XDG_CONFIG_HOME/thefuck/settings.py 中更改 The Fuck 的几个参数($XDG_CONFIG_HOME 默认为 ~/.config)
rules – 启用规则的列表,默认为 thefuck.const.DEFAULT_RULES;
exclude_rules – 禁用规则的列表,默认为 [];
require_confirmation – 在运行新命令之前需要确认,默认为 True;
wait_command – 获取上一个命令输出的最大秒数;
no_colors – 禁用彩色输出;
priority – 包含规则优先级的字典,优先级较低的规则将被首先匹配;
debug – 启用调试输出,默认为 False;
history_limit – 将扫描的历史命令的数值,如 2000;
alter_history – 将修复后的命令推送到历史记录,默认为 True。
wait_slow_command – 在 slow_commands 列表中获取上一个命令输出时的最大时间(秒);
slow_commands – 慢速命令列表;
num_close_matches – 建议的最大近似匹配数,默认为 3。
excluded_search_path_prefixes – 搜索命令时忽略的路径前缀,默认为 []。
settings.py 的一个示例
rules = ['sudo', 'no_command']
exclude_rules = ['git_push']
require_confirmation = True
wait_command = 10
no_colors = False
priority = {'sudo': 100, 'no_command': 9999}
debug = False
history_limit = 9999
wait_slow_command = 20
slow_commands = ['react-native', 'gradle']
num_close_matches = 5
或通过环境变量
THEFUCK_RULES – 启用的规则列表,如 DEFAULT_RULES:rm_root 或 sudo:no_command;
THEFUCK_EXCLUDE_RULES – 禁用的规则列表,如 git_pull:git_push;
THEFUCK_REQUIRE_CONFIRMATION – 在运行新命令前需要确认,true/false;
THEFUCK_WAIT_COMMAND – 获取上一个命令输出的最大时间(秒);
THEFUCK_NO_COLORS – 禁用彩色输出,true/false;
THEFUCK_PRIORITY – 规则的优先级,如 no_command=9999:apt_get=100,优先级较低的规则会先匹配;
THEFUCK_DEBUG – 启用调试输出,true/false;
THEFUCK_HISTORY_LIMIT – 要扫描的历史命令数量,如 2000;
THEFUCK_ALTER_HISTORY – 将固定命令推送到历史记录,true/false;
THEFUCK_WAIT_SLOW_COMMAND – 如果命令在 slow_commands 列表中,获取上一个命令输出的最大时间(秒);
THEFUCK_SLOW_COMMANDS – 慢速命令列表,如 lein:gradle;
THEFUCK_NUM_CLOSE_MATCHES – 建议的最大近似匹配数,如 5。
THEFUCK_EXCLUDED_SEARCH_PATH_PREFIXES – 搜索命令时忽略的路径前缀,默认为 []。
例如
export THEFUCK_RULES='sudo:no_command'
export THEFUCK_EXCLUDE_RULES='git_pull:git_push'
export THEFUCK_REQUIRE_CONFIRMATION='true'
export THEFUCK_WAIT_COMMAND=10
export THEFUCK_NO_COLORS='false'
export THEFUCK_PRIORITY='no_command=9999:apt_get=100'
export THEFUCK_HISTORY_LIMIT='2000'
export THEFUCK_NUM_CLOSE_MATCHES='5'
返回目录
带有规则的第三方包
如果您想创建一组非公开规则,但仍想与他人分享,请创建一个名为 thefuck_contrib_* 的包,结构如下
thefuck_contrib_foo thefuck_contrib_foo rules __init__.py *third-party rules* __init__.py *third-party-utils* setup.py
“The Fuck” 将在 rules 模块中找到规则。
返回目录
实验性即时模式
“The Fuck” 的默认行为需要时间来重新运行之前的命令。在即时模式下,“The Fuck” 通过使用 script 记录输出,然后读取日志来节省时间。
目前,即时模式仅支持 Python 3 与 bash 或 zsh。要使 thefuck 正常工作,还需要禁用 zsh 的自动纠正功能。
要启用即时模式,请将 --enable-experimental-instant-mode 添加到 .bashrc、.bash_profile 或 .zshrc 中的别名初始化中。
例如
eval $(thefuck --alias --enable-experimental-instant-mode)
返回目录
开发
请参阅 CONTRIBUTING.md
许可证 MIT
项目许可证可以在 这里 找到。
返回目录
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。