Have you ever unknowingly executed a command with a typo in it? Well, you can simply hit the UP/DOWN arrow to bring up the command history, find the misspelled command, and edit the typo, and finally re-run it. This is what most of us will usually do! However, there are also other ways to fix typos in previously entered commands. In this tutorial, we will see all possible ways to correct errors in previous console commands in Linux and Unix.
Disclaimer:
This article contains a NSFW (Not Safe For Work), and an unusual word in some places. Even though I slightly masked that word, I couldn't avoid it altogether. Since there is no way to exclude that word, please accept my apologies in advance if this post made you uncomfortable and if I offended you in any way.
1. Correct errors in previous console commands using 'thef*ck' app
Thef*ck utility is used to correct errors and typos in the previously executed commands in Linux.
Did you run a command with a typo in a rush? Have you forgot to add sudo
in a command? No problem! This application will automatically show the corrected command(s) for the previously executed command!!
You can pick the correct command and hit ENTER to run it. No need to navigate through the command history to manually correct the typo in a command!
The developer of this application took the inspiration from a tweet and made this cool application. It is written in Python language. It is an opensource application and its code is publicly available in GitHub.
1.1. Installation
We can install it using either the PiP package manager or the distribution's default package manager.
Make sure you have installed PiP.
After installing PiP, run the following command to install it like below:
$ pip3 install --user thefuck
This will install the latest available version.
It is also packaged for many Linux and Unix distributions, so you can install it using your distribution's default package manager. Refer the project's GitHub page (given at the end) to know how to install it using the OS package manager.
1.2. Configuration
It is recommended to add the following line in your .bash_profile
, .bashrc
, .zshrc
or other startup script.
$ nano ~/.bashrc
Add the following line:
eval $(thefuck --alias fixthis)
Here, I used 'fixthis' as an alias. You can use whatever you want as an alias.
Press Ctrl+o
and ENTER to save the file and Ctrl+x
to close it.
Run the following command to update the changes:
$ source ~/.bashrc
1.3. Usage
The 'fixthis' works based on a set of built-in rules. The complete list of enabled rules can be found here. You can also create your own rules.
When running a mistyped command, It will attempt to match the previous command with a rule. If a match is found, a new command is created using the matched rule and executed.
Let us see a few examples.
I am going to update my Manjaro system using the following command as normal user:
$ pacman -Syu
Sample output:
error: you cannot perform this operation unless you are root.
As you can see, the above command displays a permission error. Because, I didn't add sudo
in-front of the command.
No need to add sudo
and re-run the command. All you have to do is just run the 'fixthis' command like below:
$ fixthis
Now, it will list valid command suggestion for the previously executed command.
If it is the one that you want to run, simply hit ENTER to execute it. If it is not, press UP/DOWN arrows to show other suggested commands.
In my case I go with the first one, which is the correct command, as shown in the output below.
sudo pacman -Syu [enter/↑/↓/ctrl+c]
See? If the previous command is failed because of permission error, the 'fixthis' app will prepend the command with sudo
and run it again. Cool, right? To abort running the command, press ctrl+c.
Similarly, if you executed a misspelled command, it will correct the spelling mistake and run the command again.
$ pithon
Sample output:
bash: pithon: command not found
$ fixthis python3 [enter/↑/↓/ctrl+c]
If you want to fix commands recursively until succeeding, you can use the -r
option:
$ fixthis -r
See the below official screen-cast made by its developer for more examples.
Despite being super useful to quickly fix typos, and errors in Linux commands, this app has the sensitive and inappropriate name, so you may don't want to use it in some situations.
If you feel uncomfortable while running this command in such situations, you can simply create an alias (E.g. please, fixthis) as shown in the "Configuration" section above.
2. Correct errors in commands using Fix
The Fix is another cool utility that helps us to quickly correct errors or typos in a command. Even though the Fix does the same as thef*ck, it slightly differs.
Thef*ck is strong at syntax whereas the Fix is better when the semantic aspect is important. Thef*ck can't tell you what package to install when executing a script doesn't work.
The Fix utility is intended to be used when you don't know how to fix it yourself. It suggests a few solutions for the mistyped or misspelled commands.
It uses OpenAI's Codex AI to suggest all possible solutions to correct the errors in commands. All you need to do is just prepend the command with 'fix', and the AI will suggest optimal solutions.
2.1. Install Fix in Linux
Installing Fix is easy!
1. First, you need to get access to OpenAI's Codex API.
2. And git clone the repository:
$ git clone https://github.com/tom-doerr/fix.git
It will clone the contents of the project's github repository in a local folder named 'fix
'.
3. cd into the 'fix' directory:
$ cd fix
4. Add main.py
to your $PATH:
$ cp $PWD/main.py ~/.local/bin/fix
2.2. Usage
To fix a mistyped command, simply add 'fix' in-front of the command like below:
$ fix mkdir abc/def
To fix the last command, simply do:
$ fix !!
The AI generates mostly wrong solutions, but enough of the generated solutions are actually working for it to be useful.
Here is the visual demo of the Fix application.
Suggested read:
3. Correct misspelled Bash commands with Caret symbol
This trick will only work on BASH.
This trick is very simple. We simply replace the mistyped word with correct word using Caret (^) symbol.
Let us say you want to create a directory with 'mkdir
' command. But, you missed the 'i
' in the command and entered 'mkdr
' instead like below.
$ mkdr ostechnix bash: mkdr: command not found
To fix the typo in above command, we can simply replace 'dr
' with 'dir
' with Caret symbol like below:
$ ^dr^dir^
Now the Bash will interpret this as 'mkdir
' and run it and display the output as shown below.
mkdir ostechnix
4. Correct typos when using cd command
I often tried to type fast in the console and end up with typos when cd
into a directory.
$ cd Pctures bash: cd: Pctures: No such file or directory
I will miss a word or two when typing the directory name. If you're anything like me, here is how to automatically correct typos when using cd
command.
To fix this nuisance, edit ~/.bashrc
file:
$ nano ~/.bashrc
Add the following line at the end:
shopt -s cdspell
Press Ctrl+o
and ENTER to save the file and Ctrl+x
to close it.
Run the following command to update the changes:
$ source ~/.bashrc
From now on, your shell automatically recognizes the correct directory path even if a made a typo.
This trick has some drawbacks. The case and order is important.
For example, if you use 'pctures' instead of 'Pctures', it won't recognize the path. Here, I have used lowercase 'p' for the directory 'Pictures'.
$ cd pctures bash: cd: pctures: No such file or directory
Also if you use 'Pctrues' instead of 'Pctures', it still won't recognize the directory path even if you use the correct case and complains that there is no such file or directory.
$ cd Pctrues bash: cd: Pctrues: No such file or directory
Here, I have changed the letter's order of 'Pictures' directory.
Also, if there were more than one letters missing in the directory path, it won't work as well.
Please note that this tip is only for 'cd
' command. Also, this tip is only for Bash. In Fish and Zsh, this feature is built-in.
Bonus - Suggest package installation when running a missing command
You should have noticed that when you run an unavailable command, you'll get an error message like below:
bash: ....: command not found
If you know which package provides the missing command, you can simply install that package and re-run the command again. However if you don't know it, you may need a little help.
To mitigate this issue, there is package called 'command-not-found'. This will suggest a list of packages to install to use the unavailable command.
It will automatically search the official repositories, when entering an unrecognized or unavailable command. For details, refer the following link.
Conclusion
There you go! These are a few ways to deal with command line typos and errors in Linux and Unix systems. You can pick the one that suits you or use combination of two or more. For example, I use both 'Fix' and 'thef*ck' from time to time to fix errors and typos in console commands.
Resources: