Emacs: Prevent autosave mess

Emacs will create backup files for you. By default, it will do so after every 300 characters you type. 1

It does so by creating a new file with the same filename as the one you were editing, but wrapping it in # characters.

So if you were editing mycoolfile.js, after changing 300 characters, you’d have a file named #mycoolfile.js# in that same directory.

This can get really annoying when dealing with version control.

Here are a few methods of dealing with those files to make things a bit more manageable:

1. Add [#]*[#] to your global .gitignore file #

You can do it on per repository basis, and that works, but only for the specific repo you added it to.

A better solution is to create a global .gitignore file and add

[#]*[#] to that file so they are ignored globally by git.

To do so, create a global .gitignore file (if you don’t have one already):

git config --global core.excludesfile ~/.gitignore

Then add the glob to that file:

echo "[#]*[#]" >> ~/.gitignore

You’ll no longer see those pesky #filename# files all over the place in git as untracked files.

However, this still allows them to be created all over the place which will drive some people mad.

2. Disable Autosave #

It’s easy to disable autosave globally. In your .emacs file, add the following line:

(setq auto-save-default nil)

That will shut it off entirely and you won’t see any more #file# files.

You can also disable it manually by typing M-x auto-save-mode.

You’ll see this command toggles it.

However, disabling autosave is not necessarily ideal as it’s a nice feature to have.

3. Move autosave files #

A nice option if you want to keep autosave enabled but move the files elsewhere, add the following to your .emacs file:

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

This will force autosave files to be created in your system temporary-file-directory.

To see where that is, use C-h v then type temporary-file-directory and hit enter.

If you want them stored elsewhere, swap ,temporary-file-directory for the file directory you’d like. For example, I use:

(setq backup-directory-alist
      `((".*" . "~/.saves")))
(setq auto-save-file-name-transforms
      `((".*" "~/.saves" t)))

My usage #

I actually use a combination of 1 and 3.

3 makes sure the files are moved while still allowing autosave to work its magic, and 1 acts as a failsafe in case something goes awry and they are created anyway. Helps keep my co-workers at SocialRadar from being annoyed by my accidentally committing them.

You should follow me on twitter here.

1. You can customize the autosave frequency by number of characters, or by a specified time interval.


Now read this

Deploy Coffeescript Node App to Heroku

So you’ve got a cool node app written in Coffeescript and you want to deploy it to Heroku? Seems like it should be easy these days, right? Not so much. Well, let’s be honest, we’re in the future, you don’t have to manually manage... Continue →