【 thefuck 安装与使用】Linux 终端自动纠错工具:一头GitHub上的“草泥马“ - thefuck,妈妈再也不用担心我打错命令行了!

目录

快速安装使用
.

1.简介
2.安装
3.配置
4.补充

官方盗料参考

快速安装使用

快速安装使用,四步即可:

#Ubuntu/Debian系统
sudo apt update
sudo apt install python3-dev python3-pip
sudo pip3 install thefuck
#编辑bashrc配置文件
vim ~/.bashrc
#在文件尾加入一行给thefuck取别名fuck
eval "$(thefuck --alias fuck)"
#使生效
source ~/.bashrc

#其他#
#修正三条命令前的错误	-	 fuck 3
#新到最新的 thefuck	-	 sudo pip3 install thefuck --upgrade

在这里插入图片描述

1. 简介

thefuck 是一个开源的命令行工具,用于自动纠正终端中的错误命令。它通过分析最近执行的命令和错误信息,提供一个或多个可能的正确命令,用户可以选择自动执行或手动确认。

在这里插入图片描述

更多示例:

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.
...

2. 安装

#CentOS系统
yum -y update && yum -y install gcc 
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py && yum -y install python-devel
sudo -H pip install thefuck
 
#Ubuntu/Debian系统
sudo apt update
sudo apt install python3-dev python3-pip
sudo pip3 install thefuck

更多安装及使用方法查看Github项目地址:https://github.com/nvbn/thefuck。

3.配置

#编辑bashrc配置文件
vim ~/.bashrc
#在文件尾加入一行给thefuck取别名fuck
eval "$(thefuck --alias fuck)"
#使生效
source ~/.bashrc

最后使用fuck命令来纠正命令行拼写错误。

4. 补充

  • 历史修正

    扫描二维码关注公众号,回复: 17580870 查看本文章
    #thefuck 可以使用历史记录来更正以前的命令。例如,如果你想修正三条命令前的错误
     $ fuck 3
    
  • 更新到最新的 thefuck 的方法:

     sudo pip3 install thefuck --upgrade
    
  • 自定义部分配置示例

    #thefuck的配置文件位于 ~/.config/thefuck/settings.py。你可以在这里修改一些默认设置,例如:
    #这个参数指定了 thefuck 应用的规则列表。thefuck 会根据这些规则来修正命令错误。如果没有特别指定,thefuck 会使用所有默认的规则
    rules = [<list_of_rules>]
    require_confirmation = True
    wait_command = 3
    
    #例如:
    rules = ['sudo', 'no_command', 'missing_argument']  表示只启用 'sudo''no_command''missing_argument' 这三个规则
    #下列参数指定在修正命令之前是否需要用户确认。如果设置为 True,thefuck 会在执行修正命令之前提示用户确认。如果设置为 False,thefuck 会自动修正并执行命令而不需要用户确认
    require_confirmation = True
    #wait_command = 3:这个参数指定 thefuck 在执行修正命令之前等待的时间(单位:秒)。这是为了给用户提供一个时间窗口来取消修正操作。如果你不需要这个延迟,可以设置为 0
    wait_command = 0
    

    综上示例所述,这些设置帮助你更灵活地控制 thefuck 的行为

    rules = ['sudo', 'no_command', 'missing_argument']
    require_confirmation = True
    wait_command = 3
    

    编写自定义规则

    可以编写自己的规则来处理特定的命令错误。在 ~/.config/thefuck/rules/ 目录中创建一个新的 Python 文件。 示例,创建一个 git_no_branch.py 文件:

    from thefuck.utils import replace_argument, for_app
    from thefuck.specific.git import git_support
    
    @git_support
    def match(command):
        return ('error: src refspec master does not match any' in command.output)
    @git_support
    def get_new_command(command):
       return replace_argument(command.script, 'master', 'main')
    

参考资料

附上原文说明:

Requirements

  • python (3.5+)
  • pip
  • python-dev
Back to Contents

Intallation

On macOS or Linux, you can install The Fuck via Homebrew:

brew install thefuck

On Ubuntu / Mint, install The Fuck with the following commands:

sudo apt update
sudo apt install python3-dev python3-pip python3-setuptools
pip3 install thefuck --user

On FreeBSD, install The Fuck with the following commands:

pkg install thefuck

On ChromeOS, install The Fuck using chromebrew with the following command:

crew install thefuck

On Arch based systems, install The Fuck with the following command:

sudo pacman -S thefuck

On other systems, install The Fuck by using :pip

pip install thefuck

Alternatively, you may use an OS package manager (OS X, Ubuntu, Arch).

# It is recommended that you place this command in your , , or other startup script:.bash_profile``.bashrc``.zshrc

eval $(thefuck --alias)
# You can use whatever you want as an alias, like for Mondays:
eval $(thefuck --alias FUCK)

Or in your shell config (Bash, Zsh, Fish, Powershell, tcsh).

Changes are only available in a new shell session. To make changes immediately available, run (or your shell config file like ).source ~/.bashrc``.zshrc

To run fixed commands without confirmation, use the option (or just for short, or if you’re especially frustrated):--yeah``-y``--hard

fuck --yeah

To fix commands recursively until succeeding, use the option:-r

fuck -r
Back to Contents

Updating

pip3 install thefuck --upgrade

Noe: Alias functionality was changed in v1.34 of *The Fuck*

Uninstall

To remove The Fuck, reverse the installation process:

  • erase or comment thefuck alias line from your Bash, Zsh, Fish, Powershell, tcsh, … shell config

Noe: Alias functionality was changed in v1.34 of *The Fuck*

Uninstall

To remove The Fuck, reverse the installation process:

  • erase or comment thefuck alias line from your Bash, Zsh, Fish, Powershell, tcsh, … shell config
  • use your package manager (brew, pip3, pkg, crew, pip) to uninstall the binaries

猜你喜欢

转载自blog.csdn.net/Thmos_vader/article/details/144668524
今日推荐