Relocate Emacs backup files (tilde ~)

Are you tired of that anyoying emacs tilde ~ file that gets created where ever you edit a file? If so here is your answer to relocating the emacs tilde file.

Your first step is to create a new directory called “emacs_backups.” Now I made my directory hidden but you do not have to, in the example I will create the backup directory as a hidden directory under my users home.
mkdir ~/.emacs_backups

Then we need to edit the .emacs file, this file is located in your home directory but is not shown with a normal “ls -l” command. You will need to do an “ls  -la” as the .emacs file is hidden. Once you have confirmed the file is there edit it with your favorite editor (most likely emacs).
emacs .emacs

Add the following to the end of your “.emacs” file:
;; create a backup file directory
(defun make-backup-file-name (file)
(concat “~/.emacs_backups/” (file-name-nondirectory file) “~”))

Save and close the file. Next time you open emacs you will notice that your files are being saved to the new hidden “emacs_backups” folder located in your home directory.

4 thoughts on “Relocate Emacs backup files (tilde ~)

  1. OzgurH

    Very useful tip! Especially if you make minor changes using emacs to your “live” scripts (php, etc) on public pages of your website (even though that’s against recommended work-flow for site maintenance). Possibly sensitive information in your scripts would be visible to all who are “curious enough” to check the existence of these tilde files (as web server most likely would process them as if they are typical text files).

    One thing to note though: In case you use this HTML page to copy-paste the changes (especially over SSH for example) the special (Unicode) inward-slanted double quotes above may be replaced with placeholder characters. Make sure that they are regular double quotes in your .emacs file.

    Thanks,

    Özgür

  2. Adeodato Simó

    This can be also achieved with:

    (setq backup-directory-alist
    ‘((“.” . “~/.emacs_backups”)))

    A benefit of this (though somebody might not want it) is that /home/you/proj1/Makefile and /home/you/proj2/Makefile are backuped to !home!you!proj1!Makefile~, and the same for proj2, instead of Makefile~ for both. This is handy.

  3. Conor

    Thanks, great tip. Comments were very useful too – I had copied and pasted, and needed to replace incorrect quotes, and I like having the full path in backup name.

  4. Trevor

    Thanks for the HTML tip. That’s an easy one to forget.

    Great tip – those tilda files are invaluable but man can they ever clutter up a directory. Rather is how could I simply rename the files so they are hidden? That seems to be an more straight forward solution (i.e. put . in front of each file)
    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*