12

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.

Toothrot
  • 3,204
  • 1
  • 12
  • 30

3 Answers3

13

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) ...)).

wasamasa
  • 21,803
  • 1
  • 65
  • 97
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.

Ashton Honnecke
  • 151
  • 1
  • 5
  • `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 '22 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.

legends2k
  • 207
  • 3
  • 11