001: @c This is part of the Emacs manual.
002: @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2019 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: @item M-x transpose-sentences
153: Transpose two sentences (@code{transpose-sentences}).
154: @item M-x transpose-paragraphs
155: Transpose two paragraphs (@code{transpose-paragraphs}).
156: @item M-x transpose-regions
157: Transpose two regions.
158: @end table
159: 
160: @kindex C-t
161: @findex transpose-chars
162:   The common error of transposing two characters can be fixed, when they
163: are adjacent, with the @kbd{C-t} command (@code{transpose-chars}).  Normally,
164: @kbd{C-t} transposes the two characters on either side of point.  When
165: given at the end of a line, rather than transposing the last character of
166: the line with the newline, which would be useless, @kbd{C-t} transposes the
167: last two characters on the line.  So, if you catch your transposition error
168: right away, you can fix it with just a @kbd{C-t}.  If you don't catch it so
169: fast, you must move the cursor back between the two transposed
170: characters before you type @kbd{C-t}.  If you transposed a space with
171: the last character of the word before it, the word motion commands
172: (@kbd{M-f}, @kbd{M-b}, etc.) are a good way of getting there.
173: Otherwise, a reverse search (@kbd{C-r}) is often the best way.
174: @xref{Search}.
175: 
176: @kindex C-x C-t
177: @findex transpose-lines
178: @kindex M-t
179: @findex transpose-words
180: @c Don't index C-M-t and transpose-sexps here, they are indexed in
181: @c programs.texi, in the "List Commands" node.
182: @c @kindex C-M-t
183: @c @findex transpose-sexps
184:   @kbd{M-t} transposes the word before point with the word after point
185: (@code{transpose-words}).  It moves point forward over a word,
186: dragging the word preceding or containing point forward as well.  The
187: punctuation characters between the words do not move.  For example,
188: @w{@samp{FOO, BAR}} transposes into @w{@samp{BAR, FOO}} rather than
189: @w{@samp{BAR FOO,}}.  When point is at the end of the line, it will
190: transpose the word before point with the first word on the next line.
191: 
192: @findex transpose-sentences
193: @findex transpose-paragraphs
194:   @kbd{C-M-t} (@code{transpose-sexps}) is a similar command for
195: transposing two expressions (@pxref{Expressions}), and @kbd{C-x C-t}
196: (@code{transpose-lines}) exchanges lines.  @kbd{M-x
197: transpose-sentences} and @kbd{M-x transpose-paragraphs} transpose
198: sentences and paragraphs, respectively.  These commands work like
199: @kbd{M-t} except as regards the units of text they transpose.
200: 
201:   A numeric argument to a transpose command serves as a repeat count: it
202: tells the transpose command to move the character (or word or
203: expression or line) before or containing point across several other
204: characters (or words or expressions or lines).  For example, @kbd{C-u
205: 3 C-t} moves the character before point forward across three other
206: characters.  It would change @samp{f@point{}oobar} into
207: @samp{oobf@point{}ar}.  This is equivalent to repeating @kbd{C-t}
208: three times.  @kbd{C-u - 4 M-t} moves the word before point backward
209: across four words.  @kbd{C-u - C-M-t} would cancel the effect of plain
210: @kbd{C-M-t}.
211: 
212:   A numeric argument of zero is assigned a special meaning (because
213: otherwise a command with a repeat count of zero would do nothing): to
214: transpose the character (or word or expression or line) ending after
215: point with the one ending after the mark.
216: 
217: @findex transpose-regions
218:   @kbd{M-x transpose-regions} transposes the text between point and
219: mark with the text between the last two marks pushed to the mark ring
220: (@pxref{Setting Mark}).  With a numeric prefix argument, it transposes
221: the text between point and mark with the text between two successive
222: marks that many entries back in the mark ring.  This command is best
223: used for transposing multiple characters (or words or sentences or
224: paragraphs) in one go.
225: 
226: @node Fixing Case
227: @section Case Conversion
228: 
229: @table @kbd
230: @item M-- M-l
231: Convert last word to lower case.  Note @kbd{Meta--} is Meta-minus.
232: @item M-- M-u
233: Convert last word to all upper case.
234: @item M-- M-c
235: Convert last word to lower case with capital initial.
236: @end table
237: 
238: @kindex M-@t{-} M-l
239: @kindex M-@t{-} M-u
240: @kindex M-@t{-} M-c
241:   A very common error is to type words in the wrong case.  Because of this,
242: the word case-conversion commands @kbd{M-l}, @kbd{M-u}, and @kbd{M-c} have a
243: special feature when used with a negative argument: they do not move the
244: cursor.  As soon as you see you have mistyped the last word, you can simply
245: case-convert it and go on typing.  @xref{Case}.
246: 
247: @node Spelling
248: @section Checking and Correcting Spelling
249: @cindex spelling, checking and correcting
250: @cindex checking spelling
251: @cindex correcting spelling
252: 
253:   This section describes the commands to check the spelling of a
254: single word or of a portion of a buffer.  These commands only work if
255: a spelling checker program, one of Hunspell, Aspell, Ispell or
256: Enchant, is installed.  These programs are not part of Emacs, but one
257: of them is usually installed on GNU/Linux and other free operating
258: systems.
259: @ifnottex
260: @xref{Top, Aspell,, aspell, The Aspell Manual}.
261: @end ifnottex
262: 
263: @table @kbd
264: @item M-$
265: Check and correct spelling of the word at point (@code{ispell-word}).
266: If the region is active, do it for all words in the region instead.
267: @item M-x ispell
268: Check and correct spelling of all words in the buffer.  If the region
269: is active, do it for all words in the region instead.
270: @item M-x ispell-buffer
271: Check and correct spelling in the buffer.
272: @item M-x ispell-region
273: Check and correct spelling in the region.
274: @item M-x ispell-message
275: Check and correct spelling in a draft mail message, excluding cited
276: material.
277: @item M-x ispell-change-dictionary @key{RET} @var{dict} @key{RET}
278: Restart the spell-checker process, using @var{dict} as the dictionary.
279: @item M-x ispell-kill-ispell
280: Kill the spell-checker subprocess.
281: @item M-@key{TAB}
282: @itemx @key{ESC} @key{TAB}
283: @itemx C-M-i
284: Complete the word before point based on the spelling dictionary
285: (@code{ispell-complete-word}).
286: @item M-x flyspell-mode
287: Enable Flyspell mode, which highlights all misspelled words.
288: @item M-x flyspell-prog-mode
289: Enable Flyspell mode for comments and strings only.
290: @end table
291: 
292: @kindex M-$
293: @findex ispell-word
294:   To check the spelling of the word around or before point, and
295: optionally correct it as well, type @kbd{M-$} (@code{ispell-word}).
296: If a region is active, @kbd{M-$} checks the spelling of all words
297: within the region.  @xref{Mark}.  (When Transient Mark mode is off,
298: @kbd{M-$} always acts on the word around or before point, ignoring the
299: region; @pxref{Disabled Transient Mark}.)
300: 
301: @findex ispell
302: @findex ispell-buffer
303: @findex ispell-region
304: @cindex spell-checking the active region
305:   Similarly, the command @kbd{M-x ispell} performs spell-checking in
306: the region if one is active, or in the entire buffer otherwise.  The
307: commands @kbd{M-x ispell-buffer} and @kbd{M-x ispell-region}
308: explicitly perform spell-checking on the entire buffer or the region
309: respectively.  To check spelling in an email message you are writing,
310: use @kbd{M-x ispell-message}; that command checks the whole buffer,
311: except for material that is indented or appears to be cited from other
312: messages.  @xref{Sending Mail}.
313: 
314:   When one of these commands encounters what appears to be an
315: incorrect word, it asks you what to do.  It usually displays a list of
316: numbered @dfn{near-misses}---words that are close to the incorrect word.
317: Then you must type a single-character response.  Here are the valid
318: responses:
319: 
320: @table @kbd
321: @item @var{digit}
322: Replace the word, just this time, with one of the displayed
323: near-misses.  Each near-miss is listed with a digit; type that digit
324: to select it.
325: 
326: @item @key{SPC}
327: Skip this word---continue to consider it incorrect, but don't change it
328: here.
329: 
330: @item r @var{new} @key{RET}
331: Replace the word, just this time, with @var{new}.  (The replacement
332: string will be rescanned for more spelling errors.)
333: 
334: @item R @var{new} @key{RET}
335: Replace the word with @var{new}, and do a @code{query-replace} so you
336: can replace it elsewhere in the buffer if you wish.  (The replacements
337: will be rescanned for more spelling errors.)
338: 
339: @item a
340: Accept the incorrect word---treat it as correct, but only in this
341: editing session.
342: 
343: @item A
344: Accept the incorrect word---treat it as correct, but only in this
345: editing session and for this buffer.
346: 
347: @item i
348: Insert this word in your private dictionary file so that it will be
349: considered correct from now on, even in future sessions.
350: 
351: @item m
352: Like @kbd{i}, but you can also specify dictionary completion
353: information.
354: 
355: @item u
356: Insert the lower-case version of this word in your private dic@-tion@-ary
357: file.
358: 
359: @item l @var{word} @key{RET}
360: Look in the dictionary for words that match @var{word}.  These words
361: become the new list of near-misses; you can select one of them as
362: the replacement by typing a digit.  You can use @samp{*} in @var{word} as a
363: wildcard.
364: 
365: @item C-g
366: @itemx X
367: Quit interactive spell-checking, leaving point at the word that was
368: being checked.  You can restart checking again afterward with @kbd{C-u
369: M-$}.
370: 
371: @item x
372: Quit interactive spell-checking and move point back to where it was
373: when you started spell-checking.
374: 
375: @item q
376: Quit interactive spell-checking and kill the spell-checker subprocess.
377: 
378: @item ?
379: Show the list of options.
380: @end table
381: 
382: @findex ispell-complete-word
383:   In Text mode and related modes, @kbd{M-@key{TAB}}
384: (@code{ispell-complete-word}) performs in-buffer completion based on
385: spelling correction.  Insert the beginning of a word, and then type
386: @kbd{M-@key{TAB}}; this shows a list of completions.  (If your
387: window manager intercepts @kbd{M-@key{TAB}}, type @kbd{@key{ESC}
388: @key{TAB}} or @kbd{C-M-i}.)  Each completion is listed with a digit or
389: character; type that digit or character to choose it.
390: 
391: @cindex @code{ispell} program
392: @findex ispell-kill-ispell
393:   Once started, the spell-checker subprocess continues
394: to run, waiting for something to do, so that subsequent spell-checking
395: commands complete more quickly.  If you want to get rid of the
396: process, use @kbd{M-x ispell-kill-ispell}.  This is not usually
397: necessary, since the process uses no processor time except when you do
398: spelling correction.
399: 
400: @vindex ispell-dictionary
401: @vindex ispell-local-dictionary
402: @vindex ispell-personal-dictionary
403: @findex ispell-change-dictionary
404:   Spell-checkers look up spelling in two dictionaries:
405: the standard dictionary and your personal dictionary.  The standard
406: dictionary is specified by the variable @code{ispell-local-dictionary}
407: or, if that is @code{nil}, by the variable @code{ispell-dictionary}.
408: If both are @code{nil}, the spelling program's default dictionary is
409: used.  The command @kbd{M-x ispell-change-dictionary} sets the
410: standard dictionary for the buffer and then restarts the subprocess,
411: so that it will use a different standard dictionary.  Your personal
412: dictionary is specified by the variable
413: @code{ispell-personal-dictionary}.  If that is @code{nil}, the
414: spelling program looks for a personal dictionary in a default
415: location, which is specific to each spell-checker.
416: 
417: @vindex ispell-complete-word-dict
418:   A separate dictionary is used for word completion.  The variable
419: @code{ispell-complete-word-dict} specifies the file name of this
420: dictionary.  The completion dictionary must be different because it
421: cannot use the information about roots and affixes of the words, which
422: spell-checking uses to detect variations of words.  For some
423: languages, there is a spell-checking dictionary but no word completion
424: dictionary.
425: 
426: @cindex Flyspell mode
427: @cindex mode, Flyspell
428: @findex flyspell-mode
429:   Flyspell mode is a minor mode that performs automatic spell-checking
430: of the text you type as you type it.  When it finds a word that it
431: does not recognize, it highlights that word.  Type @kbd{M-x
432: flyspell-mode} to toggle Flyspell mode in the current buffer.  To
433: enable Flyspell mode in all text mode buffers, add
434: @code{flyspell-mode} to @code{text-mode-hook}.  @xref{Hooks}.  Note
435: that, as Flyspell mode needs to check each word across which you move,
436: it will slow down cursor motion and scrolling commands.  It also
437: doesn't automatically check the text you didn't type or move across;
438: use @code{flyspell-region} or @code{flyspell-buffer} for that.
439: 
440: @findex flyspell-correct-word
441: @findex flyspell-auto-correct-word
442: @findex flyspell-correct-word-before-point
443:   When Flyspell mode highlights a word as misspelled, you can click on
444: it with @kbd{mouse-2} (@code{flyspell-correct-word}) to display a menu
445: of possible corrections and actions.  In addition, @kbd{C-.} or
446: @kbd{@key{ESC}-@key{TAB}} (@code{flyspell-auto-correct-word}) will
447: propose various successive corrections for the word at point, and
448: @kbd{C-c $} (@code{flyspell-correct-word-before-point}) will pop up a
449: menu of possible corrections.  Of course, you can always correct the
450: misspelled word by editing it manually in any way you like.
451: 
452: @findex flyspell-prog-mode
453:   Flyspell Prog mode works just like ordinary Flyspell mode, except
454: that it only checks words in comments and string constants.  This
455: feature is useful for editing programs.  Type @kbd{M-x
456: flyspell-prog-mode} to enable or disable this mode in the current
457: buffer.  To enable this mode in all programming mode buffers, add
458: @code{flyspell-prog-mode} to @code{prog-mode-hook} (@pxref{Hooks}).
459: