22

How can I set up Emacs so that all backups are placed into one backup folder? e.g. /MyEmacsBackups

programking
  • 7,194
  • 10
  • 42
  • 63

3 Answers3

25

Check out backup-directory-alist, which allows you to set backup locations by file regexp. To have everything go to one directory, try something like:

(setq backup-directory-alist '(("." . "~/MyEmacsBackups")))

For the truly paranoid (like myself), there's also backup-each-save, which (as the name suggests) backs up your files each time they're saved in a convenient location. This gives an extra layer of protection over traditional version control, for instance for those cases when you accidentally clear your working directory without checking something in.

shosti
  • 5,058
  • 27
  • 33
  • I have done this but ls -a ~/MyEmacsBackups keep returning 0 file , what might be the reason ? – alper Oct 15 '20 at 22:30
21

The following is a quick code from my .emacs. It does not only put backups into a specific directory, but also auto-saves, and does the same for Tramp files so those are not put onto the remote system.

;; Put backup files neatly away
(let ((backup-dir "~/tmp/emacs/backups")
      (auto-saves-dir "~/tmp/emacs/auto-saves/"))
  (dolist (dir (list backup-dir auto-saves-dir))
    (when (not (file-directory-p dir))
      (make-directory dir t)))
  (setq backup-directory-alist `(("." . ,backup-dir))
        auto-save-file-name-transforms `((".*" ,auto-saves-dir t))
        auto-save-list-file-prefix (concat auto-saves-dir ".saves-")
        tramp-backup-directory-alist `((".*" . ,backup-dir))
        tramp-auto-save-directory auto-saves-dir))

(setq backup-by-copying t ; Don't delink hardlinks delete-old-versions t ; Clean up the backups version-control t ; Use version numbers on backups, kept-new-versions 5 ; keep some new versions kept-old-versions 2) ; and some old ones, too

Y. E.
  • 767
  • 5
  • 9
Jorgen Schäfer
  • 3,959
  • 2
  • 19
  • 19
4
;; put all backup files into ~/MyEmacsBackups
(setq backup-directory-alist '(("." . "~/MyEmacsBackups")))
(setq backup-by-copying t)
Terry Shi
  • 141
  • 2