tags:

views:

385

answers:

4

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 buffer 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 buffer files when I do ls of the directory.

+4  A: 

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 weary of removing them as they can come in useful. Further discussion is here:

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

James Brooks
Thanks! Is there something similar for the files created with name #filename#?
zero4
changed in my answer.
James Brooks
Thanks a lot James! :)
zero4
When I use this, I get, "Symbol's value as variable is void: dotfiles-dir"
Chris Conway
the code is taken from the github link. to get it to work you need to replace the `(expand-file-name (concat dotfiles-dir "backups"))` part with whatever folder you want your files to go into. if you need more help try this answer: http://stackoverflow.com/questions/1199216/emacs-dont-create-these-files-when-not-saving-modified-buffer/1199335#1199335
James Brooks
So, I added this to my emacs file:(setq auto-save-file-name-transforms `((".*" ,"~/.emacs-backups/")))But emacs is creating files with name "##" in the specified directory. What am I doing wrong?
zero4
Awesome! The code in your update works perfectly! Thanks a lot James.
zero4
A: 

In your .emacs:

(setq make-backup-files nil)

Edit: If you're unfamiliar with the .emacs file, it's a file named .emacs that resides in your user $HOME directory. If you don't have one already, you can just create it and emacs will load it on startup.

Rob Hruska
Thanks! Is there something similar for the files created with name #filename#?
zero4
I'm not sure about that one. To be honest, I'd hesitate to remove them completely, because usually that means emacs was closed without saving a buffer. I usually save frequently, so if a #file# does exist, it means emacs/the machine crashed; I usually end up using it to recover a file.
Rob Hruska
A: 

Here is a link to the same question answered on SuperUser and my response. And a StackOverflow question entitled Emacs: Don’t create #these# files when not saving modified buffer

And for completeness, as stated by others; to stop the backup files being created put this in your .emacs

(setq make-backup-files nil)
kjfletch
+2  A: 

The following lines in ~/.emacs will put all of the auto-save and backup files in /tmp:

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))
Chris Conway