ZSH Gem #4: Spell checking and auto correction
It often happens that you type a command, hit enter and then notice that you spelled the command incorrectly and your shell tells you something like:
zsh: command not found: wrongcommand
That is not a big problem, but especially when your command is very long, it is sometimes tedious to jump to the first word and correct it. You might configure your HOME key to do that, but sometimes this is not possible. It would be very convenient if the shell would not just say that you misspelled the command but also give you some alternative you'd only have to confirm with a keystroke. Well, ZSH can do that.
To enable this feature you have to set the ZSH option CORRECT
:
setopt correct
Like all option definitions, this should go in your .zshrc
if you need it for any new shell instance.
Now when you misspell a command, ZSH asks you whether you meant what you typed (this is sometimes very important, see below), whether you want to edit or abort the command or whether you meant another command which is spelled very similarly. As an example: I want to have a look at Gentoo's Portage repository with the command eix
but accidentally I type exi
. With CORRECT
being set, ZSH asks me:
% exi<Return> zsh: correct 'exi' to 'eix' [nyae]?
To choose what to do I just have to type one of the characters listed in the square brackets (n for “thanks no, I know what I typed”, y for “yes, please use the correction”, a for “don't do anything, just abort” and e for “I typed bullshit, let me edit that”).
That is already very convenient, but not very pretty. So let's make this prompt a bit more beautiful. We can do that by modifying the variable $SPROMPT
. To get a more verbose and meaningful prompt we could do the following:
export SPROMPT="Correct %R to %r? (Yes, No, Abort, Edit) "
The placeholders %R
and %r
are expanded to the wrong command and its correction. Now when you type a wrong command, you'll get the following prompt:
% exi<Return> Correct exi to eix? (Yes, No, Abort, Edit)
That looks a lot better. But what about colors? Not problem at all.
autoload -U colors && colors
export SPROMPT="Correct $fg[red]%R$reset_color to $fg[green]%r?$reset_color (Yes, No, Abort, Edit) "
That gives us the same prompt as before but with the incorrectly spelled command in red and the suggestion in green.
That's about it for the correction but let me mention one more thing: the auto correct function does not always work perfectly. Sometimes you have commands which ZSH always wants to correct although they are spelled correctly. But of course, there is a solution for this. You can disable the spell checker for individual commands with the build-in command nocorrect
. So to permanently disable auto correction for the command foobar
put the following alias definition in your .zshrc
:
alias foobar="nocorrect foobar"
When you open a new shell or re-source your current shell instance, there shouldn't be auto correction for foobar
anymore.
Read more about zsh auto correct:
A little nice tutorial on how to setup spell check and auto-correction on #zsh #terminal https://t.co/o9J0tmNwAQ