3

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!

  • Hi Joe, Welcome to stack overflow. This question is off-topic here, because it's not programming related. In a few minutes it will be closed. You will have much better luck if you ask this quest at SuperUser.com or Unix.stackexchange.com – Alan Jan 23 '11 at 21:12
  • The file would be named foo.c~ not ~foo.c. – ocodo Jan 23 '11 at 23:24
  • @Alan: It is not a good idea to recommend people to repost their questions on a different site. After it is closed it will be migrated automatically. – Wuffers Jan 24 '11 at 0:27
  • @Mark: Thanks, I knew that stack use to do that, but the text for "Off Topic Close" no longer mentions it, so I wasn't sure, but now I know for certain! – Alan Jan 24 '11 at 1:41
  • Just a reminder, if you use dired (c-x d) you can remove backups and autosaves by pressing ~ and/or # followed by x – johnny Jan 13 '17 at 6:21
6

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
4

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
2

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.

  • 1
    Since we're correcting the use of Ctrl+XC (which I'm sure the OP meant to mean Ctrl for both) - C-x C-c is the proper notation, also this just closes Emacs, it doesn't save and close. You need to do C-x C-s, C-x C-c to save and close. – ocodo Jan 23 '11 at 23:12
  • 1
    There is no need to let go of Ctrl while doing C-x C-c. – Rene Saarsoo Jan 26 '11 at 23:28
1

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

0

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: http://www.rpi.edu/dept/acs/rpinfo/common/Computing/Consulting/Software/Emacs/Hints/backup.html

  • I would expect backup file names of the format foo.c~. Has it been changed lately? – maxelost Jan 23 '11 at 23:22

Your Answer

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