One-Liner for Finding Typos

kojix2

kojix2

Posted on October 29, 2024

One-Liner for Finding Typos

Let's Find Those Typos

typos.

typos --format brief --color always | ruby -e 'puts ARGF.readlines.sort_by{|l| l[/`.*?`/].size}'
Enter fullscreen mode Exit fullscreen mode

If you split it into multiple lines:

typos --format brief --color always |
ruby -e 'puts ARGF.readlines.sort_by{|l| l[/`.*?`/].size}'
Enter fullscreen mode Exit fullscreen mode

With this, you should be able to find typos in most OSS projects.

However, if it catches too many typos in changelogs/README instead of the actual code, try using the fd command to specify file extensions or exclude directories.

For example, the following command searches only for .cr files and excludes the lib directory:

fd -E 'lib/*' -e cr \
  | typos --format brief --color always --file-list - \
  | ruby -e 'puts ARGF.readlines.sort_by{|l| l[/`.*?`/].size}'
Enter fullscreen mode Exit fullscreen mode

Found a Typo? Send a Pull Request!

I even sent a pull request to fix typos found using the one-liner above for Crystal, and it got merged.

https://github.com/crystal-lang/crystal/pull/15080

What Does This Do?

In short, we’re using the typos command to find typos,

and a Ruby one-liner to sort the results by the length of the highlighted words.

Longer words tend to have a higher chance of actually containing typos.

Typo Fixes Make for Friendly Pull Requests

Fixing typos is the simplest way to contribute to OSS.

It’s also a very friendly pull request for maintainers.

Sure, some maintainers might briefly think, "Oh, just a typo fix..." since they get tons of PRs every day. But that thought passes quickly.

Reviewing typo fixes takes almost no mental energy. It’s just a quick glance, and with one click, the merge button does the rest. That’s why typo fixes are a lightweight, easy-to-accept PR for maintainers.

And once you’ve made even a small commit like a typo fix, you might start to feel more connected to that project. It’s a great first step. Why not dive in and make that first commit today?

💖 💪 🙅 🚩
kojix2
kojix2

Posted on October 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related