The fuck
So I am notorious as a clumsy typist, especially in instances like demos or on stage at conferences. I’ve also been known to comment indelicately about my mistakes in front of large audiences. So when I stumbled across The Fuck it was like I had found my keyboard people.
The Fuck is a self-described “magnificent app”, inspired by a tweet from @liamosaur, that automatically corrects previous, invalid, console commands. Like so:
|
|
You can see here we’ve run apt
without sudo
, which sensibly Ubuntu is like “nah dude”. We then run The Fuck, using its stock alias fuck
, and it will correct our previous shell command, by prefixing sudo
, and offer to re-run it. The Fuck works out how to correct commands based on rules (more on this later) and can correct a wide variety of mistakes including common mistypes of Git commands, binaries, flags, and arguments.
Installing The Fuck
The Fuck requires Python (3.4+) and can be installed via pip
.
|
|
Or on OS X
|
|
Configuring The Fuck
You then need to include it in your shell’s startup, for example add the following to .bashrc
(or profile, or…):
|
|
Alternatively, if you have Fish then you can add:
|
|
Other shells are also documented in the wiki.
The --alias
option allows you to specify an alias other than the default fuck
. In my Fish shell I alias fuck
to fu
, a shorter and more poignant reminder of my mistype. This also allows folks who don’t like the idea of an expletive on the command line rename it to something more palatable.
|
|
After adding this to your shell, you’ll need to run or source the command or start a new shell to enable The Fuck.
You can also skip the confirmation step, if you’re feeling brave (I am generally not) by using the --yeah
(or -y
) flag.
|
|
The Fuck is very configurable. You can create (or edit) a configuration file at: $XDG_CONFIG_HOME/thefuck/settings.py
(generally ~/.config
) and control The Fuck’s settings.
Adding you own rules
You can also add your own rules. You create rules in the ~/.config/thefuck/rules
directory in a file suffixed with .py
. For example, ~/.config/thefuck/rules/my_awesome_rules.py
. Rules are defined as Python functions and must contain two functions: match
, which matches specific output from a command, and get_new_command
, which returns the restructured (fixed) command.
There are a variety of modifiers and other configuration you can also provide to tweak the rule too. You can also import a series of util functions and application- or OS-specific helpers that make writing a rule easier.
Here’s a rule that recognizes when you’ve mistyped a Microsoft Azure az
binary sub-command.
|
|
Our rule is Python, so we can import Python libraries, here the regex library re
. We’ve also imported two utilities: for_app
, which matches our rule only against a specific application, and replace_argument
, which is a util for replacing a command line argument.
We then specify some regular expressions, one to match when an invalid choice is made and the second to match the correct choice which is specified in the incorrect command’s output.
We then use the for_app
util to match only on the az
binary.
Our match
function ensures The Fuck is only triggered when you type in an incorrect az
sub-command by matching text in the command’s output. The command itself is contained in the command
variable, which The Fuck will pass to the rule, and the output is contained in command.output
command.script
variable contains the command actually executed.The get_new_command
function finds our mistake, finds the corrected command(s), and returns them. When you run The Fuck you’ll now be prompted with an updated command line, assuming it matched a correct option. If more than one option matched, you can use the up and down arrows to select the specific command returned by the suggested list of corrections.
Let’s see it in action.
|
|
And then running fuck
:
|
|
Woot! Correct command returned. Whilst The Fuck is perhaps named a little oddly, it’s really useful for those of us who typo our way through the command line.