001: @c This is part of the Emacs manual.
002: @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2018 Free Software
003: @c Foundation, Inc.
004: @c See file emacs.texi for copying conditions.
005: @node Fixit
006: @chapter Commands for Fixing Typos
007: @cindex typos, fixing
008: @cindex mistakes, correcting
009: 
010:   In this chapter we describe commands that are useful when you catch
011: a mistake while editing.  The most fundamental of these commands is
012: the undo command @kbd{C-/} (also bound to @kbd{C-x u} and @kbd{C-_}).
013: This undoes a single command, or a
014: part of a command (as in the case of @code{query-replace}), or several
015: consecutive character insertions.  Consecutive repetitions of
016: @kbd{C-/} undo earlier and earlier changes, back to the limit of the
017: undo information available.
018: 
019:   Aside from the commands described here, you can erase text using
020: deletion commands such as @key{DEL} (@code{delete-backward-char}).
021: These were described earlier in this manual.  @xref{Erasing}.
022: 
023: @menu
024: * Undo::        The Undo commands.
025: * Transpose::   Exchanging two characters, words, lines, lists...
026: * Fixing Case:: Correcting case of last word entered.
027: * Spelling::    Apply spelling checker to a word, or a whole file.
028: @end menu
029: 
030: @node Undo
031: @section Undo
032: @cindex undo
033: @cindex changes, undoing
034: 
035:   The @dfn{undo} command reverses recent changes in the buffer's text.
036: Each buffer records changes individually, and the undo command always
037: applies to the current buffer.  You can undo all the changes in a
038: buffer for as far back as the buffer's records go.  Usually, each editing
039: command makes a separate entry in the undo records, but some commands
040: such as @code{query-replace} divide their changes into multiple
041: entries for flexibility in undoing.  Consecutive character insertion
042: commands are usually grouped together into a single undo record, to
043: make undoing less tedious.
044: 
045: @table @kbd
046: @item C-/
047: @itemx C-x u
048: @itemx C-_
049: Undo one entry in the current buffer's undo records (@code{undo}).
050: @end table
051: 
052: @kindex C-x u
053: @kindex C-_
054: @kindex C-/
055: @findex undo
056:   To begin to undo, type @kbd{C-/} (or its aliases, @kbd{C-_} or
057: @kbd{C-x u})@footnote{Aside from @kbd{C-/}, the @code{undo} command is
058: also bound to @kbd{C-x u} because that is more straightforward for
059: beginners to remember: @samp{u} stands for ``undo''.  It is also bound
060: to @kbd{C-_} because typing @kbd{C-/} on some text terminals actually
061: enters @kbd{C-_}.}.  This undoes the most recent change in the buffer,
062: and moves point back to where it was before that change.
063:   Consecutive repetitions of @kbd{C-/} (or its aliases) undo earlier
064: and earlier changes in the current buffer.  If all the recorded
065: changes have already been undone, the undo command signals an error.
066: 
067: @cindex redo
068: @findex undo-only
069:   Any command other than an undo command breaks the sequence of undo
070: commands.  Starting from that moment, the entire sequence of undo
071: commands that you have just performed are themselves placed into the
072: undo record.  Therefore, to re-apply changes you have undone, type
073: @kbd{C-f} or any other command that harmlessly breaks the sequence of
074: undoing; then type @kbd{C-/} one or more times to undo some of the
075: undo commands.
076: 
077:   Alternatively, if you want to resume undoing, without redoing
078: previous undo commands, use @kbd{M-x undo-only}.  This is like
079: @code{undo}, but will not redo changes you have just undone.
080: 
081:   If you notice that a buffer has been modified accidentally, the
082: easiest way to recover is to type @kbd{C-/} repeatedly until the stars
083: disappear from the front of the mode line (@pxref{Mode Line}).
084: Whenever an undo command makes the stars disappear from the mode line,
085: it means that the buffer contents are the same as they were when the
086: file was last read in or saved.  If you do not remember whether you
087: changed the buffer deliberately, type @kbd{C-/} once.  When you see
088: the last change you made undone, you will see whether it was an
089: intentional change.  If it was an accident, leave it undone.  If it
090: was deliberate, redo the change as described above.
091: 
092: Alternatively, you can discard all the changes since the buffer was
093: last visited or saved with @kbd{M-x revert-buffer}
094: (@pxref{Reverting}).
095: 
096: @cindex selective undo
097: @kindex C-u C-/
098:   When there is an active region, any use of @code{undo} performs
099: @dfn{selective undo}: it undoes the most recent change within the
100: region, instead of the entire buffer.  However, when Transient Mark
101: mode is off (@pxref{Disabled Transient Mark}), @kbd{C-/} always
102: operates on the entire buffer, ignoring the region.  In this case, you
103: can perform selective undo by supplying a prefix argument to the
104: @code{undo} command: @kbd{C-u C-/}.  To undo further changes in the
105: same region, repeat the @code{undo} command (no prefix argument is
106: needed).
107: 
108:   Some specialized buffers do not make undo records.  Buffers whose
109: names start with spaces never do; these buffers are used internally by
110: Emacs to hold text that users don't normally look at or edit.
111: 
112: @vindex undo-limit
113: @vindex undo-strong-limit
114: @vindex undo-outer-limit
115: @cindex undo limit
116:   When the undo information for a buffer becomes too large, Emacs discards
117: the oldest records from time to time (during @dfn{garbage
118: collection}).  You can specify how much undo information to keep by
119: setting the variables @code{undo-limit}, @code{undo-strong-limit}, and
120: @code{undo-outer-limit}.  Their values are expressed in bytes.
121: 
122:   The variable @code{undo-limit} sets a soft limit: Emacs keeps undo
123: data for enough commands to reach this size, and perhaps exceed it,
124: but does not keep data for any earlier commands beyond that.  Its
125: default value is 80000.  The variable @code{undo-strong-limit} sets a
126: stricter limit: any previous command (though not the most recent one)
127: that pushes the size past this amount is forgotten.  The default value
128: of @code{undo-strong-limit} is 120000.
129: 
130:   Regardless of the values of those variables, the most recent change
131: is never discarded unless it gets bigger than @code{undo-outer-limit}
132: (normally 12,000,000).  At that point, Emacs discards the undo data and
133: warns you about it.  This is the only situation in which you cannot
134: undo the last command.  If this happens, you can increase the value of
135: @code{undo-outer-limit} to make it even less likely to happen in the
136: future.  But if you didn't expect the command to create such large
137: undo data, then it is probably a bug and you should report it.
138: @xref{Bugs,, Reporting Bugs}.
139: 
140: @node Transpose
141: @section Transposing Text
142: 
143: @table @kbd
144: @item C-t
145: Transpose two characters (@code{transpose-chars}).
146: @item M-t
147: Transpose two words (@code{transpose-words}).
148: @item C-M-t
149: Transpose two balanced expressions (@code{transpose-sexps}).
150: @item C-x C-t
151: Transpose two lines (@code{transpose-lines}).
152: @end table
153: 
154: @kindex C-t
155: @findex transpose-chars
156:   The common error of transposing two characters can be fixed, when they
157: are adjacent, with the @kbd{C-t} command (@code{transpose-chars}).  Normally,
158: @kbd{C-t} transposes the two characters on either side of point.  When
159: given at the end of a line, rather than transposing the last character of
160: the line with the newline, which would be useless, @kbd{C-t} transposes the
161: last two characters on the line.  So, if you catch your transposition error
162: right away, you can fix it with just a @kbd{C-t}.  If you don't catch it so
163: fast, you must move the cursor back between the two transposed
164: characters before you type @kbd{C-t}.  If you transposed a space with
165: the last character of the word before it, the word motion commands
166: (@kbd{M-f}, @kbd{M-b}, etc.) are a good way of getting there.
167: Otherwise, a reverse search (@kbd{C-r}) is often the best way.
168: @xref{Search}.
169: 
170: @kindex C-x C-t
171: @findex transpose-lines
172: @kindex M-t
173: @findex transpose-words
174: @c Don't index C-M-t and transpose-sexps here, they are indexed in
175: @c programs.texi, in the "List Commands" node.
176: @c @kindex C-M-t
177: @c @findex transpose-sexps
178:   @kbd{M-t} transposes the word before point with the word after point
179: (@code{transpose-words}).  It moves point forward over a word,
180: dragging the word preceding or containing point forward as well.  The
181: punctuation characters between the words do not move.  For example,
182: @w{@samp{FOO, BAR}} transposes into @w{@samp{BAR, FOO}} rather than
183: @w{@samp{BAR FOO,}}.  When point is at the end of the line, it will
184: transpose the word before point with the first word on the next line.
185: 
186:   @kbd{C-M-t} (@code{transpose-sexps}) is a similar command for
187: transposing two expressions (@pxref{Expressions}), and @kbd{C-x C-t}
188: (@code{transpose-lines}) exchanges lines.  They work like @kbd{M-t}
189: except as regards the units of text they transpose.
190: 
191:   A numeric argument to a transpose command serves as a repeat count: it
192: tells the transpose command to move the character (or word or
193: expression or line) before or containing point across several other
194: characters (or words or expressions or lines).  For example, @kbd{C-u
195: 3 C-t} moves the character before point forward across three other
196: characters.  It would change @samp{f@point{}oobar} into
197: @samp{oobf@point{}ar}.  This is equivalent to repeating @kbd{C-t}
198: three times.  @kbd{C-u - 4 M-t} moves the word before point backward
199: across four words.  @kbd{C-u - C-M-t} would cancel the effect of plain
200: @kbd{C-M-t}.
201: 
202:   A numeric argument of zero is assigned a special meaning (because
203: otherwise a command with a repeat count of zero would do nothing): to
204: transpose the character (or word or expression or line) ending after
205: point with the one ending after the mark.
206: 
207: @node Fixing Case
208: @section Case Conversion
209: 
210: @table @kbd
211: @item M-- M-l
212: Convert last word to lower case.  Note @kbd{Meta--} is Meta-minus.
213: @item M-- M-u
214: Convert last word to all upper case.
215: @item M-- M-c
216: Convert last word to lower case with capital initial.
217: @end table
218: 
219: @kindex M-@t{-} M-l
220: @kindex M-@t{-} M-u
221: @kindex M-@t{-} M-c
222:   A very common error is to type words in the wrong case.  Because of this,
223: the word case-conversion commands @kbd{M-l}, @kbd{M-u}, and @kbd{M-c} have a
224: special feature when used with a negative argument: they do not move the
225: cursor.  As soon as you see you have mistyped the last word, you can simply
226: case-convert it and go on typing.  @xref{Case}.
227: 
228: @node Spelling
229: @section Checking and Correcting Spelling
230: @cindex spelling, checking and correcting
231: @cindex checking spelling
232: @cindex correcting spelling
233: 
234:   This section describes the commands to check the spelling of a
235: single word or of a portion of a buffer.  These commands only work if
236: a spelling checker program, one of Hunspell, Aspell, Ispell or
237: Enchant, is installed.  These programs are not part of Emacs, but one
238: of them is usually installed on GNU/Linux and other free operating
239: systems.
240: @ifnottex
241: @xref{Top, Aspell,, aspell, The Aspell Manual}.
242: @end ifnottex
243: 
244: @table @kbd
245: @item M-$
246: Check and correct spelling of the word at point (@code{ispell-word}).
247: If the region is active, do it for all words in the region instead.
248: @item M-x ispell
249: Check and correct spelling of all words in the buffer.  If the region
250: is active, do it for all words in the region instead.
251: @item M-x ispell-buffer
252: Check and correct spelling in the buffer.
253: @item M-x ispell-region
254: Check and correct spelling in the region.
255: @item M-x ispell-message
256: Check and correct spelling in a draft mail message, excluding cited
257: material.
258: @item M-x ispell-change-dictionary @key{RET} @var{dict} @key{RET}
259: Restart the spell-checker process, using @var{dict} as the dictionary.
260: @item M-x ispell-kill-ispell
261: Kill the spell-checker subprocess.
262: @item M-@key{TAB}
263: @itemx @key{ESC} @key{TAB}
264: @itemx C-M-i
265: Complete the word before point based on the spelling dictionary
266: (@code{ispell-complete-word}).
267: @item M-x flyspell-mode
268: Enable Flyspell mode, which highlights all misspelled words.
269: @item M-x flyspell-prog-mode
270: Enable Flyspell mode for comments and strings only.
271: @end table
272: 
273: @kindex M-$
274: @findex ispell-word
275:   To check the spelling of the word around or before point, and
276: optionally correct it as well, type @kbd{M-$} (@code{ispell-word}).
277: If a region is active, @kbd{M-$} checks the spelling of all words
278: within the region.  @xref{Mark}.  (When Transient Mark mode is off,
279: @kbd{M-$} always acts on the word around or before point, ignoring the
280: region; @pxref{Disabled Transient Mark}.)
281: 
282: @findex ispell
283: @findex ispell-buffer
284: @findex ispell-region
285: @cindex spell-checking the active region
286:   Similarly, the command @kbd{M-x ispell} performs spell-checking in
287: the region if one is active, or in the entire buffer otherwise.  The
288: commands @kbd{M-x ispell-buffer} and @kbd{M-x ispell-region}
289: explicitly perform spell-checking on the entire buffer or the region
290: respectively.  To check spelling in an email message you are writing,
291: use @kbd{M-x ispell-message}; that command checks the whole buffer,
292: except for material that is indented or appears to be cited from other
293: messages.  @xref{Sending Mail}.
294: 
295:   When one of these commands encounters what appears to be an
296: incorrect word, it asks you what to do.  It usually displays a list of
297: numbered @dfn{near-misses}---words that are close to the incorrect word.
298: Then you must type a single-character response.  Here are the valid
299: responses:
300: 
301: @table @kbd
302: @item @var{digit}
303: Replace the word, just this time, with one of the displayed
304: near-misses.  Each near-miss is listed with a digit; type that digit
305: to select it.
306: 
307: @item @key{SPC}
308: Skip this word---continue to consider it incorrect, but don't change it
309: here.
310: 
311: @item r @var{new} @key{RET}
312: Replace the word, just this time, with @var{new}.  (The replacement
313: string will be rescanned for more spelling errors.)
314: 
315: @item R @var{new} @key{RET}
316: Replace the word with @var{new}, and do a @code{query-replace} so you
317: can replace it elsewhere in the buffer if you wish.  (The replacements
318: will be rescanned for more spelling errors.)
319: 
320: @item a
321: Accept the incorrect word---treat it as correct, but only in this
322: editing session.
323: 
324: @item A
325: Accept the incorrect word---treat it as correct, but only in this
326: editing session and for this buffer.
327: 
328: @item i
329: Insert this word in your private dictionary file so that it will be
330: considered correct from now on, even in future sessions.
331: 
332: @item m
333: Like @kbd{i}, but you can also specify dictionary completion
334: information.
335: 
336: @item u
337: Insert the lower-case version of this word in your private dic@-tion@-ary
338: file.
339: 
340: @item l @var{word} @key{RET}
341: Look in the dictionary for words that match @var{word}.  These words
342: become the new list of near-misses; you can select one of them as
343: the replacement by typing a digit.  You can use @samp{*} in @var{word} as a
344: wildcard.
345: 
346: @item C-g
347: @itemx X
348: Quit interactive spell-checking, leaving point at the word that was
349: being checked.  You can restart checking again afterward with @kbd{C-u
350: M-$}.
351: 
352: @item x
353: Quit interactive spell-checking and move point back to where it was
354: when you started spell-checking.
355: 
356: @item q
357: Quit interactive spell-checking and kill the spell-checker subprocess.
358: 
359: @item ?
360: Show the list of options.
361: @end table
362: 
363: @findex ispell-complete-word
364:   In Text mode and related modes, @kbd{M-@key{TAB}}
365: (@code{ispell-complete-word}) performs in-buffer completion based on
366: spelling correction.  Insert the beginning of a word, and then type
367: @kbd{M-@key{TAB}}; this shows a list of completions.  (If your
368: window manager intercepts @kbd{M-@key{TAB}}, type @kbd{@key{ESC}
369: @key{TAB}} or @kbd{C-M-i}.)  Each completion is listed with a digit or
370: character; type that digit or character to choose it.
371: 
372: @cindex @code{ispell} program
373: @findex ispell-kill-ispell
374:   Once started, the spell-checker subprocess continues
375: to run, waiting for something to do, so that subsequent spell-checking
376: commands complete more quickly.  If you want to get rid of the
377: process, use @kbd{M-x ispell-kill-ispell}.  This is not usually
378: necessary, since the process uses no processor time except when you do
379: spelling correction.
380: 
381: @vindex ispell-dictionary
382: @vindex ispell-local-dictionary
383: @vindex ispell-personal-dictionary
384: @findex ispell-change-dictionary
385:   Spell-checkers look up spelling in two dictionaries:
386: the standard dictionary and your personal dictionary.  The standard
387: dictionary is specified by the variable @code{ispell-local-dictionary}
388: or, if that is @code{nil}, by the variable @code{ispell-dictionary}.
389: If both are @code{nil}, the spelling program's default dictionary is
390: used.  The command @kbd{M-x ispell-change-dictionary} sets the
391: standard dictionary for the buffer and then restarts the subprocess,
392: so that it will use a different standard dictionary.  Your personal
393: dictionary is specified by the variable
394: @code{ispell-personal-dictionary}.  If that is @code{nil}, the
395: spelling program looks for a personal dictionary in a default
396: location, which is specific to each spell-checker.
397: 
398: @vindex ispell-complete-word-dict
399:   A separate dictionary is used for word completion.  The variable
400: @code{ispell-complete-word-dict} specifies the file name of this
401: dictionary.  The completion dictionary must be different because it
402: cannot use the information about roots and affixes of the words, which
403: spell-checking uses to detect variations of words.  For some
404: languages, there is a spell-checking dictionary but no word completion
405: dictionary.
406: 
407: @cindex Flyspell mode
408: @cindex mode, Flyspell
409: @findex flyspell-mode
410:   Flyspell mode is a minor mode that performs automatic spell-checking
411: of the text you type as you type it.  When it finds a word that it
412: does not recognize, it highlights that word.  Type @kbd{M-x
413: flyspell-mode} to toggle Flyspell mode in the current buffer.  To
414: enable Flyspell mode in all text mode buffers, add
415: @code{flyspell-mode} to @code{text-mode-hook}.  @xref{Hooks}.  Note
416: that, as Flyspell mode needs to check each word across which you move,
417: it will slow down cursor motion and scrolling commands.  It also
418: doesn't automatically check the text you didn't type or move across;
419: use @code{flyspell-region} or @code{flyspell-buffer} for that.
420: 
421: @findex flyspell-correct-word
422: @findex flyspell-auto-correct-word
423: @findex flyspell-correct-word-before-point
424:   When Flyspell mode highlights a word as misspelled, you can click on
425: it with @kbd{mouse-2} (@code{flyspell-correct-word}) to display a menu
426: of possible corrections and actions.  In addition, @kbd{C-.} or
427: @kbd{@key{ESC}-@key{TAB}} (@code{flyspell-auto-correct-word}) will
428: propose various successive corrections for the word at point, and
429: @kbd{C-c $} (@code{flyspell-correct-word-before-point}) will pop up a
430: menu of possible corrections.  Of course, you can always correct the
431: misspelled word by editing it manually in any way you like.
432: 
433: @findex flyspell-prog-mode
434:   Flyspell Prog mode works just like ordinary Flyspell mode, except
435: that it only checks words in comments and string constants.  This
436: feature is useful for editing programs.  Type @kbd{M-x
437: flyspell-prog-mode} to enable or disable this mode in the current
438: buffer.  To enable this mode in all programming mode buffers, add
439: @code{flyspell-prog-mode} to @code{prog-mode-hook} (@pxref{Hooks}).
440: