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

- 7,064
- 9
- 41
- 62
-
1Be aware that this could have the unintended side effect of leaving sensitive information lingering around on your machine, even after the originals are moved/deleted. It would be advisable to purge this backup folder occasionally. These backup files may also have different permissions than the originals. – nispio Sep 23 '14 at 22:00
-
**Related:** [How do I control how Emacs makes backup files?](http://stackoverflow.com/q/151945/183120) – legends2k Jun 26 '15 at 05:54
4 Answers
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.

- 5,048
- 26
- 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
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

- 668
- 4
- 8

- 3,899
- 2
- 17
- 19
;; put all backup files into ~/MyEmacsBackups
(setq backup-directory-alist '(("." . "~/MyEmacsBackups")))
(setq backup-by-copying t)

- 21,702
- 4
- 53
- 89

- 141
- 2
-
1What benefit does your answer have over shosti's? – Gilles 'SO- stop being evil' Sep 23 '14 at 22:00
-
@Gilles Not much, https://www.gnu.org/software/emacs/manual/html_node/emacs/Backup-Copying.html – Terry Shi Sep 23 '14 at 22:08
-
Also, file mounts in docker [doesn't work well](https://github.com/moby/moby/issues/15793#issuecomment-135411504) with `(not backup-by-copying)`. Actually I'm not really sure why one would want the default behavior. – x-yuri Jan 03 '22 at 18:55
Put this in your init file. Change the path to your chosen backup directory.
;;backup directory
(setq backup-directory-alist '(("" . "~/.emacs.d/emacs_backup")))

- 21,702
- 4
- 53
- 89

- 6,341
- 2
- 22
- 35
-
1What does the initial `""` do and why do the other answers have it as `"."`? – user129393192 Jul 19 '23 at 02:07