Zsh’s spelling correction, when enabled, doesn’t just suggest corrections; it actively modifies your command line before you even hit enter, often in ways that feel like magic.
Let’s see it in action. Imagine you’re trying to navigate to a directory named ~/development/projects/my_awesome_app, but you’re a bit rusty on the spelling.
cd ~/devlopment/projecs/my_awsome_app
You type this out, perhaps with a few more typos. You hit the right arrow key (or press Ctrl+E to move to the end of the line) and then Enter. Instead of an error, Zsh might silently correct it and execute:
cd ~/development/projects/my_awesome_app
This happened because Zsh, by default, has a spell-checking mechanism that kicks in when you try to execute a command that doesn’t exist. It tries to find the "closest" valid command or path.
The core of this feature is the setopt CORRECT or setopt CORRECT_ALL options. CORRECT applies to commands and builtins, while CORRECT_ALL extends this to filenames and arguments. When Zsh can’t find a command or file you’ve typed, it looks for a plausible alternative. This "plausibility" is determined by a scoring algorithm that considers factors like character transposition (e.g., "teh" vs. "the"), insertion, deletion, and substitution.
To enable this, you’d typically add these lines to your ~/.zshrc file:
setopt CORRECT
setopt CORRECT_ALL
After saving and sourcing your ~/.zshrc (e.g., source ~/.zshrc), the spell correction will be active. You can test it by typing a clearly misspelled command, like lsst instead of ls or gti status instead of git status. Zsh will prompt you:
zsh: correct 'lsst' to 'ls' [enter/y/n/e]?
If you press Enter or y, it will execute the corrected command. n will execute the command as typed, and e will allow you to edit the line. CORRECT_ALL makes this happen for arguments and paths too.
The magic behind the scenes involves Zsh’s internal fuzzy matching capabilities. When a command or path lookup fails, Zsh iterates through its known command list and filesystem to find candidates. It uses a distance metric, often a variation of the Levenshtein distance, to quantify how "far" one string is from another. A small distance means the strings are very similar, suggesting a typo. The setopt flags tell Zsh to engage this search and prompt for confirmation.
If you want to see what Zsh is considering as a correction without executing it, you can use setopt NO_UNMATCHED_EXIT_CODES and setopt NO_EXIT_ON_COMMAND_ERROR. Then, when you type a bad command, Zsh will simply report the error without exiting, and you can manually type echo $? to see the exit code, or which <misspelled_command> to see if Zsh can find a correction. However, the primary mechanism is the interactive prompt driven by CORRECT and CORRECT_ALL.
A subtle, but powerful, aspect of Zsh’s spelling correction is that it doesn’t just correct the command itself, but also its arguments if CORRECT_ALL is set. This means if you type cat myfil.txt and myfil.txt doesn’t exist but myfile.txt does, Zsh might offer to correct myfil.txt to myfile.txt. This can be incredibly convenient for navigating large, complex directory structures or dealing with long, easily mistyped filenames.
The next logical step after mastering basic spelling correction is exploring Zsh’s autocompletion and its integration with spell checking, particularly how you can customize the completion system to be more intelligent about suggested corrections based on your command history.