Quickly Rectify Time-Consuming Terminal Typos on Linux With thefuck

David Rutland
David Rutland CLI

If you spend much time in the Linux terminal, you'll occasionally make typos which disrupt your workflow, and distract you from your task as you digest the error message, work out what you did wrong then retype the correct command.

thefuck is a utility you can use to quickly and automatically correct your mistake, so you can crack on with your day without cursing.

It's Easy to Make Typing Mistakes in the Linux Terminal

multiple exposure image of an angry person screaming through several mouths

Most of the cool stuff you can do in Linux takes place in the terminal. From basic tasks such as downloading a new distro, to more complex activities like trying to install and configure nginx on Ubuntu, you're likely to spend a lot of time on the command line.

Typically, this involves entering multiple commands, and sometimes you'll slip up.

It could be that you attempt an operation without the correct privilege level, or a simple spelling mistake might result in an error message. If this happens, your console will spit out an error message, which you then have to read, decipher, and rectify.

Your exasperated instant verbal response is likely to be a single syllable expletive. Or if you're trying to work out what went wrong, a two word phrase might be more appropriate.

thefuck Were You Trying to Do?

To a human observer, watching over your shoulder it would be obvious what you were trying to do, and it's exasperating that your Linux terminal is incapable of fulfilling the most basic spellcheck functions.

thefuck knows what you wanted to do, and instead of repeating the entire command, you can simply type:

thefuck

...and the correct command will appear on the command line ready for you to hit return.

thefuck works by comparing your erroneous input against an extensive set of pre-defined rules.

These include fixes to repetitions, removing the .java extension when running Java programs, adding "-rf" when you try to remove a directory, and more.

thefuck will also attempt to replace a command with the most similar command from your bash history.

Install and Alias thefuck on Linux

Installing thefuck on Linux using pip

There's a variety of ways to install thefuck on Linux.
If you're using Debian or Ubuntu, enter:

sudo apt install thefuck

On Arch based systems, use the following command:

sudo pacman -S thefuck

On members of the Fedora family, use:

sudo dnf install thefuck

The version in the repositories is not necessarily up-to-date, and may lack the latest correction suggestions.

To install the latest version, first install Python and pip3 then use pip3:

pip3 install thefuck --user

That's it. thefuck is now on your system.

One self-evident problem with using thefuck on a shared system, in an office, or in an environment into which children are likely to stray, is that the name contains a heavyweight obscenity.

It's a good idea to alias thefuck, both for brevity and propriety.

Choose a name by which you'd like to invoke thefuck. We're going with "bonk" as it's thematically appropriate and child-safe.

You can set up an alias for thefuck by entering the following command into any terminal:

eval $(thefuck --alias bonk)

To make your alias available immediately, run:

source ~/.bashrc

Use thefuck to Automatically Correct Terminal Errors on Linux

For most Linux terminal users, it won't be long until you make an error while tapping out a command.

Perhaps you attempted to update packages using your user account without using sudo. Entering:

apt update

...will result in a permission denied error.

Rather than retype the entire command again, you can simply enter your alias for thefuck:

bonk

When you hit Enter, the program will give present what it thinks you meant to type. You can hit Enter again to have it automatically

If you're confident that thefuck is consistently giving correct suggestions, you can append "--yeah" or "-y" to the command to have it entered without confirmation. You can even add it to your alias.

correcting ping command with thefuck

Making a typo while trying to ping this very site:

png reallyuse.com

...will result in a "Command 'png' not found" error, but using our alias with the -y flag:

bonk -y

...will see ping responses appear instantly.

Save Time on the Command Line by Not Making Mistakes!

While thefuck, and whatever alias you choose, can help you to alleviate the stress and consequences or command line mistakes, it's better if you don't make any to start with.

Improve your typing skills to keep typos and spelling errors to a minimum.