thefuck

command module
v0.0.0-...-6a7235a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 9 Imported by: 0

README

The Fuck Build Status MIT License

The Fuck is a magnificent app, ported from nvbn's amazing python version which was inspired by a @liamosaur tweet, that corrects errors in previous console commands. This mainly exists because installing python packages keeps getting worse. This makes me sad, but it seems like the python community can't figure this out. Or figures it out in multiple, incompatible ways.

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.
...
$ git brnch
git: 'brnch' is not a git command. See 'git --help'.

Did you mean this?
    branch

$ fuck
git branch [enter/ctrl-c]
* master

Contents

  1. Installation
  2. Updating
  3. How it works
  4. Contributing rules
  5. Developing
  6. License

Installation

Install with Go:

go install github.com/lyda/thefuck@main

Then add the alias to your shell config:

Shell Files to edit What to add
bash ~/.bashrc or ~/.bash_profile eval "$(thefuck)"
zsh ~/.zshrc eval "$(thefuck)"
fish ~/.config/fish/config.fish thefuck | source
tcsh ~/.tcshrc eval $(thefuck)
PowerShell $profile iex "$(thefuck init powershell)"

Note for csh users and users of older tcsh shells: You'll want to replace $() with backticks.

Note for PowerShell users: The Fuck will helpfully suggest a superior operating system. This is the full extent of PowerShell support and we consider it a feature.

Changes are only available in a new shell session. To apply immediately, source your config file (e.g. source ~/.bashrc) or just type the one-liner above.

Updating

go install github.com/lyda/thefuck@main

How it works

The Fuck re-runs the previous command and matches the output against a set of rules. If a match is found, a new corrected command is created and presented for confirmation. Press Enter to run it, or Ctrl-C to abort.

The following rules are included:

  • adb_unknown_command – fixes misspelled commands like adb logcta;
  • ag_literal – adds -Q to ag when suggested;
  • apt_get – installs app from apt if it is not installed;
  • apt_get_search – changes trying to search using apt-get with searching using apt-cache;
  • apt_invalid_operation – fixes invalid apt and apt-get calls, like apt-get isntall vim;
  • apt_list_upgradable – helps you run apt list --upgradable after apt update;
  • apt_upgrade – helps you run apt upgrade after apt list --upgradable;
  • aws_cli – fixes misspelled commands like aws dynamdb scan;
  • az_cli – fixes misspelled commands like az providers;
  • brew_cask_dependency – installs cask dependencies;
  • brew_install – fixes formula name for brew install;
  • brew_link – adds --overwrite --dry-run if linking fails;
  • brew_reinstall – turns brew install <formula> into brew reinstall <formula>;
  • brew_uninstall – adds --force to brew uninstall if multiple versions were installed;
  • brew_unknown_command – fixes wrong brew commands, for example brew docto/brew doctor;
  • brew_update_formula – turns brew update <formula> into brew upgrade <formula>;
  • cargo – runs cargo build instead of cargo;
  • cargo_no_command – fixes wrong commands like cargo buid;
  • cat_dir – replaces cat with ls when you try to cat a directory;
  • cd_correction – spellchecks and corrects failed cd commands;
  • cd_cs – changes cs to cd;
  • cd_mkdir – creates directories before cd'ing into them;
  • cd_parent – changes cd.. to cd ..;
  • chmod_x – adds execution bit;
  • choco_install – appends common suffixes for chocolatey packages;
  • composer_not_command – fixes composer command name;
  • conda_mistype – fixes conda commands;
  • cp_create_destination – creates a new directory when you attempt to cp or mv to a non-existent one;
  • cp_omitting_directory – adds -a when you cp a directory;
  • cpp11 – adds missing -std=c++11 to g++ or clang++;
  • dirty_untar – fixes tar x command that untarred in the current directory;
  • dirty_unzip – fixes unzip command that unzipped in the current directory;
  • django_south_ghost – adds --delete-ghost-migrations to failed django south migration;
  • django_south_merge – adds --merge to inconsistent django south migration;
  • docker_image_being_used_by_container – removes the container that is using the image before removing the image;
  • docker_login – executes a docker login and repeats the previous command;
  • docker_not_command – fixes wrong docker commands like docker tags;
  • dry – fixes repetitions like git git push;
  • fab_command_not_found – fixes misspelled fabric commands;
  • fix_alt_space – replaces Alt+Space with Space character;
  • fix_file – opens a file with an error in your $EDITOR;
  • gem_unknown_command – fixes wrong gem commands;
  • git_add – fixes "pathspec 'foo' did not match any file(s) known to git.";
  • git_add_force – adds --force to git add <pathspec>... when paths are .gitignore'd;
  • git_bisect_usage – fixes git bisect strt, git bisect goood, etc. when bisecting;
  • git_branch_0flag – fixes commands such as git branch 0v and git branch 0r;
  • git_branch_delete – changes git branch -d to git branch -D;
  • git_branch_delete_checked_out – changes git branch -d to git checkout master && git branch -D when deleting a checked out branch;
  • git_branch_exists – offers git branch -d foo, git branch -D foo or git checkout foo when creating a branch that already exists;
  • git_branch_list – catches git branch list in place of git branch and removes created branch;
  • git_checkout – fixes branch name or creates new branch;
  • git_clone_git_clone – replaces git clone git clone ... with git clone ...;
  • git_clone_missing – adds git clone to URLs that appear to link to a git repository;
  • git_commit_add – offers git commit -a ... or git commit -p ... after failed commit with nothing staged;
  • git_commit_amend – offers git commit --amend after previous commit;
  • git_commit_reset – offers git reset HEAD~ after previous commit;
  • git_diff_no_index – adds --no-index to git diff on untracked files;
  • git_diff_staged – adds --staged to git diff with unexpected output;
  • git_fix_stash – fixes git stash commands (misspelled subcommand and missing save);
  • git_flag_after_filename – fixes fatal: bad flag '...' after filename;
  • git_go – fixes git install/git get/git modgo install/go get/go mod;
  • git_help_aliased – fixes git help <alias> replacing the alias with the aliased command;
  • git_hook_bypass – adds --no-verify flag to git am, git commit, or git push;
  • git_lfs_mistype – fixes mistyped git lfs <command> commands;
  • git_main_master – fixes incorrect branch name between main and master;
  • git_merge – adds remote to branch names;
  • git_merge_unrelated – adds --allow-unrelated-histories when required;
  • git_not_command – fixes wrong git commands like git brnch;
  • git_pull – sets upstream before executing git pull;
  • git_pull_clone – clones instead of pulling when the repo does not exist;
  • git_pull_uncommitted_changes – stashes changes before pulling and pops them afterwards;
  • git_push – adds --set-upstream origin $branch to failed git push;
  • git_push_different_branch_names – fixes pushes when local and remote branch names differ;
  • git_push_force – adds --force-with-lease to a git push;
  • git_push_pull – runs git pull when push was rejected;
  • git_push_without_commits – creates an initial commit when you git add . but forget to commit;
  • git_rebase_merge_dir – offers git rebase (--continue | --abort | --skip) or removing .git/rebase-merge when a rebase is in progress;
  • git_rebase_no_changes – runs git rebase --skip instead of git rebase --continue when there are no changes;
  • git_remote_delete – replaces git remote delete with git remote remove;
  • git_remote_seturl_add – runs git remote add when git remote set-url is used on a nonexistent remote;
  • git_rm_local_modifications – adds -f or --cached when you try to rm a locally modified file;
  • git_rm_recursive – adds -r when you try to rm a directory;
  • git_rm_staged – adds -f or --cached when you try to rm a file with staged changes;
  • git_stash – stashes your local modifications before rebasing or switching branch;
  • git_stash_pop – adds your local modifications before popping stash, then resets;
  • git_tag_force – adds --force to git tag <tagname> when the tag already exists;
  • git_two_dashes – adds a missing dash to commands like git commit -amend or git rebase -continue;
  • go_git – fixes go pull/go push/go cogit pull/git push/git co;
  • go_run – appends .go extension when compiling/running Go programs;
  • go_unknown_command – fixes wrong go commands, for example go bulid;
  • gradle_no_task – fixes not found or ambiguous gradle task;
  • gradle_wrapper – replaces gradle with ./gradlew;
  • grep_arguments_order – fixes grep arguments order for situations like grep -lir . test;
  • grep_recursive – adds -r when you try to grep a directory;
  • grunt_task_not_found – fixes misspelled grunt commands;
  • gulp_not_task – fixes misspelled gulp tasks;
  • has_exists_script – prepends ./ when script/binary exists in the current directory;
  • heroku_multiple_apps – adds --app <app> to heroku commands like heroku pg;
  • heroku_not_command – fixes wrong heroku commands like heroku log;
  • history – replaces command with the most similar command from history;
  • hostscli – fixes hostscli usage;
  • ifconfig_device_not_found – fixes wrong device names like wlan0 to wlp2s0;
  • java – removes .java extension when running Java programs;
  • javac – appends missing .java when compiling Java files;
  • lein_not_task – fixes wrong lein tasks like lein rpl;
  • ln_no_hard_link – catches hard link creation on directories, suggests symbolic link;
  • ln_s_order – fixes ln -s arguments order;
  • long_form_help – changes -h to --help when the short form is not supported;
  • ls_all – adds -A to ls when output is empty;
  • ls_lah – adds -lah to ls;
  • man – changes manual section;
  • man_no_space – fixes man commands without spaces, for example mandiff;
  • mercurial – fixes wrong hg commands;
  • missing_space_before_subcommand – fixes commands with missing space like npminstall;
  • mkdir_p – adds -p when you try to create a directory without a parent;
  • mvn_no_command – adds clean package to mvn;
  • mvn_unknown_lifecycle_phase – fixes misspelled lifecycle phases with mvn;
  • nixos_cmd_not_found – installs apps on NixOS;
  • no_command – fixes wrong console commands, for example vom/vim;
  • no_such_file – creates missing directories with mv and cp commands;
  • npm_missing_script – fixes npm custom script name in npm run-script <script>;
  • npm_run_script – adds missing run-script for custom npm scripts;
  • npm_wrong_command – fixes wrong npm commands like npm urgrade;
  • omnienv_no_such_command – fixes wrong commands for goenv, nodenv, pyenv and rbenv;
  • open – prepends http:// to addresses passed to open, or creates missing files/directories;
  • pacman – installs app with pacman if it is not installed (uses yay, pikaur or yaourt if available);
  • pacman_invalid_option – replaces lowercase pacman options with uppercase;
  • pacman_not_found – fixes package name with pacman, yay, pikaur or yaourt;
  • path_from_history – replaces not-found path with a similar path from history;
  • php_s – replaces -s with -S when trying to run a local php server;
  • pip_install – fixes permission issues with pip install by adding --user or sudo;
  • pip_unknown_command – fixes wrong pip commands, for example pip instatl;
  • port_already_in_use – kills the process bound to a port;
  • prove_recursively – adds -r when called with a directory;
  • python_command – prepends python when you try to run a non-executable Python script;
  • python_execute – appends missing .py when executing Python files;
  • python_module_error – fixes ModuleNotFoundError by trying to pip install the module;
  • quotation_marks – fixes uneven usage of ' and " in arguments;
  • rails_migrations_pending – runs pending migrations;
  • react_native_command_unrecognized – fixes unrecognized react-native commands;
  • remove_shell_prompt_literal – removes leading $ prompt symbol, common when copying commands from documentation;
  • remove_trailing_cedilla – removes trailing cedillas ç, a common typo on European keyboards;
  • rm_dir – adds -rf when you try to remove a directory;
  • rm_root – adds --no-preserve-root to rm -rf /;
  • scm_correction – corrects wrong scm like hg log to git log;
  • sed_unterminated_s – adds missing / to sed's s commands;
  • sl – changes sl to ls;
  • ssh_known_hosts – removes host from known_hosts on warning;
  • sudo – prepends sudo to the previous command if it failed due to permissions;
  • sudo_command_from_user_path – runs commands from user $PATH with sudo;
  • switch_lang – switches command from your local keyboard layout to en;
  • systemctl – correctly orders parameters of confusing systemctl;
  • terraform_init – runs terraform init before plan or apply;
  • terraform_no_command – fixes unrecognized terraform commands;
  • tmux – fixes tmux commands;
  • touch – creates missing directories before touching a file;
  • tsuru_login – runs tsuru login if not authenticated or session expired;
  • tsuru_not_command – fixes wrong tsuru commands like tsuru shell;
  • unknown_command – fixes hadoop hdfs-style "unknown command", e.g. adds missing - to hdfs dfs ls;
  • unsudo – removes sudo from previous command if the process refuses to run as superuser;
  • vagrant_up – starts up the vagrant instance;
  • whois – fixes whois command;
  • workon_doesnt_exists – fixes virtualenvwrapper env name or suggests creating a new one;
  • wrong_hyphen_before_subcommand – removes an improperly placed hyphen (apt-installapt install);
  • yarn_alias – fixes aliased yarn commands like yarn ls;
  • yarn_command_not_found – fixes misspelled yarn commands;
  • yarn_command_replaced – fixes replaced yarn commands;
  • yarn_help – makes it easier to open yarn documentation;
  • yum_invalid_operation – fixes invalid yum calls, like yum isntall vim;

Contributing rules

Rules are static Go code in internal/rules/. Each rule is a file that calls register() in its init() function:

package rules

import "github.com/lyda/thefuck/internal/types"

func init() {
    register(Rule{
        Name: "my_rule",
        Match: func(cmd types.Command) bool {
            return strings.Contains(cmd.Output, "some error")
        },
        GetNewCommand: func(cmd types.Command) []types.CorrectedCommand {
            return single(strings.Replace(cmd.Script, "wrong", "right", 1))
        },
    })
}

Helper functions available in the rules package:

  • single(script) – returns a single correction;
  • multi([]string) – returns multiple corrections;
  • shellAnd(cmds...) – joins commands with the shell's AND operator;
  • replaceArgument(script, from, to) – replaces a shell word in a command;
  • getCloseMatches(word, possibilities, cutoff) – fuzzy match, cutoff 0.0–1.0;
  • getAllMatchedCommands(output, separators) – extracts suggestions after a separator line.

types.Command has two fields: Script (the command string) and Output (combined stdout+stderr from re-running it), plus ScriptParts() which splits the script into shell words.

Developing

git clone https://github.com/lyda/thefuck
cd thefuck
make check   # fmt, vet, staticcheck, gosec, test
make build   # builds ./bin/thefuck
make         # does both

License MIT

Project License can be found here.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
ui

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL