Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Cleaning up undo-tree

Tags:

undo

emacs

I have noticed that in long editing sessions, calling undo-tree (C-x C-u) becomes slower and slower. I presume that the reason is that as the tree grows bigger it takes longer to parse it (as it keeps track of all my edits since I opened the file)

Is there any way to clear up the tree of undos on an open file? I have tried C-x C-v (find-alternate-file), but this re-opens the file which resets the cursor (and in modes like org-mode collapses my lists)

like image 982
Amelio Vazquez-Reina Avatar asked Oct 04 '12 14:10

Amelio Vazquez-Reina


2 Answers

Reset the buffer-undo-tree variable using M-: (setq buffer-undo-tree nil). This discards the entire undo history of the buffer, as recorded by undo-tree.el.

This can also be made into a command and bound to a key with code like this:

(defun clear-undo-tree ()
  (interactive)
  (setq buffer-undo-tree nil))
(global-set-key [(control c) u] 'clear-undo-tree)
like image 59
user4815162342 Avatar answered Sep 18 '22 09:09

user4815162342


Note that this tip may be obsolete in recent undo-tree versions. From version 0.6 onwards, undo-tree uses "lazy tree-drawing" by default. This significantly speeds up visualization of large undo trees, by only drawing the visible portion (and extending it as necessary as you move around the tree). See the docstring for the undo-tree-visualizer-lazy-drawing variable for further details.

If you nonetheless want to discard the entire undo tree, manually setting buffer-undo-tree to nil is a good one-off solution. A longer-term solution is to reduce the values of undo-limit, undo-strong-limit, and undo-outer-limit to limit the undo tree size, by limiting how much undo history it stores before discarding older data.

Manually calling undo-tree-discard-history is pointless. It gets called automatically whenever you do anything undo-related.

like image 39
Toby Cubitt Avatar answered Sep 19 '22 09:09

Toby Cubitt