Skip to main content

The `aspell` Command in Linux

aspell is a command-line spell checker on Linux systems. It's known for its rich feature set and customization options. Unlike other spell checkers, aspell considers the context of words, which increases the accuracy of its suggestions.

Syntax

The basic syntax of the aspell command is as follows:

aspell [options] command

command is usually check for checking a document, but it can also be list to list misspelled words from standard input, among others.

Basic Usage

To start using aspell, you can simply type aspell check filename to check the spelling in a text file. aspell will go through the file, word by word, and suggest corrections for any words it thinks are misspelled.

Options Table

Here is a table with some common options for aspell:

OptionShorthandDescription
--masterSpecifies the main dictionary to use.
--personalSpecifies a personal word list file.
--replSpecifies a personal replacement list file.
--modeSets the mode such as email, html, etc.
--lang-lSets the language for spelling.
--encodingSpecifies the character encoding of the input text.
--ignoreIgnores words with numbers or all uppercase words.
--add-extra-dictsIncludes additional dictionaries.
--dont-backupPrevents creating a backup file.

Examples and Vim File Creation

Let's create an example text file with vim and then check it with aspell:

  1. Open vim and create a file:

    vim example.txt
  2. Press i to enter insert mode.

  3. Type or paste the following text:

    This is a smple text file.
    Contaning some misspelled words.
    You can add more txt to it.
  4. Press Esc, then type :wq to save and exit vim.

Now, let's check the spelling in example.txt:

aspell check example.txt

aspell will open an interactive mode where you can choose to ignore, replace, or add words to the dictionary.

If you want to list misspelled words from a file without entering the interactive mode, use:

aspell list < example.txt

To specify a language, use the --lang option:

aspell --lang=en check example.txt

Additional Dictionaries

aspell comes with a variety of dictionaries for different languages. You can add these additional dictionaries to aspell to check spelling for languages other than the default English. Each dictionary package is usually named aspell-lang, where lang is the two-letter ISO 639-1 code for the language.

For example, to install the Spanish dictionary, you would use your distribution's package manager:

sudo apt-get install aspell-es  # For Debian/Ubuntu
sudo yum install aspell-es # For CentOS/RHEL
sudo dnf install aspell-es # For Fedora

To use a newly installed dictionary, you can pass the --lang option to aspell followed by the two-letter language code:

aspell --lang=es check mi_archivo.txt

Custom Word Lists

You might want to add a custom word list to aspell if you frequently use specialized terminology that isn't included in the standard dictionary. aspell allows you to create personal dictionaries, or "word lists", to supplement the main dictionary.

To create or add words to a personal dictionary:

  1. Use aspell in the interactive mode and add words when prompted. These words will be added to your personal list.

    aspell --personal=./mywords.list check example.txt

    When you add a new word in the interactive mode, it will be added to mywords.list.

  2. Alternatively, create a list of words in a file manually. Open a file named mywords.list with vim:

    vim mywords.list
  3. Add your custom words, one per line:

    techterm1
    techterm2
    acronym1
    acronym2
  4. Save and exit by pressing Esc, then typing :wq.

To use this personal word list the next time you check a document, specify the --personal option:

aspell --personal=./mywords.list check example.txt

Replacement List

You can also have a replacement list that aspell will use to suggest corrections for frequently misspelled words. To create a replacement list:

  1. Manually create a file named replacements.list with vim:

    vim replacements.list
  2. Enter your common misspellings and the correct replacements in the following format, one per line:

    misspeling correction
    anotehr another
  3. Save and exit vim.

When running aspell, use the --repl option to specify your replacements list:

aspell --repl=./replacements.list check example.txt

aspell will now suggest corrections from your replacement list when it encounters the specified misspellings.

Certainly! aspell has the capability to check spelling within HTML files. It ignores the HTML tags and focuses on the text contained within them. Here's how you can use aspell to spell-check an HTML document:

Spell-Checking HTML Files with Aspell

When you want to check the spelling of plain text within an HTML file, you use the --mode=html option. This tells aspell to parse the file as HTML, ignoring the markup tags and focusing on the text content.

Example

  1. First, let's create a sample HTML file using vim. You can do this by running the command:
vim example.html
  1. Then, press i to switch to insert mode and enter the following HTML content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Sample Document</title>
</head>
<body>
<h1>Welcome to Our Websit</h1>
<p>
This is a sampl page to demonstrate how to use aspel for spell checking.
</p>
<p>Please contact us if you hve any queries.</p>
</body>
</html>
  1. Press Esc to exit insert mode, type :wq, and then press Enter to save and quit vim.

Using Aspell on the HTML file

Now that you have your HTML file, you can check its spelling with aspell by running:

aspell --mode=html check example.html

Expected Output

Running this command will open an interactive session, just as with plain text files. aspell will parse the HTML, ignore the tags, and highlight misspellings such as "Websit," "sampl," "aspel," and "hve." As before, it will suggest corrections and allow you to interactively choose whether to ignore, replace, or add each word to the dictionary.

For non-interactive use, you might want to list all misspelled words without going through each one. You can do this with:

aspell --mode=html list < example.html

This command will output a list of misspelled words found in the HTML content, such as:

Websit
sampl
aspel
hve

These utilities can greatly enhance the efficiency of proofreading HTML content, especially when maintaining websites or preparing content for web publication.

Conclusion

aspell is a powerful tool for detecting and correcting spelling mistakes in text files on Linux. With a variety of options and modes, it can be customized to suit the needs of different text types and languages. By integrating aspell into your editing workflow, you can ensure your writing is free of spelling errors and maintain a high standard of professionalism in your documents.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle