thef***: Automatically Correct Previous Incorrect Command

thef*** is a Linux utility that comes as a python package can be installed via pip command to provide you automatic features to correct previous incorrect commands.

Although various tools provide such features for Linux shells, such as autosuggestion and refined manual page, thef*** tool also comes in the middle of all other tools.

Prerequisite

Two tools, python3 and pip3, are required on your system before starting the installation process for thef*** command in Linux.

Type the below command into your terminal to check whether it is already installed on your system.

#To check python3 is installed on your system
$ python3 -V
Python 3.8.10
#To check pip3 is installed on your system
$ pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

Install thef*** on Linux

After ensuring that both the required tool is installed on your system, use one of the below-mentioned methods depending upon your system to install thef*** command.

On Debian/Ubuntu/PopOS/Mint/RedHat/Fedora/CentOS/Arch/Manjaro

$ pip3 install thefuck --user

Now navigate to your home directory and edit either “.bashrc” for the bash shell or “.zshrc” for zsh shell, and add “eval $(thefuck --alias FUCK)” this line at the end of the file and save exit.

Finally, start the new shell session, or to make changes reflected immediately, execute “source ./bashrc” for bash shell or “source ./zshrc” for zsh shell in the terminal.

On macOS

use the Homebrew command to install thef*** on your mac system, as shown below.

$ brew install thefuck

Usage

To autocorrect every previously incorrect command, type f*** as the following command for python is incorrect.

#Example 1

$ puthon
puthon: command not found
$ fuck
python [enter/↑/↓/ctrl+c]
Python 2.7.18 (default, Mar  8 2021, 13:02:45) 
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

First, I have entered puthon which incorrect command for python, so corrected it using f*** command.

Autocorrect the wrong command using thef*** command
Autocorrect the wrong command using thef*** command

#Example 2

To update your repository for the Debian based operating system, the correct command is the apt update. What if I write aptupdate without providing space between apt and update?

[email protected]:~$ aptupdate
aptupdate: command not found
[email protected]:~$ fuck
nsupdate [enter/↑/↓/ctrl+c]

At this time, instead of directly suggesting an apt update thef*** command suggesting nsupdate for aptupdate command.

It is finding relatable command to the previous wrong entered command. If the output command is incorrect for you, then navigate to some other sample related to your wrong executed command using the up or down arrow key.

Navigating to right command for the wrong suggestion in thef***
Navigating to the right command for the wrong suggestion in thef***

After confirming the right suggestion, press enter to execute the command as shown below.

$ aptupdate
aptupdate: command not found
$ fuck
sudo apt update [enter/↑/↓/ctrl+c]
Reading package lists... Done

Below is the behaviour of the above command.

Navigating and confirming the right command for the incorrectly entered command
Navigating and confirming the right command for the incorrectly entered command

Optional Arguments

In thef*** command, two important optional flag/arguments need to be considered, ---yeah or -y for direct execution of suggested commands and -r to execute until the correct command.

For example, you accidentally executed any command such as puthon for python and want to run the first suggested command in thef*** use -y flags as shown below.

Instant execution for the correct suggested command
Instant execution for the correct suggested command

If you try to perform the same method for the wrong aptupdate command as shown below.

Executing wrong suggested command
Executing the wrong suggested command

It will execute the wrong command due to the incorrect suggestion that you need to specify the correct command using the up and down arrow key and press enter without -y flag as we did earlier.

Correcting apt update command using f*** command
Correcting apt update command using f*** command

As you can see above, the apt update command unable execute due to sudo privileges. In such a situation, you can use the -r flag.

The -r flag executes until the command is error-free, as shown below.

Execute until the error-free command
Execute until the error-free command
Do you need thef*** tool?

If you are new to Linux, then thef*** tool can be pretty helpful to avoid unusual errors and practice the command line utility in Linux, while at the same time, you also get relative flags with that command which help you understand more about that particular command.

Leave a Reply