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.

ADVERTISEMENTS

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 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:

Gif. 01: fuck command in action

For more info see source code at: Magnificent app which corrects your previous console command.

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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • ioannis Aug 30, 2017 @ 8:47

    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 ;)

  • TJ Aug 30, 2017 @ 18:22

    echo 'alias cat="ccat "' >> ~/.bash_aliases
    I use ~/.bashrc so:
    echo 'alias cat="ccat "' >> ~/.bashrc

  • Johnny Nov 29, 2017 @ 13:51

    This is my favourite linux tool. It helps in so many ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.