Spell-checking code using Typos

Typos works differently from other spell checkers, and is easier to keep updated

Written by Rico Sta. Cruz
(@rstacruz) · 18 Dec 2023
...

I’ve been struggling with lots of typos on my articles. I tried a few solutions, and eventually found one that works for me called typos.

Checks code and prose

Running typos on this very blog shows that I have an embarrassing amount of typos. Anyway, check out how it’s been helpful here:

  • Works on code. Typos splits identifiers into words (eg, ArticleSubcribeBox is checked as “Article Subcribe Box”).
  • Works on prose. Works great on Markdown files and its derivatives.
Terminal window
brew install typos-cli
·
·
typos
error: `classses` should be `classes`
--> ./articles/2023/advanced-tailwind.mdx:185:18
|
185 | ### Other pseudo-classses
| ^^^^^^^^
|
error: `Subcribe` should be `Subscribe`
--> ./articles/2022/public-cdns-arent-useful-anymore.mdx:64:12
|
64 | <ArticleSubcribeBox />
| ^^^^^^^^
|

Blocks typos

  • Uses a list of typo corrections (not valid words)
  • Pro: False positives are very rare
  • Pro: Hardly gets confused with jargon, URL’s, hex codes, etc
  • Con: Ignores typos it doesn’t know about

Unlike traditional spell checkers, typos doesn’t use dictionaries of valid words. Instead, it maintains a list of typo corrections. Typos maintains a large typo correction list (shown below). As of Dec 2023, it has 4291 entries, and is updated regularly.

dictionary.txt
abudance->abundance
didnt->didn't
realyl->really
surviver->survivor
yaer->year
·
·
·

Low maintenance overhead

  • Pro: Low effort to maintain (eg, no need to add new words)
  • Pro: Suitable for unassisted corrections (CI)
  • Con: Ignores typos it doesn’t know about

You can argue Typos’s approach makes it less effective, but I’ve found that it makes it more practical. My experience with dictionary-based spell checkers like cspell needed a lot of configuration to deal with false positives. This is less of a problem with typos.

One of the use cases I wanted this for is to add spell check to rstacruz/cheatsheets, which has 429 contributors as of Dec 2023. I initially considered traditional dictionary-based spell checkers cspell, but doing so would mean more overhead for anyone contributing cheatsheets. Any new words would need to be added to the dictionary.

I’d prefer to lower the barrier to contribution as much as possible. Typos seems like a good fit for this.

Powered by Rust

  • Fast. Fast startup. Fast in CI.
  • Cross platform. Available in Linux and MacOS (install instructions)

The typos GitHub Action finishes in 8 seconds in one moderately-sized repo I maintain. My experience with a code editor is seamless too… corrections happen as instantly as I typed.

Terminal window
brew install typos-cli
cargo install typos-cli
sudo pacman -S typos

One-step fixing of typos

  • Run typos to show changes
  • Run typos -w to apply changes to all files

Also: Typos comes with a --diff option (and --format brief, shown below) to inspect changes. Running typos -w to apply the changes.

# Inspect changes
typos --format brief
articles/2022/public-cdns….md:64:12: `Reuseable` -> `Reusable`
articles/2022/articl….md:14:8: `mutiple` -> `multiple`
articles/2022/lua….md:14:8: `DotA` -> `data`
# Write to file
typos -w

Can deal with false positives

  • Confgurable using .typos.toml
  • Can ignore files (files.extend-exclude)
  • Can add words (default.extend-words)

While false positives are rare, they do happen. The word DotA above is one of such examples. Typos has a configuration file to deal with these.

.typos.toml
[default.extend-words]
"DotA" = "DotA" # don't autocorrect 'DotA' to 'data'
[files]
extend-exclude = [
# Ignore these files
"localized/*.po",
]

Supports some editors

Unfortunately, I couldn’t find integrations to other editors. In any case, it’s also implemented as an LSP, which means it should be possible to integrate it with other LSP-supporting editors.

Screenshot of typos-lsp on Neovim

Alternatives I considered

Before settling on Typos, I also had a look at codespell and cspell.

Typos vs codespell

codespell is another spell checker with very similar goals to Typos. It also uses a list of typo corrections. In my experience, it works very well. I ended up sticking to Typos for 2 reasons:

  • Typos corrected more typos (at least on my code bases). This was surprising considering codespell’s corrections list is at least 10 times larger than Typos’s.
  • Typos has Vscode support.

Typos vs cspell

cspell is a dictionary-based spell checker. I’ve given this a try in some code bases I maintained, and usually produced good results.

Ultimately, I ended up not using it. While cspell can be very comprehensive if you took time to add the right dictionaries, my needs were a bit more modest and I preferred the low maintenance approach of Typos.

Conclusion

Typos is a spell checker that works on code and prose. It’s low maintenance, and works with my favourite editors. While I might consider cspell for when spell checking must be as strict as possible, I think Typos is a great fit for cases where convenience is more important.

Do you use a spell checker in your editor? Let me know your experiences in the comments below.

Written by Rico Sta. Cruz

I am a web developer helping make the world a better place through JavaScript, Ruby, and UI design. I write articles like these often. If you'd like to stay in touch, subscribe to my list.

Comments

More articles

← More articles