A smart, learning-based tool for your Zsh shell that automatically detects and suggests corrections for failed commands. Never mistype a git command again!
- Seamless Correction: Automatically hooks into your shell to detect failed commands.
- Corrects Command and Argument Typos: Fixes both
gti statusandgit sttus. - Interactive Learning: When it can't find a match, it prompts you to provide the correct command and learns it for next time.
- Fuzzy Matching: Uses
rapidfuzzto intelligently find the closest correction for your typos. - User-Controlled: Provides an interactive
(a)ccept, (r)eject, or (c)orrect?prompt for all suggestions. - Configurable: Use a simple JSON file to manage learned patterns and an ignore list to prevent the tool from running on specific commands.
This tool uses Zsh's command_not_found_handler to intercept failed commands.
- When you type a command that Zsh cannot find, the
command_not_found_handleris triggered. - This handler passes the failed command to a Python script.
- The Python script uses fuzzy matching to find the best correction from a list of previously successful commands stored in
autocorrect_aliases.json. - If a good suggestion is found, it is presented to you with an interactive
(a)ccept, (r)eject, or (c)orrect?prompt. - If you choose to
(c)orrect, you can enter the correct command, and if successful, it's automatically saved toautocorrect_aliases.jsonfor future use.
This tool is designed for Zsh.
-
Clone this repository:
git clone <repository_url> cd auto-correct
-
Set up the Python environment:
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt -
Edit your
.zshrcfile: Add the following line to your~/.zshrcfile:# Load the autocorrect tool that defines the `command_not_found_handler` source /path/to/your/clone/of/auto-correct/autocorrect.sh
Make sure to replace
/path/to/your/clone/of/with the actual path to where you cloned the repository. -
Restart your shell for the changes to take effect.
This file is the brain of the tool. It stores all the successful command patterns the tool has learned. You can manually edit it if you wish.
Example:
[
{
"command": "git",
"patterns": [
["status"],
["add", "-A"]
]
},
{
"command": "docker",
"patterns": [
["ps", "-a"]
]
}
]To prevent the tool from trying to correct commands that are expected to fail (like grep), you can add them to the ignore list located inside the autocorrect.sh script.
This will be moved to a more user-friendly configuration file in a future update.
- Shell: Zsh
- Core Logic: Python 3
- Fuzzy Matching:
rapidfuzz(a fast implementation of Levenshtein Distance) - Hooks: Zsh's
command_not_found_handler