Check Your Spelling at the Command Line with Aspell
(Note: This post was first published at Opensource.com and appears here via a CC-BY-SA 4.0 International License.)
Correct 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 and, of course, any word processor packs a spelling checker. If you're working in plain text, you can go another route to check the spelling of your document: at the command line. How? With the help of a nifty utility called GNU Aspell (which I’ll be calling Aspell from here on in).
Aspell is fast, easy to use (yes, even for a command line tool!), and flexible. Let’s take a look at how to use it.
First, make sure that 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, then you can install Aspell using your distro’s package manager or download and install it. You'll probably also need to install a dictionary, which (again) you can do from your package manager.
So you have a text file that you want to spell check at the command line. What now? Crack open a terminal window and then navigate to the directory containing the text file that you want to spell check. Then, run this 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 beside an alternative on my keyboard 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 once it's done its work.
Next, –mode=. Not all of the files that I spell check at the command line are straight text. Often, I’ll check file formatted with Markdown, LaTeX, or HTML. When I run Aspell with no options, it flags any 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 that you can use, type aspell dump modes.
What you’ve just read is far from an exhaustive look at Aspell and its capabilities. If you’re really interested in everything that Aspell can do, take a peek at the manual.