Commands for Fixing Typos
*************************

In this chapter we describe the commands that are especially useful for the times when you catch a mistake in your text just after you have made it, or change your mind while composing text on the fly.

The most fundamental command for correcting erroneous editing is the undo command, C-x u or C-_ or C-/.  This command undoes a single command (usually), a part of a command (in the case of query-replace), or several consecutive self-inserting characters.  Consecutive repetitions of the undo command undo earlier and earlier changes, back to the limit of the undo information available.  See Undo, for more information.

Undo
====

The undo commands undo recent changes in the buffer's text. Each buffer records changes individually, and the undo command always applies to the current buffer.  You can undo all the changes in a buffer for as far as back these records go.  Usually each editing command makes a separate entry in the undo records, but some commands such as query-replace divide their changes into multiple entries for flexibility in undoing.  Meanwhile, self-inserting characters are usually grouped to make undoing less tedious.

C-x u
C-_
C-/
     Undo one entry in the current buffer's undo records (undo).

To begin to undo, type the command C-/ (or its aliases, C-_ or C-x u).  This undoes the most recent change in the buffer, and moves point back to where it was before that change.

Consecutive repetitions of C-/ (or its aliases) undo earlier and earlier changes in the current buffer, back to the limit of the current buffer's undo records.  If all the recorded changes have already been undone, the undo command just signals an error.

If you notice that a buffer has been modified accidentally, the easiest way to recover is to type C-/ repeatedly until the stars disappear from the front of the mode line.  At this time, all the modifications you made have been canceled.  Whenever an undo command makes the stars disappear from the mode line, it means that the buffer contents are the same as they were when the file was last read in or saved.

If you do not remember whether you changed the buffer deliberately, type C-/ once.  When you see the last change you made undone, you will see whether it was an intentional change.  If it was an accident, leave it undone.  If it was deliberate, redo the change as described below.

Any command other than an undo command breaks the sequence of undo commands.  Starting from that moment, the previous undo commands become ordinary changes that you can undo.  Thus, to redo changes you have undone, type C-f or any other command that will harmlessly break the sequence of undoing, then type undo commands again.  On the other hand, if you want to resume undoing, without redoing previous undo commands, use M-x undo-only.  This is like undo, but will not redo changes you have just undone.

When there is an active region, any use of undo performs selective undo, undoing the most recent change within the region, instead of the entire buffer.  However, when Transient Mark mode is off (see Persistent Mark), C-/ always operates on the entire buffer, ignoring the region.  In this case, you can perform selective undo by supplying a prefix argument to the undo command: C-u C-/ or C-u C-x u.  To undo further changes in the same region, repeat the undo command (no prefix argument is needed).

Some specialized buffers do not make undo records.  Buffers whose names start with spaces never do; these buffers are used internally by Emacs and its extensions to hold text that users don't normally look at or edit.

When the undo records for a buffer becomes too large, Emacs discards the oldest undo records from time to time (during garbage collection).  You can specify how much undo records to keep by setting three variables: undo-limit, undo-strong-limit, and undo-outer-limit.  Their values are expressed in units of bytes of space.

The variable undo-limit sets a soft limit: Emacs keeps undo data for enough commands to reach this size, and perhaps exceed it, but does not keep data for any earlier commands beyond that.  Its default value is 20000.  The variable undo-strong-limit sets a stricter limit: a previous command (not the most recent one) which pushes the size past this amount is itself forgotten.  The default value of undo-strong-limit is 30000.

Regardless of the values of those variables, the most recent change is never discarded unless it gets bigger than undo-outer-limit (normally 3,000,000).  At that point, Emacs discards the undo data and warns you about it.  This is the only situation in which you cannot undo the last command.  If this happens, you can increase the value of undo-outer-limit to make it even less likely to happen in the future.  But if you didn't expect the command to create such large undo data, then it is probably a bug and you should report it. See Reporting Bugs.

The reason the undo command has three key bindings, C-x u, C-_ and C-/, is that it is worthy of a single-character key, but C-x u is more straightforward for beginners to remember and type.  Meanwhile, C-- on a text-only terminal is really C-_, which makes it a natural and easily typed binding for undoing.

Killing Your Mistakes
=====================

DEL
     Delete last character (delete-backward-char).

M-DEL
     Kill last word (backward-kill-word).

C-x DEL
     Kill to beginning of sentence (backward-kill-sentence).

The DEL character (delete-backward-char) is the most important correction command.  It deletes the character before point. When DEL follows a self-inserting character command, you can think of it as canceling that command.  However, avoid the confusion of thinking of DEL as a general way to cancel a command!

When your mistake is longer than a couple of characters, it might be more convenient to use M-DEL or C-x DEL. M-DEL kills back to the start of the last word, and C-x DEL kills back to the start of the last sentence.  C-x DEL is particularly useful when you change your mind about the phrasing of the text you are writing.  M-DEL and C-x DEL save the killed text for C-y and M-y to retrieve.  See Yanking.

M-DEL is often useful even when you have typed only a few characters wrong, if you know you are confused in your typing and aren't sure exactly what you typed.  At such a time, you cannot correct with DEL except by looking at the screen to see what you did.  Often it requires less thought to kill the whole word and start again.

Transposing Text
================

C-t
     Transpose two characters (transpose-chars).

M-t
     Transpose two words (transpose-words).

C-M-t
     Transpose two balanced expressions (transpose-sexps).

C-x C-t
     Transpose two lines (transpose-lines).

The common error of transposing two characters can be fixed, when they are adjacent, with the C-t command (transpose-chars).  Normally, C-t transposes the two characters on either side of point.  When given at the end of a line, rather than transposing the last character of the line with the newline, which would be useless, C-t transposes the last two characters on the line.  So, if you catch your transposition error right away, you can fix it with just a C-t.  If you don't catch it so fast, you must move the cursor back between the two transposed characters before you type C-t.  If you transposed a space with the last character of the word before it, the word motion commands are a good way of getting there.  Otherwise, a reverse search (C-r) is often the best way.  See Search.

M-t transposes the word before point with the word after point (transpose-words).  It moves point forward over a word, dragging the word preceding or containing point forward as well.  The punctuation characters between the words do not move.  For example, FOO, BAR transposes into BAR, FOO rather than BAR FOO,.

C-M-t (transpose-sexps) is a similar command for transposing two expressions (see Expressions), and C-x C-t (transpose-lines) exchanges lines.  They work like M-t except as regards what units of text they transpose.

A numeric argument to a transpose command serves as a repeat count: it tells the transpose command to move the character (word, expression, line) before or containing point across several other characters (words, expressions, lines).  For example, C-u 3 C-t moves the character before point forward across three other characters.  It would change f-!-oobar into oobf-!-ar.  This is equivalent to repeating C-t three times.  C-u - 4 M-t moves the word before point backward across four words.  C-u - C-M-t would cancel the effect of plain C-M-t.

A numeric argument of zero is assigned a special meaning (because otherwise a command with a repeat count of zero would do nothing): to transpose the character (word, expression, line) ending after point with the one ending after the mark.

Case Conversion
===============

M-- M-l
     Convert last word to lower case.  Note Meta-- is Meta-minus.

M-- M-u
     Convert last word to all upper case.

M-- M-c
     Convert last word to lower case with capital initial.

A very common error is to type words in the wrong case.  Because of this, the word case-conversion commands M-l, M-u and M-c have a special feature when used with a negative argument: they do not move the cursor.  As soon as you see you have mistyped the last word, you can simply case-convert it and go on typing.  See Case.

Checking and Correcting Spelling
================================

This section describes the commands to check the spelling of a single word or of a portion of a buffer.  These commands work with the spelling checker programs Aspell and Ispell, which are not part of Emacs. See Aspell (The Aspell Manual).

M-x flyspell-mode
     Enable Flyspell mode, which highlights all misspelled words.

M-x flyspell-prog-mode
     Enable Flyspell mode for comments and strings only.

M-$
     Check and correct spelling of the word at point (ispell-word).

M-TAB
ESC TAB
     Complete the word before point based on the spelling dictionary (ispell-complete-word).

M-x ispell
     Spell-check the active region or the current buffer.

M-x ispell-buffer
     Check and correct spelling of each word in the buffer.

M-x ispell-region
     Check and correct spelling of each word in the region.

M-x ispell-message
     Check and correct spelling of each word in a draft mail message, excluding cited material.

M-x ispell-change-dictionary RET DICT RET
     Restart the Aspell or Ispell process, using DICT as the dictionary.

M-x ispell-kill-ispell
     Kill the Aspell or Ispell subprocess.

Flyspell mode is a fully-automatic way to check spelling as you edit in Emacs.  It operates by checking words as you change or insert them. When it finds a word that it does not recognize, it highlights that word.  This does not interfere with your editing, but when you see the highlighted word, you can move to it and fix it.  Type M-x flyspell-mode to enable or disable this mode in the current buffer.

When Flyspell mode highlights a word as misspelled, you can click on it with Mouse-2 to display a menu of possible corrections and actions.  You can also correct the word by editing it manually in any way you like.

Flyspell Prog mode works just like ordinary Flyspell mode, except that it only checks words in comments and string constants.  This feature is useful for editing programs.  Type M-x flyspell-prog-mode to enable or disable this mode in the current buffer.

The other Emacs spell-checking features check or look up words when you give an explicit command to do so.

To check the spelling of the word around or before point, and optionally correct it as well, use the command M-$ (ispell-word).  If the word is not correct, Emacs offers you various alternatives for what to do about it.  If a region is active, M-$ checks the spelling of all words within the region (see Mark).  (However, when Transient Mark mode is off, M-$ always acts on the word around or before point, ignoring the region. See Persistent Mark.)

The M-x ispell command performs spell-checking on the entire buffer if no region is active, or on region if one is active.  (When Transient Mark mode is off, it always acts on the entire buffer, ignoring the region.)

The commands M-x ispell-buffer and M-x ispell-region explicitly perform spell-checking on the entire buffer or the region respectively.

To check spelling in an email message you are writing, use M-x ispell-message; that command checks the whole buffer, except for material that is indented or appears to be cited from other messages.

Each time these commands encounter an incorrect word, they ask you what to do.  They display a list of alternatives, usually including several ``near-misses''---words that are close to the word being checked.  Then you must type a single-character response.  Here are the valid responses:

SPC
     Skip this word---continue to consider it incorrect, but don't change it here.

r NEW RET
     Replace the word (just this time) with NEW.  (The replacement string will be rescanned for more spelling errors.)

R NEW RET
     Replace the word with NEW, and do a query-replace so you can replace it elsewhere in the buffer if you wish.  (The replacements will be rescanned for more spelling errors.)

DIGIT
     Replace the word (just this time) with one of the displayed near-misses.  Each near-miss is listed with a digit; type that digit to select it.

a
     Accept the incorrect word---treat it as correct, but only in this editing session.

A
     Accept the incorrect word---treat it as correct, but only in this editing session and for this buffer.

i
     Insert this word in your private dictionary file so that Aspell or Ispell will consider it correct from now on, even in future sessions.

u
     Insert the lower-case version of this word in your private dictionary file.

m
     Like i, but you can also specify dictionary completion information.

l WORD RET
     Look in the dictionary for words that match WORD.  These words become the new list of ``near-misses''; you can select one of them as the replacement by typing a digit.  You can use * in WORD as a wildcard.

C-g
     Quit interactive spell checking, leaving point at the word that was being checked.  You can restart checking again afterward with C-u M-$.

X
     Same as C-g.

x
     Quit interactive spell checking and move point back to where it was when you started spell checking.

q
     Quit interactive spell checking and kill the Ispell subprocess.

C-l
     Refresh the screen.

C-z
     This key has its normal command meaning (suspend Emacs or iconify this frame).

?
     Show the list of options.

The command ispell-complete-word, which is bound to the key M-TAB in Text mode and related modes, shows a list of completions based on spelling correction.  Insert the beginning of a word, and then type M-TAB; the command displays a completion list window.  (If your window manager intercepts M-TAB, type ESC TAB or C-M-i.)  To choose one of the completions listed, click Mouse-2 or Mouse-1 fast on it, or move the cursor there in the completions window and type RET.  See Text Mode.

Once started, the Aspell or Ispell subprocess continues to run (waiting for something to do), so that subsequent spell checking commands complete more quickly.  If you want to get rid of the process, use M-x ispell-kill-ispell.  This is not usually necessary, since the process uses no time except when you do spelling correction.

Ispell and Aspell use two dictionaries together for spell checking: the standard dictionary and your private dictionary.  The variable ispell-dictionary specifies the file name to use for the standard dictionary; a value of nil selects the default dictionary.  The command M-x ispell-change-dictionary sets this variable and then restarts the subprocess, so that it will use a different standard dictionary.

Aspell and Ispell use a separate dictionary for word completion. The variable ispell-complete-word-dict specifies the file name of this dictionary.  The completion dictionary must be different because it cannot use root and affix information.  For some languages there is a spell checking dictionary but no word completion dictionary.