codespell
Fix common misspellings in source code and other text files.
A file .codespellrc can be created at the root of the repository to configure codespell.
See the codespell documentation for details.
codespell documentation
- Version in MegaLinter: 2.4.1
- Visit Official Web Site
- See How to configure codespell rules
- See How to disable codespell rules in files
Configuration in MegaLinter
- Enable codespell by adding
SPELL_CODESPELLin ENABLE_LINTERS variable - Disable codespell by adding
SPELL_CODESPELLin DISABLE_LINTERS variable
- Enable autofixes by adding
SPELL_CODESPELLin APPLY_FIXES variable
| Variable | Description | Default value |
|---|---|---|
| SPELL_CODESPELL_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
| SPELL_CODESPELL_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
| SPELL_CODESPELL_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Include every file |
| SPELL_CODESPELL_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
| SPELL_CODESPELL_CLI_LINT_MODE | Override default CLI lint mode - file: Calls the linter for each file- list_of_files: Call the linter with the list of files as argument- project: Call the linter from the root of the project |
list_of_files |
| SPELL_CODESPELL_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".md", ".mdx", ".markdown", ".html", ".htm", ".rst", ".txt", ".json", ".jsonc", ".json5", ".yaml", ".yml"] |
| SPELL_CODESPELL_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
| SPELL_CODESPELL_PRE_COMMANDS | List of bash commands to run before the linter | None |
| SPELL_CODESPELL_POST_COMMANDS | List of bash commands to run after the linter | None |
| SPELL_CODESPELL_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling SPELL_CODESPELL and its pre/post commands | None |
| SPELL_CODESPELL_CONFIG_FILE | codespell configuration file name Use LINTER_DEFAULT to let the linter find it |
.codespellrc |
| SPELL_CODESPELL_RULES_PATH | Path where to find linter configuration file | Workspace folder, then MegaLinter default rules |
| SPELL_CODESPELL_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
| SPELL_CODESPELL_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
| SPELL_CODESPELL_CLI_EXECUTABLE | Override CLI executable | ['codespell'] |
MegaLinter Flavors
This linter is available in the following flavors
| Flavor | Description | Embedded linters | Info | |
|---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 131 | |
| c_cpp | Optimized for pure C/C++ projects | 57 | ||
| cupcake | MegaLinter for the most commonly used languages | 89 | ||
| documentation | MegaLinter for documentation projects | 50 | ||
| dotnet | Optimized for C, C++, C# or VB based projects | 65 | ||
| dotnetweb | Optimized for C, C++, C# or VB based projects with JS/TS | 74 | ||
| go | Optimized for GO based projects | 52 | ||
| java | Optimized for JAVA based projects | 55 | ||
| javascript | Optimized for JAVASCRIPT or TYPESCRIPT based projects | 60 | ||
| php | Optimized for PHP based projects | 55 | ||
| python | Optimized for PYTHON based projects | 66 | ||
| ruby | Optimized for RUBY based projects | 51 | ||
| rust | Optimized for RUST based projects | 51 | ||
| salesforce | Optimized for Salesforce based projects | 57 | ||
| swift | Optimized for SWIFT based projects | 51 | ||
| terraform | Optimized for TERRAFORM based projects | 55 |
Behind the scenes
How are identified applicable files
- If this linter is active, all files linted by all other active linters will be linted
- File extensions:
.md,.mdx,.markdown,.html,.htm,.rst,.txt,.json,.jsonc,.json5,.yaml,.yml
How the linting is performed
- codespell is called once with the list of files as arguments (
list_of_filesCLI lint mode)
Example calls
codespell
codespell ./my_file ./my_directory
codespell --config codespell.ini --check-filenames test.txt
Help content
usage: codespell [-h] [--version] [-d] [-c] [-w] [-D DICTIONARY]
[--builtin BUILTIN-LIST] [--ignore-regex IGNORE_REGEX]
[--ignore-multiline-regex IGNORE_MULTILINE_REGEX] [-I FILES]
[-L WORDS] [--uri-ignore-words-list WORDS] [-r REGEX]
[--uri-regex URI_REGEX] [-s] [--count] [-S SKIP] [-x FILES]
[-i MODE] [-q LEVEL] [-e] [-f] [-H] [-A LINES] [-B LINES]
[-C LINES] [--stdin-single-line] [--config CONFIG]
[--toml TOML]
[files ...]
positional arguments:
files files or directories to check
options:
-h, --help show this help message and exit
--version show program's version number and exit
-d, --disable-colors disable colors, even when printing to terminal
-c, --enable-colors enable colors, even when not printing to terminal
-w, --write-changes write changes in place if possible
-D, --dictionary DICTIONARY
comma-separated list of custom dictionary files that
contain spelling corrections. If this flag is not
specified or equals "-" then the default dictionary is
used.
--builtin BUILTIN-LIST
comma-separated list of builtin dictionaries to
include (when "-D -" or no "-D" is passed). Current
options are:
- 'clear' for unambiguous errors
- 'rare' for rare (but valid) words that are likely to
be errors
- 'informal' for making informal words more formal
- 'usage' for replacing phrasing with recommended
terms
- 'code' for words from code and/or mathematics that
are likely to be typos in other contexts (such as
uint)
- 'names' for valid proper names that might be typos
- 'en-GB_to_en-US' for corrections from en-GB to en-US
The default is 'clear,rare'.
--ignore-regex IGNORE_REGEX
regular expression that is used to find patterns to
ignore by treating as whitespace. When writing regular
expressions, consider ensuring there are boundary non-
word chars, e.g., "\bmatch\b". Defaults to
empty/disabled.
--ignore-multiline-regex IGNORE_MULTILINE_REGEX
regular expression that is used to ignore text that
may span multi-line regions. The regex is run with
re.DOTALL. For example to allow skipping of regions of
Python code using begin/end comments one could use:
--ignore-multiline-regex '# codespell:ignore-begin
*\n.*# codespell:ignore-end *\n'. Defaults to
empty/disabled.
-I, --ignore-words FILES
comma-separated list of files that contain words to be
ignored by codespell. Files must contain 1 word per
line. Words are case sensitive based on how they are
written in the dictionary file.
-L, --ignore-words-list WORDS
comma-separated list of words to be ignored by
codespell. Words are case sensitive based on how they
are written in the dictionary file.
--uri-ignore-words-list WORDS
comma-separated list of words to be ignored by
codespell in URIs and emails only. Words are case
sensitive based on how they are written in the
dictionary file. If set to "*", all misspelling in
URIs and emails will be ignored.
-r, --regex REGEX regular expression that is used to find words. By
default any alphanumeric character, the underscore,
the hyphen, and the apostrophe are used to build
words. This option cannot be specified together with
--write-changes.
--uri-regex URI_REGEX
regular expression that is used to find URIs and
emails. A default expression is provided.
-s, --summary print summary of fixes
--count print the number of errors as the last line of stderr
-S, --skip SKIP comma-separated list of files to skip. It accepts
globs as well. E.g.: if you want codespell to skip
.eps and .txt files, you'd give "*.eps,*.txt" to this
option.
-x, --exclude-file FILES
ignore whole lines that match those in the comma-
separated list of files EXCLUDE. The lines in these
files should match the to-be-excluded lines exactly
-i, --interactive MODE
set interactive mode when writing changes:
- 0: no interactivity.
- 1: ask for confirmation.
- 2: ask user to choose one fix when more than one is
available.
- 3: both 1 and 2
-q, --quiet-level LEVEL
bitmask that allows suppressing messages:
- 0: print all messages.
- 1: disable warnings about wrong encoding.
- 2: disable warnings about binary files.
- 4: omit warnings about automatic fixes that were
disabled in the dictionary.
- 8: don't print anything for non-automatic fixes.
- 16: don't print the list of fixed files.
- 32: don't print configuration files.
As usual with bitmasks, these levels can be combined;
e.g. use 3 for levels 1+2, 7 for 1+2+4, 23 for
1+2+4+16, etc. The default mask is 34.
-e, --hard-encoding-detection
use chardet to detect the encoding of each file. This
can slow down codespell, but is more reliable in
detecting encodings other than utf-8, iso8859-1, and
ascii.
-f, --check-filenames
check file names as well
-H, --check-hidden check hidden files and directories (those starting
with ".") as well.
-A, --after-context LINES
print LINES of trailing context
-B, --before-context LINES
print LINES of leading context
-C, --context LINES print LINES of surrounding context
--stdin-single-line output just a single line for each misspelling in
stdin mode
--config CONFIG path to config file.
--toml TOML path to a pyproject.toml file.
Installation on mega-linter Docker image
- Dockerfile commands :
# renovate: datasource=pypi depName=codespell
ARG PIP_CODESPELL_VERSION=2.4.1
- PIP packages (Python):
