Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Emacs editing environment, Part 2: Learn the essential modes and editing features of Emacs

Get going with this famous open source editor

Michael Stutz, Author, Consultant
Photo of Michael Stutz
Michael Stutz is author of The Linux Cookbook, which he also designed and typeset using only open source software. His research interests include digital publishing and the future of the book. He has used various UNIX operating systems for 20 years.

Summary:  One of the powerhouses of UNIX® computing, the open source Emacs editor is a large, complex application that does everything from edit text to function as a complete development environment. This tutorial, the second in a series, introduces you to some of the essential concept of modes, shows you some of the powerful text manipulation functions available, and teaches you how to use the built-in search, replace, and spellcheck facilities of Emacs.

View more content in this series

Date:  10 Apr 2007
Level:  Introductory

Activity:  20018 views
Comments:  

Use the spelling checker

Ispell, an interactive UNIX spelling checker, is built into Emacs and is a powerful and convenient way to check buffers for misspelled words. The various ispell- functions are described in this section.

Spell check a word

Use the ispell-word function (or its equivalent key binding, M-$) to check the spelling of the word at point.

That word baid doesn't look right. Move point to it and check the spelling by typing M-$.

Emacs doesn't recognize this word either, so it's highlighted as a misspelling; you're given a list of suggestions in a new window above the buffer window, as in Figure 6. Each suggestion is prefaced with a character you can type to replace the misspelling with that suggestion. (You can also type C-g to cancel the replacement.)


Figure 6. Correct spelling in Emacs with Ispell
Correct spelling in Emacs with Ispell.

Find the word you want, bid, and press the key that corresponds to it. Ispell exits and the misspelled word is replaced with the one you chose.

Spell check a region

You can also spell check the region with the ispell-region function.

Try using the mouse to highlight the last two lines in the buffer by pressing and holding B1 as you drag over them (as described in the first tutorial in this series). Then type M-x ispell-region to spell check this highlighted region of text.

In the minibuffer, Ispell reports that spell checking is complete and that no misspellings have been found.

Spell check a buffer

To check the spelling of the whole buffer, use the ispell-buffer function.

Try running it: Type M-x ispell-buffer.

As with all the Ispell commands, you have many options whenever you encounter a misspelling in a buffer check. They are described in Table 6.


Table 6. Ispell word-replacement commands
KeyDescription
character Make the suggested replacement prefaced by (character).
SpaceAccept this word as correct in this context.
iAccept this word as correct and insert it into the personal dictionary file.
aAccept this word as correct only for this Emacs session.
AAccept this word as correct only for this buffer in this Emacs session.
rReplace the word with a string you type (and that Ispell then rechecks).
RReplace the word with a string you type (and that Ispell then rechecks) and run a query-replace through the rest of the buffer.
lReplace the word with a given string and do a lookup of the new string in a given dictionary file.
uInsert a lowercase version of the word into the personal dictionary file.
mReplace the word with a given string, save it to the personal dictionary, and then recheck the word.
C-lRecenter the screen on the current line.
C-rEnter a recursive edit.
C-zSuspend Emacs. (In X, this iconifies the Emacs client window.)
xExit the spell check and move point back to its original position.
XExit the spell check, leaving point where it is.
qImmediately quit the spell check.
?Display a menu of options.

Catch misspellings as they happen

Flyspell mode is a special minor mode that highlights misspellings as you type them. This is particularly useful when you're writing quick documents (such as e-mails) or first drafts that have to get into production quickly. When Emacs encounters a misspelling, it doesn't stop you -- you can keep on typing -- but the misspelled word is highlighted in the buffer.

Flyspell mode works by running Ispell in the background; after you turn on Flyspell mode, even the existing text in the buffer is checked and any misspellings are highlighted.

Try it:

  1. Turn on Flyspell mode: Type M-x flyspell-mode.

  2. Move down to the end of the first stanza and type the following two lines, with the intentional misspelling on the first line:

    Little lamb, hwo made thee?
    Dost thou know who made thee?
    

Notice that Emacs didn't like Blake's antique Dost, even though you know it's right. Both it and the misspelled hwo are highlighted, as in Figure 7.


Figure 7. Use Emacs' Flyspell mode
Use Emacs' Flyspell mode

To correct the highlighted words, use the mouse pointer to click the middle button on each of them; a menu appears with suggested words. For hwo, choose the correct who; for Dost, choose Accept (buffer) to accept the spelling as correct for this buffer.

A few more commands are bound in Flyspell mode in addition to the mouse control; a list is given in Table 7.


Table 7. Key bindings for Flyspell mode
BindingDescription
M-$ Correct the last misspelled word using Ispell.
M-x flyspell-auto-correct-word, M-Tab Correct the last misspelled word automatically, with a suggestion found with Ispell.
M-x flyspell-auto-correct-previous-word Correct the previous misspelled word automatically, with a suggestion found with Ispell.
M-x flyspell-correct-word, B2 Display a pop-up menu with word suggestions.

Summary of Emacs spelling commands

Table 8 summarizes the various spelling functions you've just learned for Emacs, describing their meanings and giving the default key bindings, if any.


Table 8. Spelling commands for Emacs
BindingCommand or functionDescription
M-$ ispell-word Call Ispell to check the spelling of the word at point.
ispell-region Call Ispell to check the spelling of the region.
ispell-buffer Call Ispell to check the spelling of all the words from point to the end of the buffer.
flyspell-mode Call Ispell to check the spelling of all the words in buffer in the background as you type and highlight all misspellings.

6 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Open source
ArticleID=208259
TutorialTitle=Emacs editing environment, Part 2: Learn the essential modes and editing features of Emacs
publish-date=04102007
author1-email=stutz@dsl.org
author1-email-cc=mmccrary@us.ibm.com