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.
400 readers like this.
red pen editing mistakes

Opensource.com

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:

Aspell flagging the word PDF as a spelling error

opensource.com

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:

Aspell flagging "archving" as a spelling error

opensource.com

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.

Correcting the spelling "archiving"

opensource.com

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.

Tags
That idiot 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.

2 Comments

Nice! As I mostly write in Vim and Markdown this will help me a lot instead of copy'paste into something else to spell check.

At least on my Fedora, don't forget to also install the aspell dictionaries.

Great

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.