Emacs tips: Checking spelling and syntax

197

Author: Nathan Willis

Even the Emacs-enlightened are still but humble human beings who may err. We make spelling mistakes in our writing, and syntactical mistakes in our structured documents. So let’s take a tour through Emacs’ built-in tools for preventing, catching, and correcting mistakes.

Spell-checking with Ispell

The basic Emacs spell-checking functions are a wrapper around the venerable Ispell program. Ispell is a separate package, but is a Unix staple virtually guaranteed to be included in all Linux distributions.

Spell-checking a single word is as simple as typing Meta-$. Emacs will look up the word beneath the cursor and report its results in the minibuffer at the bottom of the screen. If the word is correctly spelled, it will say so. If not, Emacs will bring up a listing of its suggestions for the correct spelling at the top of the screen.

The suggestions are displayed in “best guess” order, each one marked with an option number in parentheses (starting with zero). To replace the incorrect spelling with an option, just press the corresponding number on the keyboard. If none of the suggestions is correct, you can type r and enter the correct spelling yourself. Alternatively, if you know that the spelling is correct, but Ispell just doesn’t recognize it, you can skip over it by pressing the space bar, or insert it into your private dictionary by typing i.

If you are checking the spelling of a single word, the odds are that it is because you are not sure. You are more likely to start adding words to your private dictionary when you spell-check entire documents at once. In Emacs, you start this process by typing Meta-x ispell-buffer (or by choosing Spell Check Buffer from the Tools menu in a GUI session). Emacs will start at the beginning of the buffer and iterate through the entire text, highlighting each unrecognized word in turn and giving you the chance to correct it.

During such a process, you may notice Ispell repeatedly highlighting obscure words (such as proper nouns) that are spelled correctly, but which you don’t necessarily want to add to your private dictionary with i. For these, type a, and Emacs will accept the word as correct for the duration of your current setting.

For a fancier-looking, “on-the-fly” spell checking option, you can activate Flyspell mode, which will highlight and underscore misspelled words as you type, much like GUI-centric word processing apps. Similarly, as you type, you can press Meta-TAB and Emacs will suggest auto-completions for the word you are typing.

Look out, though, because on many windowing systems Meta-TAB is the same as Alt-Tab, which your window manager may intercept and use to cycle you through your open application windows. To get around this inconvenience, you can call the underlying function directly by typing Meta-x ispell-complete-word.

Major modes

Unfortunately, at present there is no good grammar checking program to accompany Ispell — a void felt by many a free software application. But Emacs can help you out with syntax when you are editing structured documents like HTML or C source code by using “major modes.”

Major modes redefine Emacs’ behavior to make editing a particular kind of text easier. Emacs automatically handles indentation, aligning blocks of code, and matching parentheses, brackets, and braces, and can even warn you of questionable constructions. In GUI Emacs sessions, changing into a major mode also adds a new function menu with specialized commands and macros.

You can switch into a major mode by typing Meta-x foo-mode, where foo is the mode name — generally the name of the markup or programming language you are interested in, such as c-mode or html-mode. The major mode is specific to the buffer you activate it for; if you have multiple files open they can all be edited in different modes and Emacs will keep them straight automatically. The status line at the bottom of the Emacs window (right above the minibuffer) always lists which major mode is currently active.

Even though you can switch major modes manually, for the most part it is unnecessary. Emacs will discern what type of text a given file contains when you open it up, and adjust its major mode for you. It makes this judgment based on the file extension, but also by looking at the contents of the file itself.

In programming modes like C-mode, the auto-formatting rules encompass a lot of complex features, handling nested indentation of functions, conditionals, and loops, automatically adjusting whitespace between blocks of code, and inserting comments.

For a markup language like HTML, I find the simplified tag insertion to be the biggest benefit. Simple keystroke macros insert tag pairs like HREF anchors and move the cursor in between them; after all, nothing is more embarrassing than forgetting to close a tag. And for long ordered or unordered lists, letting Emacs worry about which level you are at is a welcome relief.

Like everything else in the Emacs universe, major mode behaviors can be customized. The details are so specific to each major mode, however, that I think it best to refer you to the official Emacs manual for the details. Remember, the Emacs manual is not only available on the GNU project Web site, it is also accessible from within Emacs itself. Type Crtl-h to launch Emacs’ built-in help.