Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I hide the backup files that emacs creates?

Tags:

emacs

I just started using emacs after having used vi for a long time. :)

One thing which is annoying me is that whenever I modify a file, save it and exit emacs, I see a backup file created in the same directory named filename~ (if the file I edited was filename).

Is there any way I can get rid of this? Or hide these files? It is very annoying to see tons of backup files when I do ls of the directory.

like image 452
n1kh1lp Avatar asked Jan 07 '10 14:01

n1kh1lp


People also ask

How do I hide backup files?

Right-click on the File/Folder that you want to hide and then click on Properties option in the menu that appears. 2. In the File Properties window, scroll down to the “Attributes” section and then check the little box next to Hidden and click on Apply (See image below).

How do I stop Emacs from making files?

This can be done by renaming the file or by copying. Renaming means that Emacs renames the existing file so that it is a backup file, then writes the buffer into a new file.

Does emacs auto save?

Emacs periodically saves all files that you are visiting; this is called auto-saving. Auto-saving prevents you from losing more than a limited amount of work if the system crashes. By default, auto-saves happen every 300 keystrokes, or after around 30 seconds of idle time.


1 Answers

You can either move them to their own folder with the following code:

;; Don't clutter up directories with files~
(setq backup-directory-alist `(("." . ,(expand-file-name
                                    (concat dotfiles-dir "backups")))))

;; Don't clutter with #files either
(setq auto-save-file-name-transforms
      `((".*" ,(expand-file-name (concat dotfiles-dir "backups")))))

Or you can remove them completely, like so:

(setq make-backup-files nil)
(setq auto-save-default nil)

Personally I would be wary of removing them as they can come in useful. Further discussion is here:

  • http://www.emacswiki.org/emacs/BackupDirectory
  • http://www.emacswiki.org/emacs/AutoSave

I would recommend checking out the emacs-starter-kit it sorts out a load of issues that people have when coming to emacs, and is pretty heavily used.

http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-misc.el


Update:

There seems to be much confusion over how to use the functions. I'm going to have a little play around later but here is some more information. Note that auto-save-file-name-transforms:

lets you specify a series of regular expressions and replacements to transform the auto save file name [emacs-manual]

so it's not just as simple as adding in a folder name. That said it seems from a quick google search the following might just do what you all want:

;;; backup/autosave
(defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
(defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
(setq backup-directory-alist (list (cons ".*" backup-dir)))
(setq auto-save-list-file-prefix autosave-dir)
(setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))

http://www.google.com/codesearch?hl=en&lr=&q=auto-save-file-name-transforms&sbtn=Search

like image 138
James Brooks Avatar answered Oct 04 '22 05:10

James Brooks