When you enter the command C-/, Emacs undoes some part of your recent changes to a buffer. When you enter C-/ again, it undoes another chunk of work.
I have read the Emacs manual entry on Undo but it is vague about exactly how it works. The manual says "Consecutive character insertion commands are usually grouped together into a single undo record" but it does not explain how it decides the number of character insertion commands that constitute a group. The number of characters it puts in a group seems random.
Can anyone explain the algorithm Emacs uses to group characters into undo records?
The command C-x u or C-_ is how you undo. The first time you give this command, it undoes the last change.
Just move the cursor in any direction, and type C-x u again. Emacs redoes the last command you undid. You can repeat it to redo previous undos. Although undo is an important command, it can be slow if you want to undo a large number of changes.
By default, redo in Emacs requires pressing C-g, then undo.
Buffers in Emacs editing are objects that have distinct names and hold text that can be edited. Buffers appear to Lisp programs as a special data type. You can think of the contents of a buffer as a string that you can extend; insertions and deletions may occur in any part of the buffer.
The logic for setting undo boundaries is mostly in self-insert-command
which is implemented in cmds.c
. You'll have to read the code for the full story, but basically:
internal_self_insert
) cause an an undo boundary to be added immediately, and the character count to be reset. Reading the code, it looks as though these are: (1) in overwrite-mode
, if you overwrote a character with one that has a different width, e.g. typing over a tab; (2) if the character you inserted caused an abbreviation to be expanded; (3) if the character you typed caused auto-fill-mode
to insert indentation.undo-boundary
. This does not cause the character count to be reset.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With