Know Thyself. Automatically Check Spelling and Grammar on Git Commits
I work very hard to write clean concise comments. I will toil over them, read, re-read, and re-read again. Even still - I am terrible at catching my own spelling and grammar mistakes. I've recently gotten sick of other people spending time in code review fixing my mistakes. I know that I can do better, and I want to be better. So like any good developer - lets automate it!
This is my setup using pre-commit
, aspell
, and languagetool
to automatically check spelling and grammar only in the changed lines of *.go
and *.md
files when committing code.
π Tools Used
pre-commit
: Git hook managerpipx install pre-commit
aspell
: Spell checkerbrew install aspell
languagetool
: Grammar checkerbrew install languagetool
These tools integrate cleanly into your local development workflow without linting untouched lines or scanning the entire codebase.
π Project Structure
Below is a simplified version of my repository layout:
your-repo/
βββ .git/
βββ .pre-commit-config.yaml
βββ hooks/
β βββ smart_spellcheck.sh
β βββ smart_grammar_check.sh
βββ main.go
βββ README.md
βοΈ .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: smart-spellcheck
name: Smart Spell Check (diff-only)
entry: .hooks/smart_spellcheck.sh
language: system
pass_filenames: false
- id: smart-grammar-check
name: Smart Grammar Check (diff-only)
entry: .hooks/smart_grammar_check.sh
language: system
pass_filenames: false
βοΈ hooks/smart_spellcheck.sh
#!/bin/bash
# Get changed lines from staged files
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(go|md)$')
HAS_ERRORS=0
for FILE in $FILES; do
if [ -f "$FILE" ]; then
# Extract added or changed lines (without + at beginning)
git diff --cached -U0 "$FILE" | \
grep '^+[^+]' | \
sed 's/^+//' > /tmp/spell_lines.txt
if [ -s /tmp/spell_lines.txt ]; then
echo "π Checking spelling in changed lines of $FILE..."
# Run aspell and collect misspelled words
MISSPELLED=$(cat /tmp/spell_lines.txt | aspell list | sort | uniq)
if [[ -n "$MISSPELLED" ]]; then
echo "β Misspelled words in $FILE:"
echo "$MISSPELLED"
HAS_ERRORS=1
fi
fi
fi
done
exit $HAS_ERRORS
π hooks/smart_grammar_check.sh
#!/bin/bash
if ! command -v languagetool >/dev/null 2>&1; then
echo "β οΈ LanguageTool CLI not found. Skipping grammar check."
exit 0
fi
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(go|md)$')
HAS_ERRORS=0
for FILE in $FILES; do
if [ -f "$FILE" ]; then
git diff --cached -U0 "$FILE" | \
grep '^+[^+]' | \
sed 's/^+//' > /tmp/grammar_lines.txt
if [ -s /tmp/grammar_lines.txt ]; then
echo "π Checking grammar in changed lines of $FILE..."
languagetool --language en-US /tmp/grammar_lines.txt || HAS_ERRORS=1
fi
fi
done
exit $HAS_ERRORS
β Usage
To set it up:
-
Install
pre-commit
:bash pip install pre-commit
-
Install the hooks:
bash pre-commit install
-
Manually test:
bash pre-commit run --all-files
β¨ Result
Now when I try to commit changes, these hooks run automatically and alert me if Iβve introduced spelling or grammar mistakes β without touching the rest of the file.
Itβs a simple setup, but itβs already helping keep my commits a little more polished. Hope it helps you too!