How To Appropriate Errors And Typos In Earlier Console Instructions In Linux

Get real time updates directly on you device, subscribe now.

Have you ever ever unknowingly executed a command with a typo in it? Effectively, you possibly can merely hit the UP/DOWN arrow to convey up the command historical past, discover the misspelled command, and edit the typo, and at last re-run it. That is what most of us will normally do! Nevertheless, there are additionally different methods to repair typos in beforehand entered instructions. On this tutorial, we are going to see all potential methods to appropriate errors in earlier console instructions in Linux and Unix.

Disclaimer:

This text incorporates a NSFW (Not Protected For Work), and an uncommon phrase in some locations. Though I barely masked that phrase, I could not keep away from it altogether. Since there is no such thing as a option to exclude that phrase, please settle for my apologies prematurely if this submit made you uncomfortable and if I offended you in any means.

1. Appropriate errors in earlier console instructions utilizing ‘thef*ck’ app

Thef*ck utility is used to appropriate errors and typos within the beforehand executed instructions in Linux.

Did you run a command with a typo in a rush? Have you ever forgot so as to add sudo in a command? No downside! This utility will mechanically present the corrected command(s) for the beforehand executed command!!

You may decide the right command and hit ENTER to run it. No must navigate by the command historical past to manually appropriate the typo in a command!

The developer of this utility took the inspiration from a tweet and made this cool utility. It’s written in Python language. It’s an opensource utility and its code is publicly accessible in GitHub.

1.1. Set up

We are able to set up it utilizing both the PiP bundle supervisor or the distribution’s default bundle supervisor.

Be sure you have put in PiP.

After putting in PiP, run the next command to put in it like under:

$ pip3 set up –user thefuck

This may set up the newest accessible model.

It is usually packaged for a lot of Linux and Unix distributions, so you possibly can set up it utilizing your distribution’s default bundle supervisor. Refer the mission’s GitHub web page (given on the finish) to know set up it utilizing the OS bundle supervisor.

1.2. Configuration

It’s endorsed so as to add the next line in your .bash_profile, .bashrc, .zshrc or different startup script.

$ nano ~/.bashrc

Add the next line:

eval $(thefuck –alias fixthis)

Right here, I used ‘fixthis’ as an alias. You should utilize no matter you need as an alias.

Press Ctrl+o and ENTER to avoid wasting the file and Ctrl+x to shut it.

Run the next command to replace the adjustments:

$ supply ~/.bashrc

1.3. Utilization

The ‘fixthis’ works primarily based on a set of built-in guidelines. The entire checklist of enabled guidelines may be discovered right here. You can even create your personal guidelines.

When operating a mistyped command, It’ll try to match the earlier command with a rule. If a match is discovered, a brand new command is created utilizing the matched rule and executed.

Allow us to see a number of examples.

I’m going to replace my Manjaro system utilizing the next command as regular consumer:

$ pacman -Syu

Pattern output:

error: you can not carry out this operation except you’re root.

As you possibly can see, the above command shows a permission error. As a result of, I did not add sudo in-front of the command.

No want so as to add sudo and re-run the command. All you need to do is simply run the ‘fixthis’ command like under:

$ fixthis

Now, it’ll checklist legitimate command suggestion for the beforehand executed command.

If it’s the one that you just need to run, merely hit ENTER to execute it. If it isn’t, press UP/DOWN arrows to point out different steered instructions.

In my case I’m going with the primary one, which is the right command, as proven within the output under.

sudo pacman -Syu [enter/↑/↓/ctrl+c] Correct errors in previous console commands with fixthis appAppropriate errors in earlier console instructions with fixthis app

See? If the earlier command is failed due to permission error, the ‘fixthis’ app will prepend the command with sudo and run it once more. Cool, proper? To abort operating the command, press ctrl+c.

Equally, for those who executed a misspelled command, it’ll appropriate the spelling mistake and run the command once more.

$ pithon

Pattern output:

bash: pithon: command not discovered $ fixthis
python3 [enter/↑/↓/ctrl+c]

If you wish to repair instructions recursively till succeeding, you should utilize the -r possibility:

$ fixthis -r

See the under official screen-cast made by its developer for extra examples.

Correct errors and typos in previous console commands using 'fixthis' appAppropriate errors and typos in earlier console instructions utilizing ‘fixthis’ app

Regardless of being tremendous helpful to rapidly repair typos, and errors in Linux instructions, this app has the delicate and inappropriate title, so it’s possible you’ll do not need to use it in some conditions.

In the event you really feel uncomfortable whereas operating this command in such conditions, you possibly can merely create an alias (E.g. please, fixthis) as proven within the “Configuration” part above.

2. Appropriate errors in instructions utilizing Repair

The Repair is one other cool utility that helps us to rapidly appropriate errors or typos in a command. Though the Repair does the identical as thef*ck, it barely differs.

Thef*ck is powerful at syntax whereas the Repair is healthier when the semantic facet is vital. Thef*ck cannot let you know what bundle to put in when executing a script does not work.

The Repair utility is meant for use when you do not know repair it your self. It suggests a number of options for the mistyped or misspelled instructions.

It makes use of OpenAI’s Codex AI to counsel all potential options to appropriate the errors in instructions. All it is advisable to do is simply prepend the command with ‘repair’, and the AI will counsel optimum options.

2.1. Set up Repair in Linux

Putting in Repair is straightforward!

1. First, it is advisable to get entry to OpenAI’s Codex API.

2. And git clone the repository:

$ git clone https://github.com/tom-doerr/repair.git

It’ll clone the contents of the mission’s github repository in a neighborhood folder named ‘repair’.

3. cd into the ‘repair’ listing:

$ cd repair

4. Add principal.py to your $PATH:

$ cp $PWD/principal.py ~/.native/bin/repair

2.2. Utilization

To repair a mistyped command, merely add ‘repair’ in-front of the command like under:

$ repair mkdir abc/def

To repair the final command, merely do:

$ repair !!

The AI generates principally unsuitable options, however sufficient of the generated options are literally working for it to be helpful.

Right here is the visible demo of the Repair utility.

Correct errors in commands using Fix in LinuxAppropriate errors in instructions utilizing Repair in Linux

3. Appropriate misspelled Bash instructions with Caret image

This trick will solely work on BASH.

This trick could be very easy. We merely exchange the mistyped phrase with appropriate phrase utilizing Caret (^) image.

Allow us to say you need to create a listing with ‘mkdir’ command. However, you missed the ‘i’ within the command and entered ‘mkdr’ as an alternative like under.

$ mkdr ostechnix
bash: mkdr: command not discovered

To repair the typo in above command, we will merely exchange ‘dr’ with ‘dir’ with Caret image like under:

$ ^dr^dir^

Now the Bash will interpret this as ‘mkdir’ and run it and show the output as proven under.

mkdir ostechnix Correct misspelled Bash commands with Caret symbolAppropriate misspelled Bash instructions with Caret image

4. Appropriate typos when utilizing cd command

I usually tried to sort quick within the console and find yourself with typos when cd right into a listing.

$ cd Pctures
bash: cd: Pctures: No such file or listing

I’ll miss a phrase or two when typing the listing title. In the event you’re something like me, right here is mechanically appropriate typos when utilizing cd command.

To repair this nuisance, edit ~/.bashrc file:

$ nano ~/.bashrc

Add the next line on the finish:

shopt -s cdspell

Press Ctrl+o and ENTER to avoid wasting the file and Ctrl+x to shut it.

Run the next command to replace the adjustments:

$ supply ~/.bashrc

To any extent further, your shell mechanically acknowledges the right listing path even when a made a typo.

Correct typos when using cd commandAppropriate typos when utilizing cd command

This trick has some drawbacks. The case and order is vital.

For instance, for those who use ‘pctures’ as an alternative of ‘Pctures’, it will not acknowledge the trail. Right here, I’ve used lowercase ‘p’ for the listing ‘Footage’.

$ cd pctures
bash: cd: pctures: No such file or listing

Additionally for those who use ‘Pctrues’ as an alternative of ‘Pctures’, it nonetheless will not acknowledge the listing path even for those who use the right case and complains that there is no such thing as a such file or listing.

$ cd Pctrues
bash: cd: Pctrues: No such file or listing

Right here, I’ve modified the letter’s order of ‘Footage’ listing.

Additionally, if there have been a couple of letters lacking within the listing path, it will not work as nicely.

Please notice that this tip is just for ‘cd’ command. Additionally, this tip is just for Bash. In Fish and Zsh, this characteristic is built-in.

Bonus – Counsel bundle set up when operating a lacking command

It’s best to have seen that once you run an unavailable command, you will get an error message like under:

bash: ….: command not discovered

If you already know which bundle offers the lacking command, you possibly can merely set up that bundle and re-run the command once more. Nevertheless if you do not know it, it’s possible you’ll want somewhat assist.

To mitigate this problem, there’s bundle referred to as ‘command-not-found‘. This may counsel a listing of packages to put in to make use of the unavailable command.

It’ll mechanically search the official repositories, when coming into an unrecognized or unavailable command. For particulars, refer the next hyperlink.

Conclusion

There you go! These are a number of methods to cope with command line typos and errors in Linux and Unix programs. You may decide the one which fits you or use mixture of two or extra. For instance, I exploit each ‘Repair’ and ‘thef*ck’ sometimes to repair errors and typos in console instructions.

Assets:

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More