How to check spelling at the Linux command line with Aspell

How to check spelling at the Linux command line with Aspell

GNU Aspell is a nifty utility that flags spelling errors in plain text files.

How to check spelling at the Linux command line with Aspell
Image by : 

opensource.com

x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

Proper spelling doesn't seem to be very important to many people these days. There are, however, those of us for whom it is. Yes, I am one of those people.

While I'm not a spelling cop, misspelled words stick out when I encounter them. They hurt my eyes. They hurt my brain. Any good text editor or word processor packs a spelling checker. If you're working in plain text, you can go another route to check spelling: at the command line with a nifty utility called GNU Aspell (which I'll be calling Aspell from here on in).

Aspell is fast, easy to use, and flexible. Let's take a look at how to use it.

Getting going

First, make sure you have Aspell installed on your system. It's standard kit with most Linux distributions. To find out if Aspell is installed, open a terminal window and type which aspell. That command should return something like /usr/bin/aspell. If it returns nothing, you can install Aspell using your distro's package manager, or you can download and install it.

So you have a text file that you want to spell check at the command line. Crack open a terminal window and navigate to the directory containing the text file you want to spell check. Then, run the following command:

aspell check file.txt

Aspell opens the text file in a two-pane interactive editor:

The top pane shows the file, with any errors (or perceived errors) highlighted. The bottom lists the suggested corrections (based on Aspell's default dictionary) and various commands that you can use.

In the screen capture above, Aspell has flagged the acronym "PDF" as an error and suggested several alternatives. I can do the following:

  • Press the number on my keyboard next to an alternative to replace the misspelled word with another one.
  • Press i to ignore that instance of the perceived error, or press I to ignore all instances of the error.
  • Press a to add the word to Aspell's dictionary.
  • Press r or R to replace that instance or all instances of the word with a new word.

Let's look at the last item in that list a little more closely. Let's say I use the word archiving a number of times in a file. And, being a consistent person, I misspell the word as archving each time I use it. Aspell will point that out to me:

Instead of correcting the spelling of that word each time, I want to do it in one fell swoop. So, I press R. Aspell prompts me for a replacement.

I type the replacement and then press Enter. The deed is done, and Aspell moves to the next mistake.

Using some of Aspell's options

Like any command-line utility, Aspell has a number of options. You probably won't use many of them, but here are two that I find useful.

First off, –dont-backup. When you finish spell checking a file, Aspell saves a copy of the original with the extension .bak—for example, djvu_utilities.txt.bak. I don't like backup files littering my directories. By specifying the –dont-backup option, Aspell doesn't save a copy.

Next, –mode=. Not every file I spell check at the command line is straight text. Often, I'll check Markdown, LaTeX, or HTML files. When I run Aspell with no options, it flags markup as spelling mistakes. So, instead of filling my dictionary with markup, I can specify –mode=tex or –mode=html. If you want a full list of the modes you can use, type aspell dump modes.

This is far from an exhaustive look at Aspell and its capabilities. If you're really interested in everything it can do, check out the manual.

Topics

About the author

That idiot Scott Nesbitt ...
Scott Nesbitt - I'm a long-time user of free/open source software, and write various things for both fun and profit. I don't take myself all that seriously and I do all of my own stunts. You can find me at these fine establishments on the web: Twitter, Mastodon, GitLab...