nvbn / thefuck

Magnificent app which corrects your previous console command.
MIT License
85.6k stars 3.43k forks source link
python shell

The Fuck Version Build Status Coverage MIT License

The Fuck is a magnificent app, inspired by a @liamosaur tweet, that corrects errors in previous console commands.

Is The Fuck too slow? Try the experimental instant mode!

gif with examples

More examples:

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

If you're not afraid of blindly running corrected commands, the require_confirmation settings option can be disabled:

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

Contents

  1. Requirements
  2. Installations
  3. Updating
  4. How it works
  5. Creating your own rules
  6. Settings
  7. Third party packages with rules
  8. Experimental instant mode
  9. Developing
  10. License

Requirements

Back to Contents

Installation

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 .bash_profile, .bashrc, .zshrc or other startup script:

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 source ~/.bashrc (or your shell config file like .zshrc).

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

fuck --yeah

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

fuck -r
Back to Contents

Updating

pip3 install thefuck --upgrade

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

Uninstall

To remove The Fuck, reverse the installation process:

How it works

The Fuck attempts to match the previous command with a rule. If a match is found, a new command is created using the matched rule and executed. The following rules are enabled by default:

Back to Contents

The following rules are enabled by default on specific platforms only:

The following commands are bundled with The Fuck, but are not enabled by default:

Back to Contents

Creating your own rules

To add your own rule, create a file named your-rule-name.py in ~/.config/thefuck/rules. The rule file must contain two functions:

match(command: Command) -> bool
get_new_command(command: Command) -> str | list[str]

Additionally, rules can contain optional functions:

side_effect(old_command: Command, fixed_command: str) -> None

Rules can also contain the optional variables enabled_by_default, requires_output and priority.

Command has three attributes: script, output and script_parts. Your rule should not change Command.

Rules api changed in 3.0: To access a rule's settings, import it with from thefuck.conf import settings

settings is a special object assembled from ~/.config/thefuck/settings.py, and values from env (see more below).

A simple example rule for running a script with 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

More examples of rules, utility functions for rules, app/os-specific helpers.

Back to Contents

Settings

Several The Fuck parameters can be changed in the file $XDG_CONFIG_HOME/thefuck/settings.py ($XDG_CONFIG_HOME defaults to ~/.config):

An example of 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

Or via environment variables:

For example:

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'
Back to Contents

Third-party packages with rules

If you'd like to make a specific set of non-public rules, but would still like to share them with others, create a package named thefuck_contrib_* with the following structure:

thefuck_contrib_foo
  thefuck_contrib_foo
    rules
      __init__.py
      *third-party rules*
    __init__.py
    *third-party-utils*
  setup.py

The Fuck will find rules located in the rules module.

Back to Contents

Experimental instant mode

The default behavior of The Fuck requires time to re-run previous commands. When in instant mode, The Fuck saves time by logging output with script, then reading the log.

gif with instant mode

Currently, instant mode only supports Python 3 with bash or zsh. zsh's autocorrect function also needs to be disabled in order for thefuck to work properly.

To enable instant mode, add --enable-experimental-instant-mode to the alias initialization in .bashrc, .bash_profile or .zshrc.

For example:

eval $(thefuck --alias --enable-experimental-instant-mode)
Back to Contents

Developing

See CONTRIBUTING.md

License MIT

Project License can be found here.

Back to Contents