[repost]How to corrects your previous Linux/Unix command with thefuck app
Let us face it we all make mistakes at the command line. Typos? Wrong options? Forget to add sudo? The list goes on. There is a neat little app called thefuck which corrects your previous Linux and Unix command line mistakes.
1. Installation
You need python version 2.7+ or 3.3+ along with pip and python-dev. Installation is super easy:
1-1. macoS installation
Open the terminal app and type the following command:
$ brew install thefuck
Sample outputs:
1-2. Debian/Ubuntu Linux installation
Use the apt command/apt-get command as follows:
$ sudo apt-get update
$ sudo apt-get install python3-dev python3-pip
$ sudo pip3 install thefuck
1-3. Linux/Unix installation
On other system try:
$ pip install thefuck
2. Configuration
Edit your shell config file such as ~/.bash_profile/~/.bashrc/~/.zshrc and append the following config:
echo 'eval $(thefuck --alias)' >> ~/.bashrc
Reload the changes or restart the terminal app/shell:
$ source ~/.bashrc
3. How do I use it?
Simple. Whenever you made a mistake or typo, just type the fuck command. For example:
$ apt-get install nginx
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?
To add sudo before the apt-get command:
$ fuck
sudo apt-get install nginx [enter/↑/↓/ctrl+c]
[sudo] password for vivek:
Reading package lists... Done
...
.....
..
Here are some more commands from my macos powered laptop:

For more info see source code at: Magnificent app which corrects your previous console command.
4. A note about using bash !!
If you can’t install 3rd party tool or do not want to use this tool. Try !! syntax to run the entire previous command:
$ sudo $$
For example:
$ apt-get install vim
# Oh nooo, let us called above command with sudo
$ sudo !!
Let us say you run the following command instead of ls -l bar
$ ls -l foo
To correct it run:
^foo^bar
OR
!:s/foo/bar
Use it with vim if you want to write file when forgot to add sudo before and getting permission errors:
:w !sudo tee %
See bash man page for more info:
$ man bash
[source] How to corrects your previous Linux/Unix command with thefuck app