Turning On Undo in a Temporary Buffer

Today I learned something new from a short post on Xah Lee's blog. The idea is that you've written a command that generates output in a temporary buffer and you want to enable the undo function for the buffer so that the user can edit the buffer with undo capabilities.

You can't link to individual posts on Lee's blog but you can search for the date of the post, which is 2014-05-30. Here's the code from that post in case you have trouble finding the post:

(with-output-to-temp-buffer outputBuffer
  (switch-to-buffer outputBuffer)
  (setq buffer-undo-list nil )          ; enable undo

As you can see, it's merely a matter of providing an empty buffer-undo-list for the buffer. This isn't the sort of thing you're going to need very often and it would probably be pretty hard to find it in the documentation1. So this post is mainly a note to myself although I hope it might help others who didn't know this fact.



Well, maybe not. As Lee points out, you can find the information by looking at the undo node in the Elisp manual.

This entry was posted in Programming and tagged . Bookmark the permalink.
  • I just do "M-x buffer-enable-undo" when I want to turn it on. My most common use case is in the "*Pp Eval Output*" buffer when I want to manipulate the result expression.

  • Phil

    Likewise, for code readability I think (buffer-enable-undo &optional buffer-or-name) is slightly better for readability.

    • Phil

      ...and unintentional repetition of words is slightly worse :)