Spell Check on your Linux Terminal

Do you know you can use the terminal to check the spelling of the passage you’ve written? Terminal has a lot of utility commands but most people are unaware of most of those commands. I’ve seen people using Microsoft Word / Google to check the spelling of a word. This utility command will be the best alternative tool for developers to check the spelling. The fact that it comes pre-installed with the terminal is an added advantage.

Spell Check Command

ispell and aspell are the 2 commands used to check the spelling of a word. Out of these 2, ispell is the old spellchecker from GNU which has a limited capability to read different kinds of encoded files.

aspell is an interactive spell checker which checks the spelling via standard input or by reading through the file. It checks spelling on UTF-8 encoded files. It can read and check the spelling on markdown files too.

aspell has a lot of options. Running --help with the command shows the list of all the available options.

aspell --help
aspell --help command output

If you encounter errors while running the above command, it means that you don’t have it installed. Run the following command to install aspell in your machine.

sudo apt install aspell

How to check the spelling of words one-by-one interactively?

Pass -a flag with the aspell command to open it in interactive mode.

aspell -a
aspell -a command to open aspell in interactive mode

At this mode, you can enter the words with wrong spelling one by one and you will get a list of words with corrected spelling that are almost closer to the entered word.

Here’s an example screenshot,

Correct word suggestions from aspell command

From the above screenshot, you can see that aspell suggests multiple words for each wrong-spelled word by me.

The close alternatives for the word sampee are sample, simper, sampler. Similarly, you can see the suggestions for other words too (waier, calendrr, moble, bqttle).

This will be quite handy for the developers as they can quickly switch to the terminal and find the spelling of a word during their development. The fact that it does not require internet is an added advantage.

How to check the spelling of words from a file?

Using the terminal to check the spelling of words from a file is the best alternative approach if you don’t have internet access. Grammarly and Google Docs will best assist you with a good internet connection.

You can write the passage in a text file and pass the file path as an argument to -c flag in aspell command. This accept a HTML or Markdown file too.

aspell -c 

I’ve created a file named computer.txt and added the below content

Content of computer.txt file

I’ve made some spelling mistakes in the middle. You could be able to spot them easily. Some of them are macine instead of machine, intrenet instead of internet, etc.

Let’s ask aspell to find the spelling mistakes in this passage.

aspell -c computer.txt

Once you run the above command, you’ll be shown a screen similar to the following screenshot

aspell command to find spelling mistakes in a file

This means that, aspell has figured out some spelling mistakes in our passage. It will highlight the misspelled words one-by-one with the appropriate alternative correct spelled words at the bottom.

There will be 10 options shown with row number, you can choose the rightly spelled word by entering the corresponding row number.

For example, in the above screenshot, the word “macine” is highlighted and the right replacement for this misspelled word is “machine”, which is the 1st option. So, I press 1.

Immediately after I press 1, the correction was made and aspell moves to the next misspelled word.

aspell command corrects and moves to the next misspelled word in the file

In addition to the 10 options, aspell command shows 8 different options. You can choose either one of the them if you did not find the right spelled word from the above 10 alternate options.

Let’s understand each option,

CharacterActionDescription
iIgnoreIgnore this occurrence and move on to next mis-spelled word
rReplaceReplace this word by manually entering a new word
aAddAdd this word to the dictionary
bAbortAbort this operation (The changes you applied will not be saved)
IIgnore allIgnore all occurrences of this word
RReplace allReplace all occurrences of this word by manually entering a new word
lAdd LowerAdd the word to the dictionary
xExitExit the operation (The changes you applied will be saved)
Table describing the action for each character for aspell check command

Once you’re done with correcting the spelling of all the misspelled words, the file will be automatically saved by aspell. In addition to that, a new file will be created with the name .bak , which is a backup of the same file without applying spell check.

How to ignore creating a new file while correcting the spelling of the words in a file?

This is quite simple and can be easily achieved by passing a flag with the aspell command. The flag is --dont-backup.

Let’s look at an example command,

aspell check --dont-backup computer.txt
Ignore creating a backup file by passing --dont-backup flag

From the above screenshot, you can see that I’ve removed the existing computer.txt.bak file and run the spell check by passing --dont-backup flag. No more .bak file has been created after I complete the spell check.

You may also notice one more change from the previous and the above command. That is check and -c. In my previous command, I used -c, but in the above command, I used check to pass the file name.

Yes. you can either use -c or check to pass the file name. Both of them do the same job.

Is it possible to check the spelling on other files?

Absolutely Yes. aspell checks for spelling by reading Markdown and HTML files too. You have to pass the mode of the file as a separate argument (--mode).

Here’s the syntax,

aspell check --mode= file_name

The supported modes are none, url, email, markdown, html, tex, texinfo, and nroff.

Let’s see a quick example of fixing the spelling mistakes on a markdown file.

Sample markdown file content

I’ve created a markdown file named spellcheck.md and added the content as shown in the above screenshot.

Let’s fire our aspell command by passing this markdown file.

aspell check --dont-backup --mode=markdown spellcheck.md
Running aspell with the markdown file

It opens a similar interface, but it understood the markdown format and highlights only the misspelled word.

I like to bring one important item to your attention. You can find a misspelled word (blok) in the middle of the backtick (“` … “`) block. The content inside this block will not be evaluated by the aspell command. So, you’ll not be able to spot and correct the misspelled words inside the block.

Similarly, you can evaluate the spelling in the HTML files by changing the mode to html.

Conclusion

In this article, you have learnt to check the spelling of words using your terminal.

Subscribe to my newsletter by entering your email address in the below box to receive more such insightful articles that get delivered straight to your inbox.

Have a look at my site which has a consolidated list of all my blogs.

Share this article