Why does Emacs create a file that starts with ~?

I'm new to Unix. Whenever I exit the Emacs editor after editing a file say foo.c by pressing CTRL+X+C, I see a new file automatically created called ~foo.c. What is it and why is it automatically created?

Please help me... Thank you!

5

5 Answers

You can disable backups, but that's a bad idea for obvious reasons. If you dislike seeing them alongside the current file, you can tell Emacs to store them somewhere else.

I use the following in my ~/.emacs.d/init.el file:

;; Write backups to ~/.emacs.d/backup/
(setq backup-directory-alist '(("." . "~/.emacs.d/backup")) backup-by-copying t ; Don't de-link hard links version-control t ; Use version numbers on backups delete-old-versions t ; Automatically delete excess backups: kept-new-versions 20 ; how many of the newest versions to keep kept-old-versions 5) ; and how many of the old

They are backup files, so you can roll back to the previous version of the file if you need to.

If they bother you, just clean them up periodically with:

find . -name "*~" -depth 1 -delete

This will clear all the *~ files found in the current directory, remove -depth 1 if you want to clean them recursively in all sub-directories too, of course, any file ending with ~ will get killed, so apply caution.

Of course, you can tell Emacs to stop making them if you want, just add this line to your .emacs file.

(setq make-backup-files nil) ;; do not make backup files

What you're seeing is a temporary file created when editing the file.

The proper way to save and exit in emacs is Ctrl-x Ctrl-c (I don't believe you let go of Ctrl in between the x and c) Please see here for more emacs help.

2

This is a backup file created automatically by emacs. Don't worry.

When you save a file in Emacs, it automatically creates a backup file (what the file looked like before editing) with the “~” prefix.

If you don’t want this, see:

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like