README
¶
The Fuck

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.
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
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 likeadb logcta;ag_literal– adds-Qtoagwhen suggested;apt_get– installs app from apt if it is not installed;apt_get_search– changes trying to search usingapt-getwith searching usingapt-cache;apt_invalid_operation– fixes invalidaptandapt-getcalls, likeapt-get isntall vim;apt_list_upgradable– helps you runapt list --upgradableafterapt update;apt_upgrade– helps you runapt upgradeafterapt list --upgradable;aws_cli– fixes misspelled commands likeaws dynamdb scan;az_cli– fixes misspelled commands likeaz providers;brew_cask_dependency– installs cask dependencies;brew_install– fixes formula name forbrew install;brew_link– adds--overwrite --dry-runif linking fails;brew_reinstall– turnsbrew install <formula>intobrew reinstall <formula>;brew_uninstall– adds--forcetobrew uninstallif multiple versions were installed;brew_unknown_command– fixes wrong brew commands, for examplebrew docto/brew doctor;brew_update_formula– turnsbrew update <formula>intobrew upgrade <formula>;cargo– runscargo buildinstead ofcargo;cargo_no_command– fixes wrong commands likecargo buid;cat_dir– replacescatwithlswhen you try tocata directory;cd_correction– spellchecks and corrects failedcdcommands;cd_cs– changescstocd;cd_mkdir– creates directories beforecd'ing into them;cd_parent– changescd..tocd ..;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 tocpormvto a non-existent one;cp_omitting_directory– adds-awhen youcpa directory;cpp11– adds missing-std=c++11tog++orclang++;dirty_untar– fixestar xcommand that untarred in the current directory;dirty_unzip– fixesunzipcommand that unzipped in the current directory;django_south_ghost– adds--delete-ghost-migrationsto failed django south migration;django_south_merge– adds--mergeto 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 adocker loginand repeats the previous command;docker_not_command– fixes wrong docker commands likedocker tags;dry– fixes repetitions likegit 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 wronggemcommands;git_add– fixes "pathspec 'foo' did not match any file(s) known to git.";git_add_force– adds--forcetogit add <pathspec>...when paths are .gitignore'd;git_bisect_usage– fixesgit bisect strt,git bisect goood, etc. when bisecting;git_branch_0flag– fixes commands such asgit branch 0vandgit branch 0r;git_branch_delete– changesgit branch -dtogit branch -D;git_branch_delete_checked_out– changesgit branch -dtogit checkout master && git branch -Dwhen deleting a checked out branch;git_branch_exists– offersgit branch -d foo,git branch -D fooorgit checkout foowhen creating a branch that already exists;git_branch_list– catchesgit branch listin place ofgit branchand removes created branch;git_checkout– fixes branch name or creates new branch;git_clone_git_clone– replacesgit clone git clone ...withgit clone ...;git_clone_missing– addsgit cloneto URLs that appear to link to a git repository;git_commit_add– offersgit commit -a ...orgit commit -p ...after failed commit with nothing staged;git_commit_amend– offersgit commit --amendafter previous commit;git_commit_reset– offersgit reset HEAD~after previous commit;git_diff_no_index– adds--no-indextogit diffon untracked files;git_diff_staged– adds--stagedtogit diffwith unexpected output;git_fix_stash– fixesgit stashcommands (misspelled subcommand and missingsave);git_flag_after_filename– fixesfatal: bad flag '...' after filename;git_go– fixesgit install/git get/git mod→go install/go get/go mod;git_help_aliased– fixesgit help <alias>replacing the alias with the aliased command;git_hook_bypass– adds--no-verifyflag togit am,git commit, orgit push;git_lfs_mistype– fixes mistypedgit lfs <command>commands;git_main_master– fixes incorrect branch name betweenmainandmaster;git_merge– adds remote to branch names;git_merge_unrelated– adds--allow-unrelated-historieswhen required;git_not_command– fixes wrong git commands likegit brnch;git_pull– sets upstream before executinggit 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 $branchto failedgit push;git_push_different_branch_names– fixes pushes when local and remote branch names differ;git_push_force– adds--force-with-leaseto agit push;git_push_pull– runsgit pullwhenpushwas rejected;git_push_without_commits– creates an initial commit when yougit add .but forget to commit;git_rebase_merge_dir– offersgit rebase (--continue | --abort | --skip)or removing.git/rebase-mergewhen a rebase is in progress;git_rebase_no_changes– runsgit rebase --skipinstead ofgit rebase --continuewhen there are no changes;git_remote_delete– replacesgit remote deletewithgit remote remove;git_remote_seturl_add– runsgit remote addwhengit remote set-urlis used on a nonexistent remote;git_rm_local_modifications– adds-for--cachedwhen you try torma locally modified file;git_rm_recursive– adds-rwhen you try torma directory;git_rm_staged– adds-for--cachedwhen you try torma 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--forcetogit tag <tagname>when the tag already exists;git_two_dashes– adds a missing dash to commands likegit commit -amendorgit rebase -continue;go_git– fixesgo pull/go push/go co→git pull/git push/git co;go_run– appends.goextension when compiling/running Go programs;go_unknown_command– fixes wronggocommands, for examplego bulid;gradle_no_task– fixes not found or ambiguousgradletask;gradle_wrapper– replacesgradlewith./gradlew;grep_arguments_order– fixesgreparguments order for situations likegrep -lir . test;grep_recursive– adds-rwhen you try togrepa directory;grunt_task_not_found– fixes misspelledgruntcommands;gulp_not_task– fixes misspelledgulptasks;has_exists_script– prepends./when script/binary exists in the current directory;heroku_multiple_apps– adds--app <app>toherokucommands likeheroku pg;heroku_not_command– fixes wrongherokucommands likeheroku log;history– replaces command with the most similar command from history;hostscli– fixeshostscliusage;ifconfig_device_not_found– fixes wrong device names likewlan0towlp2s0;java– removes.javaextension when running Java programs;javac– appends missing.javawhen compiling Java files;lein_not_task– fixes wrongleintasks likelein rpl;ln_no_hard_link– catches hard link creation on directories, suggests symbolic link;ln_s_order– fixesln -sarguments order;long_form_help– changes-hto--helpwhen the short form is not supported;ls_all– adds-Atolswhen output is empty;ls_lah– adds-lahtols;man– changes manual section;man_no_space– fixes man commands without spaces, for examplemandiff;mercurial– fixes wronghgcommands;missing_space_before_subcommand– fixes commands with missing space likenpminstall;mkdir_p– adds-pwhen you try to create a directory without a parent;mvn_no_command– addsclean packagetomvn;mvn_unknown_lifecycle_phase– fixes misspelled lifecycle phases withmvn;nixos_cmd_not_found– installs apps on NixOS;no_command– fixes wrong console commands, for examplevom/vim;no_such_file– creates missing directories withmvandcpcommands;npm_missing_script– fixesnpmcustom script name innpm run-script <script>;npm_run_script– adds missingrun-scriptfor customnpmscripts;npm_wrong_command– fixes wrong npm commands likenpm urgrade;omnienv_no_such_command– fixes wrong commands forgoenv,nodenv,pyenvandrbenv;open– prependshttp://to addresses passed toopen, or creates missing files/directories;pacman– installs app withpacmanif it is not installed (usesyay,pikauroryaourtif available);pacman_invalid_option– replaces lowercasepacmanoptions with uppercase;pacman_not_found– fixes package name withpacman,yay,pikauroryaourt;path_from_history– replaces not-found path with a similar path from history;php_s– replaces-swith-Swhen trying to run a local php server;pip_install– fixes permission issues withpip installby adding--userorsudo;pip_unknown_command– fixes wrongpipcommands, for examplepip instatl;port_already_in_use– kills the process bound to a port;prove_recursively– adds-rwhen called with a directory;python_command– prependspythonwhen you try to run a non-executable Python script;python_execute– appends missing.pywhen executing Python files;python_module_error– fixesModuleNotFoundErrorby trying topip installthe module;quotation_marks– fixes uneven usage of'and"in arguments;rails_migrations_pending– runs pending migrations;react_native_command_unrecognized– fixes unrecognizedreact-nativecommands;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-rfwhen you try to remove a directory;rm_root– adds--no-preserve-roottorm -rf /;scm_correction– corrects wrong scm likehg logtogit log;sed_unterminated_s– adds missing/tosed'sscommands;sl– changessltols;ssh_known_hosts– removes host fromknown_hostson warning;sudo– prependssudoto the previous command if it failed due to permissions;sudo_command_from_user_path– runs commands from user$PATHwithsudo;switch_lang– switches command from your local keyboard layout to en;systemctl– correctly orders parameters of confusingsystemctl;terraform_init– runsterraform initbefore plan or apply;terraform_no_command– fixes unrecognizedterraformcommands;tmux– fixestmuxcommands;touch– creates missing directories before touching a file;tsuru_login– runstsuru loginif not authenticated or session expired;tsuru_not_command– fixes wrongtsurucommands liketsuru shell;unknown_command– fixes hadoop hdfs-style "unknown command", e.g. adds missing-tohdfs dfs ls;unsudo– removessudofrom previous command if the process refuses to run as superuser;vagrant_up– starts up the vagrant instance;whois– fixeswhoiscommand;workon_doesnt_exists– fixesvirtualenvwrapperenv name or suggests creating a new one;wrong_hyphen_before_subcommand– removes an improperly placed hyphen (apt-install→apt install);yarn_alias– fixes aliasedyarncommands likeyarn ls;yarn_command_not_found– fixes misspelledyarncommands;yarn_command_replaced– fixes replacedyarncommands;yarn_help– makes it easier to openyarndocumentation;yum_invalid_operation– fixes invalidyumcalls, likeyum 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
¶
There is no documentation for this package.
