Using a spelling checker on a git pre-commit hook

English is not my native language, and on top of that I am really bad at grammar and spelling. But when I combine my Linux powers with some creativity, I can make my posts more readable for my visitors.

To keep this blog readable and tidy without any spelling errors I use the spellcheck feature of GNU Aspell combined with the git pre-commit hook to check my markdown files.

The pre-commit hook is a bash script I wrote with the help of the Aspell package to check the correct spelling before committing a markdown file into my local git branch. It will not commit if it finds wrong spelled or unknown words. It then gives you an overview of the wrongly spelled words and the commands you can use to correct them in GNU Aspell.

When all words are fixed and known in Aspell, the commit will succeed. This method will however not save you from any grammatical errors.


I am working on MacOS Sierra with brew installed but the installation may also work on a Linux setup. This article will not explain how to use git. For this script you will need Aspell, which you can install by executing the following command in your terminal:

For MacOS:

brew install aspell

For Ubuntu & Debian:

apt install aspell

For CentOS:

yum install aspell

Setup the script

Place the bash script below in your .git/hooks/pre-commit file (If file does not exists, create it). Don't forget to make the script executable by your user:

chmod +x .git/hooks/pre-commit 

Next, copy and paste the script and save the file in .git/hooks/pre-commit

#!/usr/bin/env bash

# This script is used to check files with the .md extension (markdown) for spelling errors.
# It will run each .md file through aspell and returns an exit code other then 0 when it matches.

# Where are the markdown files located?

# Set some fancy colors to indicate errors and wrongly spelled words.
NC='\033[0m' # No Color

# Start checking the current directory for md files
for file in $(find $SEARCH_DIR -type f -name "*.md");
        # Pass each .md file through aspell.
        do output=$(cat $file | aspell --lang=en list);
        if [[ $? != 0 ]]; then
                echo -e "${RED}Error found in output${NC}, cannot continue." 
                echo -e "Please check manually for aspell -c $file?"
                exit 1
        elif [[ $output ]]; then
                echo -e "-> ${RED}Spelling errors found${NC} <-"
                echo -e "${YELLOW}$output${NC}" |sort -u
                echo "Please check with: aspell -c $file"

# Matched in aspell
if [[ "$bad" == "yes" ]]; then
        exit 1

# No match in aspell
if [[ "$good" == "yes" ]]; then
        echo -e "Spelling check is ${RED}OK${NC}, good to go."
        exit 0

You can find the latest version of this script on GitHub.


The next time you want to commit your files to Git, it will show your misspelled words if any before the commit is successful:

$ git commit content/ -m "Fingers crossed!"
-> Spelling errors found <-
Please check with: aspell -c content/

As you can see, there are some spelling mistakes and there are some words aspell does not recognize, like Unix commands. When all the errors are fixed and unknown words are added to the custom dictionary, it will commit successfully.

If you have any questions, leave them in the comments below.

Happy spellchecking :)


comments powered by Disqus