24
November
2022
15:11

Batch spell checking Markdown files using the free spell checker cSpell

24 November 2022 15:11

I recently received a letter with a comment in which a site visitor noted a large number of typos in articles.
How to automate the spell checking process - this article is about that.

Introduction

To correct errors, you must first find them. There are spell checking programs for this.
One such program is the free but powerful command line utility cSpell.

What is cSpell

cSpell is a so-called “spell checker” (tracing paper from the English word “spell checker”), a program for checking spelling (correct spelling of words) and identifying typos in the text.

Program website:https://cspell.org/, GitHub page:https://github.com/streetsidesoftware/cspell.

Unlike other programs, it works from the command line and allows you to recursively check directories, and also supports several languages ​​simultaneously (for multilingual texts). Also, the cSpell program knows the keywords of programming languages such as JavaScript, TypeScript, Python, PHP, C#, C++, LaTex, Go, HTML, CSS. Text checking speed is high - checking one 4 kB file on a Core2Duo 2 GHz processor takes 4 seconds.

Installing Snap

First you need to install an alternative package management program snap(see Description of snap installation on itfoss.com).

Since I have Linux Mint, which is close to Ubuntu, I installed snap using APT commands:

sudo apt update
sudo apt install snapd

Installing NodeJS and NPM

To run cSpell, you first need to install Node JS - a server-side, very powerful version of JavaScript for writing and executing programs. Node JS website showing latest version:https://github.com/nodejs/release.
There are several ways to install Node JS, I chose the installation option from snap.

Note: if you previously installed an older version of nodejs, first uninstall node:
sudo snap remove node

1) Install Node JS and NPM package management program

sudo snap install node --classic --channel=19

!!!For 2025, the current version is 22.14.0. The installation command below will install it.

sudo snap install node --classic --channel=22

The Node JS runtime environment and the NPM package manager program will be installed. Checking program versions:

npm version

2) Clear the NPM cache

npm cache clean --force

3) Then I removed traces of the previous installation of module "n" that were in the operating system:

sudo rm -R /usr/lib/node_modules/n

4) Installed the program "n" globally:

sudo npm install -g n

5) Updated the runtime with "n" node js:

sudo n stable

6) Installed the npm-check-updates utility (also known as ncu)

sudo npm install -g npm-check-updates

7) Launched a system update, including updating the npm package manager:

sudo npm update -g

8) Create a custom "package.json" file for your local project

cd ~
PATH="$PATH" 
npm init

We answer all the wizard’s questions by pressing Enter.

Installing the cSpell program

Installation of the spell checker cSpell is done with one command:

sudo npm install -g cspell

Installing and connecting a Russian language dictionary to cSpell

By default, cSpell only knows English. To “teach” him Russian, you need to install and connect a Russian language dictionary (source -https://www.npmjs.com/package/@cspell/dict-ru_ru):

Installing a Russian language dictionary in CSpell:

sudo npm install -g @cspell/dict-ru_ru

Add a link to the dictionary:

cspell link add @cspell/dict-ru_ru

Setting the document language using the cspell.json configuration file

In the folder from which cSpell will be launched (usually the user's home folder), create a text file with the name cspell.json(a detailed list of folders in which the program looks for the configuration file is listed in article on CSpell.org).

cd ~
nano cspell.json

Paste the following contents in json format and save the changes (Ctrl+O, Ctrl+X):

// cSpell Settings

{

"version": "0.2",
    "language": "en,ru",
    "words": [
        "vcfm",
        "webp",
        "Yaesu",
        "распиновка"
    ]
}

Updating cSpell dictionaries from the Internet

sudo npm update -g

ncu -g

[====================] 6/6 100%

All global packages are up-to-date :)

Test run of CSpell

echo "этороверка работы eckho test" > test.txt
npx cspell test.txt

CSpell will print misspelled words.

Examples of checking spelling of *.md files (Markdown format)

1) Using cSpell to check the spelling of Markdown files in the "test" directory

npx cspell test/*.md

---/test/item.md:29:119 - Unknown word (доставелен)
---/test/item.md:49:45 - Unknown word (микровыключатель)
---/test/item.md:64:26 - Unknown word (разъм)
---/test/item.md:78:1 - Unknown word (Цифроые)

Where 29 is the line number, 119 is the position of the erroneous word in the line.

2) Batch spell check of *.md files in subdirectories in the "blog" folder, writing the check results to a file errata.txt:

npx cspell blog/**/*.md > errata.txt

What's next?

You need to correct the syntax errors and typos found in each of the checked files in a text editor.
You can also explore additional features of the program on the home page on GitHub, as well as the "lint" command, which is used to check files not only in the markdown format, but also in others, for example, *.txt:

npx cspell lint --help

Source:Article "Spell checking your markdown blog posts with cspell"


Last change - 04/14/2025