13

It looks like this can be achieved by modifying the variable undo-tree-history-directory-alist, but I can't figure out how. I tried a number of lines, the last of which is

(setq undo-tree-history-directory-alist ("." ! "~/.emacs.d/undo"))

But this returns errors.

3 Answers 3

15

You most certainly want:

(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))

The docstring of this variable speaks of an alist aka association list. These are a list of pairs, where a pair is a cons cell. The short-hand syntax for a cons cell is (foo . 1) whereas the short-hand syntax for returning a list as is would be '(...), both combined result in '((foo . 1) ...)).

2
  • This gets rid of the error messages, but I see no undo files in the directory.
    – Toothrot
    Sep 11, 2016 at 11:23
  • 2
    Maybe you need to pass that path through expand-file-name () first.
    – VanLaser
    Sep 11, 2016 at 13:32
5

Here's the answer from @wasamasa, but in the use-package format:

(use-package undo-tree
  :defer t
  :diminish undo-tree-mode
  :init (global-undo-tree-mode)
  :custom
  (undo-tree-visualizer-diff t)
  (undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))
  (undo-tree-visualizer-timestamps t))

This worked for me as is, no file expansion needed.

1
  • use-package's :init section code runs before the package is loaded. The function global-undo-tree-mode is defined in undo-tree.el. Wouldn't the call fail?
    – legends2k
    Mar 26, 2022 at 10:43
1

I store the directory path in a variable and use it also to create the directory if it doesn't exist:

  (defvar --undo-history-directory (concat user-emacs-directory "undos/")
    "Directory to save undo history files.")
  (unless (file-exists-p --undo-history-directory)
    (make-directory --undo-history-directory t))
  ;; stop littering with *.~undo-tree~ files everywhere
  (setq undo-tree-history-directory-alist `(("." . ,--undo-history-directory)))

While this was inspired from the other answers, I'm putting it here since I stumbled around the quoting since --undo-history-directory is a variable and not a string literal.

For those interested, we're using the backquote construct to selectively evaluate elements in a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.