LinuxCommandLibrary

codespell

Find and fix spelling errors in code

TLDR

Check for typos in all text files in the current directory, recursively

$ codespell
copy

Correct all typos found in-place
$ codespell --write-changes
copy

Skip files with names that match the specified pattern (accepts a comma-separated list of patterns using wildcards)
$ codespell --skip "[pattern]"
copy

Use a custom dictionary file when checking (--dictionary can be used multiple times)
$ codespell --dictionary [path/to/file.txt]
copy

Do not check words that are listed in the specified file
$ codespell --ignore-words [path/to/file.txt]
copy

Do not check the specified words
$ codespell --ignore-words-list [ignored_word1,ignored_word2,...]
copy

Print 3 lines of context around, before or after each match
$ codespell --[context|before-context|after-context] [3]
copy

Check file names for typos, in addition to file contents
$ codespell --check-filenames
copy

SYNOPSIS

codespell [options] [files or directories]

PARAMETERS

-w, --write-changes
    Write changes to files instead of displaying a diff.

-q, --quiet-level
    Set verbosity level: 1 (default), 2 (less), 3 (errors only).

-i , --ignore-words
    Read allowed words from specified file.

-I , --ignore-paths
    Read paths to ignore from specified file. One path per line. Globs are supported.

-d , --dictionary
    Use custom dictionary. Supported values: clear, informal, rare, words.

-s =, --substitute =
    Substitute specified word with the specified correction.

--check-filenames
    Check filenames for spelling errors.

--builtin
    Use builtin wordlist.

--version
    Show version number and exit.

-h, --help
    Show this help message and exit.

DESCRIPTION

codespell is a command-line tool designed to find and correct common spelling mistakes in source code, documentation, and text files.
It aims to improve code quality and readability by automatically identifying misspelled words. codespell is configurable, allowing users to customize its behavior with ignore files and different dictionaries. It's particularly useful in large projects where manual spell checking is impractical. It helps maintain consistency and professionalism across the codebase.
The utility can be easily integrated into continuous integration (CI) pipelines to ensure that new code meets spelling standards before it's merged. It is commonly used to quickly search text files and apply changes. The focus is on common misspellings found in programming and documentation.

CAVEATS

codespell relies on a predefined list of common misspellings, so it may not catch all errors.
It's essential to review suggested changes carefully before applying them, as context matters and some corrections may be inappropriate. It's also important to maintain an ignore list for project-specific terms or abbreviations.

INTEGRATION WITH GIT

codespell can be easily integrated into a Git pre-commit hook to automatically check for spelling errors before committing code. This helps to maintain a high standard of code quality.
Example: Create a `.git/hooks/pre-commit` file with the following content:

#!/bin/sh
codespell -w
git add .

IGNORE FILES

Use `.codespellignore` (or the file specified with `-I`) to exclude files or directories from spell checking.
Each line in the ignore file should specify a single path or glob pattern to ignore. This is useful for excluding third-party libraries or generated code. Use `-i` to ignore specific words.

HISTORY

codespell emerged to address the need for automated spell checking specifically tailored for source code environments.
Its development focuses on providing a simple, configurable, and effective tool for identifying and correcting common spelling mistakes. It has gained popularity as a pre-commit hook or CI pipeline check to improve code quality.

SEE ALSO

aspell(1), hunspell(1)

Copied to clipboard