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.
Installation
You need python version 2.7+ or 3.3+ along with pip and python-dev. Installation is super easy:
macoS installation
Open the terminal app and type the following commands. First, install Homebrew on macOS and then type the following brew command:
$ brew install thefuck
Sample outputs:
==> Installing dependencies for thefuck: sqlite ==> Installing thefuck dependency: sqlite ==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.20.1.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring sqlite-3.20.1.sierra.bottle.tar.gz ==> Caveats This formula is keg-only, which means it was not symlinked into /usr/local, because macOS provides an older sqlite3. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/sqlite/lib CPPFLAGS: -I/usr/local/opt/sqlite/include For pkg-config to find this software you may need to set: PKG_CONFIG_PATH: /usr/local/opt/sqlite/lib/pkgconfig ==> Summary 🍺 /usr/local/Cellar/sqlite/3.20.1: 11 files, 3.0MB ==> Installing thefuck ==> Downloading https://homebrew.bintray.com/bottles/thefuck-3.23.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring thefuck-3.23.sierra.bottle.tar.gz ==> Caveats Add the following to your .bash_profile, .bashrc or .zshrc: eval "$(thefuck --alias)" For other shells, check https://github.com/nvbn/thefuck/wiki/Shell-aliases ==> Summary 🍺 /usr/local/Cellar/thefuck/3.23: 711 files, 6.2MB
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
Linux/Unix installation
On other system try:
$ pip install thefuck
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
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:
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
🐧 4 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
When forgetting to sudo a command, a quick (and more polite) solution can be adding something like the following alias.
alias please='sudo $(history 2| head -n1 | sed "s/^ [0-9]\+ //")'
Next time you forget to type sudo, just say please ;)
Yes, and it is more kids friendly too.
echo 'alias cat="ccat "' >> ~/.bash_aliases
I use ~/.bashrc so:
echo 'alias cat="ccat "' >> ~/.bashrc
This is my favourite linux tool. It helps in so many ways.