Configuration Resolution & Overrides
Spell checking is a two step process:
Step 1: Determine Which Files to Check
This is based on the command line together with the configuration file loaded at start up (the file nearest
the current directory, or the one given with --config).
- If globs are given on the command line, they are used to search for matching files. (If none are given, the
configuration's
filessetting is used as the globs instead.) The results are filtered against the configuration'signorePaths(plus any--excludeglobs) and against.gitignore(unless--no-gitignoreis used). - If
--file,--files, or--file-listis used, those are treated as exact file names instead of globs. They are still filtered againstignorePaths/--excludeand.gitignoreby default -- unless--force-checkis given, which checks them anyway. (When globs are also given on the command line,--file/--file-listentries must additionally match one of those globs.)
See Understanding CSpell Globs for glob syntax.
A file is also skipped -- regardless of overrides or other settings -- if CSpell determines it to be binary,
or of a known binary / generated file type.
Step 2: Determine the Settings and Check the Document
Once a file has been selected, CSpell determines its settings and checks it.
Loading Configuration
CSpell merges settings from the following sources, in order. Later sources override earlier ones for individual settings (see Merge Rules below for how array-like settings are combined instead of overwritten).
- Default Configuration -- the settings built into
cspell(disable with--no-default-configuration). - The configuration loaded at start up -- the same configuration file used in Step 1, found by searching
upward from the current directory (or
--root), or given explicitly with--config <path>. Any files itimports are merged in first, with the file's own settings merged in last so it always wins over what it imports. A few CLI flags adjust these settings directly:--dictionary <name>/--disable-dictionary <name>(enable/disable dictionaries) and--report <level>(which categories of unknown words to report). - The configuration nearest the document -- found by a second, independent search that starts at the
document's own directory and walks upward. It overrides everything above it. (Skipped when
--no-config-searchis used, or when--configis given explicitly.)
This means a cspell.json placed next to (or above) an individual file always wins over the configuration file
found from the current working directory.
Merge Rules
Most individual settings will overwrite the settings from the previous step / file. There are a few important exceptions.
Array Settings are Unions
Most Array like settings are joined as a union. In most cases order is preserved.
Examples:
- Word lists like:
words,flagWords,ignoreWordsare the union of all the settings. The order is not preserved. overridesandlanguageSettingsare accumulated in the order they were loaded to be applied later.patterns,includeRegExpList, andignoreRegExpListare collected in order so that patterns of the same name can be replaced.dictionaryDefinitionsanddictionariesare collected in order so that dictionaries can be replaced by other dictionaries with the same name.
Finalizing Settings
Once the configuration is merged, CSpell finalizes the settings for the document, in order:
overrides-- settings from entries whosefilenameglob matches the document's path are applied. If the resultingenabledsetting isfalse, the document is skipped and not spell checked.languageSettings-- settings from entries whoselanguageIdand/orlocalematch the document are applied.--language-idand--localeon the command line can force what a document is treated as for this matching step.- In-document directives -- comments like
cspell:ignoreorcspell:wordsin the document itself. These always win, and are applied last. See In-Document Settings.
Override Configuration Field: overrides
The overrides configuration is a useful way to force configuration on a per file basis.
The filename field is used to set the glob selection criteria.
When the path of the file being checked matches the glob/globs specified in filename, the override settings will be applied.
Example Overrides
filename can be a single glob or an array of globs.
Example:
"overrides": [
{
// Force `*.hrr` and `*.crr` files to be treated as `cpp` files:
"filename": "**/{*.hrr,*.crr}",
"languageId": ["cpp", "hpp"] // Set the languageId `cpp` and `hpp` overriding the defaults.
},
{
// Force `*.txt` to use the Dutch dictionary (Dutch dictionary needs to be installed separately):
"filename": "**/dutch/**/*.txt",
"language": "nl",
},
{
// Enable the `lorem-ipsum` dictionary for all test files.
"filename": ["**/*.test.ts", "**/test/**"],
"dictionaries": ["lorem-ipsum"]
},
{
"filename": "**/images/**",
"enabled": false // Disable spellchecking.
}
]