Spell Checking
Installation
See: Installation
Basic Usage
Example: recursively spell check all JavaScript files in src
JavaScript files
cspell "src/**/*.js"
# or
cspell lint "src/**/*.js"
Check everything
cspell .
# or
cspell "**"
Adding CSpell to an existing project
In the steps below we will create a cspell configuration file and setup a single custom dictionary for the project.
Steps:
1. Create a configuration file.
CSpell can use JSON, Yaml, and JavaScript files for configuration. It automatically searches for one of the following:
package.json.cspell.jsoncspell.json.cSpell.jsoncSpell.json.cspell.jsonccspell.jsonc.cspell.yamlcspell.yaml.cspell.ymlcspell.yml.cspell.config.jsoncspell.config.json.cspell.config.jsonccspell.config.jsonc.cspell.config.yamlcspell.config.yaml.cspell.config.ymlcspell.config.yml.cspell.config.mjscspell.config.mjs.cspell.config.cjscspell.config.cjs.cspell.config.jscspell.config.js.cspell.config.tomlcspell.config.toml.config/.cspell.json.config/cspell.json.config/.cSpell.json.config/cSpell.json.config/.cspell.jsonc.config/cspell.jsonc.config/cspell.yaml.config/cspell.yml.config/.cspell.config.json.config/cspell.config.json.config/.cspell.config.jsonc.config/cspell.config.jsonc.config/.cspell.config.yaml.config/cspell.config.yaml.config/.cspell.config.yml.config/cspell.config.yml.config/.cspell.config.mjs.config/cspell.config.mjs.config/.cspell.config.cjs.config/cspell.config.cjs.config/.cspell.config.js.config/cspell.config.jsconfig/.cspell.config.tomlconfig/cspell.config.toml.vscode/.cspell.json.vscode/cSpell.json.vscode/cspell.json
For now choose to use either JSON or Yaml. Below are examples of each that include a custom dictionary definition. Both of them are equivalent. If you have both, CSpell will look for the .json file first.
- cspell.json
- cspell.config.yaml
- cspell.config.mjs
- cspell.config.cjs
- package.json
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"version": "0.2",
"dictionaryDefinitions": [
{
"name": "project-words",
"path": "./project-words.txt",
"addWords": true
}
],
"dictionaries": ["project-words"],
"ignorePaths": ["node_modules", "/project-words.txt"]
}
---
$schema: https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json
version: '0.2'
dictionaryDefinitions:
- name: project-words
path: './project-words.txt'
addWords: true
dictionaries:
- project-words
ignorePaths:
- 'node_modules'
- '/project-words.txt'
import { defineConfig } from 'cspell';
export default defineConfig({
version: '0.2',
dictionaryDefinitions: [
{
name: 'project-words',
path: './project-words.txt',
addWords: true,
},
],
dictionaries: ['project-words'],
ignorePaths: ['node_modules', '/project-words.txt'],
});
'use strict';
const { defineConfig } = require('@cspell/cspell-types');
module.exports = defineConfig({
version: '0.2',
dictionaryDefinitions: [
{
name: 'project-words',
path: './project-words.txt',
addWords: true,
}
],
dictionaries: ['project-words'],
ignorePaths: ['node_modules', '/project-words.txt'],
});
{
...
"cspell": {
"version": "0.2",
"dictionaryDefinitions": [
{
"name": "project-words",
"path": "./project-words.txt",
"addWords": true
}
],
"dictionaries": ["project-words"],
"ignorePaths": ["node_modules", "/project-words.txt"]
}
}
These configuration files do three things:
- Define the custom dictionary
project-words. - Tell the spell checker to use the custom dictionary.
- Tell the spell checker to ignore any files inside of
node_modulesand the fileproject-words.txt.
2. Add words to the project dictionary
It might take a few iterations to get fully setup, but the process in the same.
Steps:
-
Create the dictionary file
touch project-words.txt -
Choose a set of files to start with, like all Markdown files,
**/*.mdand run the spell checker.cspell "**/*.md" -
Look for any directories that need to be ignored and add them to
ignorePaths. Example:"bin"- to ignore any directory / file calledbin."translations/**"- to ignore all files under thetranslationsdirectory."packages/*/dist"- to ignore thedistdirectory in each package.
Once you have finished identifying directories and files to be ignored, it is now time to add words to the custom dictionary.
-
Have CSpell populate it with the words from your project.
echo "# New Words" >> project-words.txt
cspell --words-only --unique "**/*.md" | sort --ignore-case >> project-words.txtThis will append all new issues to the end of
project-words.txt -
Review the words in
project-words.txtto check for any actual misspellings and remove them (the spell checker already thinks they are wrong). -
Fix spelling issues.
To show the issues and suggestions, use:
cspell --no-progress --show-suggestions --show-context "**/*.md" -
Repeat the process with the other file types you want to check.
3. Fine-tuning
The following resources can help you with fine-tuning your configurations:
Help
Command: lint -- Spell Checking
The lint command is used for spell checking files.
Help
cspell lint --help