How to corrects your previous Linux/Unix command with thefuck app

Posted on in Categories Command Line Hacks, Howto last updated August 29, 2017

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

3 comment

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

Leave a Comment