I always notice this file that duplicates the file I'm editing whenever I use Emacs:
duplicated file
What code should I put in my ~/.emacs file to avoid this?
Those are backups, if you want to disable them add
(setq make-backup-files nil)
Alternatively, you can keep backups but store them all in a specific location by customizing backup-directory-alist, eg
(setq backup-directory-alist '(("" . "your/backup/directory")))
Related
When Emacs is closed with modified buffers present, it creates a file with the format '#file_name#' for recovery purpose in its parent directory for each modified buffer (except for scratch).
When recover-file command is used when Emacs opens the next time, the previously modified buffer of the file is recovered. Even so the file #file_name# is not deleted automatically.
This would not occur if you manually kill all buffers before closing. This is a bit tedious as you would have to use the command kill-some-buffer or kill-matching buffer and say 'yes' to each of the prompts one by one.
Is there a simpler method to overcome this? It would be nice to have solutions for one or more of the following.
Prevent Emacs from creating a recovery file for modified buffer while closing
A simple command to force kill all buffers without prompt to save
Setting to re-route the recovery files to a different location (like ~/.emacs.d/)
(Versions: Emacs-24 on Ubuntu-12.04 / OS-X-10.9)
By adding the following line on your init.el file you'll disable the auto-save feature and no more of these files will be generated
(setq auto-save-default nil)
But you may want those files so you can use the code above which I borrowed from emacswiki
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
This will cause that all backup files will go to the temporary folder on your operating system. Check the emacswiki link to know more about what you can do with this feature
More on this
Emacs manual entry on auto-save
Emacs puts backup files named foo~ everywhere and I don't like having to remember to delete them. Also, if I edit a file that has a hard link somewhere else in the file system, the hard link points to the backup when I'm done editing, and that's confusing and awful. How can I either eliminate these backup files, or have them go somewhere other than the same directory?
If you've ever been saved by an Emacs backup file, you
probably want more of them, not less of them. It is annoying
that they go in the same directory as the file you're editing,
but that is easy to change. You can make all backup files go
into a directory by putting something like the following in your
.emacs.
(setq backup-directory-alist `(("." . "~/.saves")))
There are a number of arcane details associated with how Emacs
might create your backup files. Should it rename the original
and write out the edited buffer? What if the original is linked?
In general, the safest but slowest bet is to always make backups
by copying.
(setq backup-by-copying t)
If that's too slow for some reason you might also have a look at
backup-by-copying-when-linked.
Since your backups are all in their own place now, you might want
more of them, rather than less of them. Have a look at the Emacs
documentation for these variables (with C-h v).
(setq delete-old-versions t
kept-new-versions 6
kept-old-versions 2
version-control t)
Finally, if you absolutely must have no backup files:
(setq make-backup-files nil)
It makes me sick to think of it though.
Emacs backup/auto-save files can be very helpful. But these features are confusing.
Backup files
Backup files have tildes (~ or ~9~) at the end and shall be written to the user home directory. When make-backup-files is non-nil Emacs automatically creates a backup of the original file the first time the file is saved from a buffer. If you're editing a new file Emacs will create a backup the second time you save the file.
No matter how many times you save the file the backup remains unchanged. If you kill the buffer and then visit the file again, or the next time you start a new Emacs session, a new backup file will be made. The new backup reflects the file's content after reopened, or at the start of editing sessions. But an existing backup is never touched again. Therefore I find it useful to created numbered backups (see the configuration below).
To create backups explicitly use save-buffer (C-x C-s) with prefix arguments.
diff-backup and dired-diff-backup compares a file with its backup or vice versa. But there is no function to restore backup files. For example, under Windows, to restore a backup file
C:\Users\USERNAME\.emacs.d\backups\!drive_c!Users!USERNAME!.emacs.el.~7~
it has to be manually copied as
C:\Users\USERNAME\.emacs.el
Auto-save files
Auto-save files use hashmarks (#) and shall be written locally within the project directory (along with the actual files). The reason is that auto-save files are just temporary files that Emacs creates until a file is saved again (like with hurrying obedience).
Before the user presses C-x C-s (save-buffer) to save a file Emacs auto-saves files - based on counting keystrokes (auto-save-interval) or when you stop typing (auto-save-timeout).
Emacs also auto-saves whenever it crashes, including killing the Emacs job with a shell command.
When the user saves the file, the auto-saved version is deleted. But when the user exits the file without saving it, Emacs or the X session crashes, the auto-saved files still exist.
Use revert-buffer or recover-file to restore auto-save files. Note that Emacs records interrupted sessions for later recovery in files named ~/.emacs.d/auto-save-list. The recover-session function will use this information.
The preferred method to recover from an auto-saved filed is M-x revert-buffer RET. Emacs will ask either "Buffer has been auto-saved recently. Revert from auto-save file?" or "Revert buffer from file FILENAME?". In case of the latter there is no auto-save file. For example, because you have saved before typing another auto-save-intervall keystrokes, in which case Emacs had deleted the auto-save file.
Auto-save is nowadays disabled by default because it can slow down editing when connected to a slow machine, and because many files contain sensitive data.
Configuration
Here is a configuration that IMHO works best:
(defvar --backup-directory (concat user-emacs-directory "backups"))
(if (not (file-exists-p --backup-directory))
(make-directory --backup-directory t))
(setq backup-directory-alist `(("." . ,--backup-directory)))
(setq make-backup-files t ; backup of a file the first time it is saved.
backup-by-copying t ; don't clobber symlinks
version-control t ; version numbers for backup files
delete-old-versions t ; delete excess backup files silently
delete-by-moving-to-trash t
kept-old-versions 6 ; oldest versions to keep when a new numbered backup is made (default: 2)
kept-new-versions 9 ; newest versions to keep when a new numbered backup is made (default: 2)
auto-save-default t ; auto-save every buffer that visits a file
auto-save-timeout 20 ; number of seconds idle time before auto-save (default: 30)
auto-save-interval 200 ; number of keystrokes between auto-saves (default: 300)
)
Sensitive data
Another problem is that you don't want to have Emacs spread copies of files with sensitive data. Use this mode on a per-file basis. As this is a minor mode, for my purposes I renamed it sensitive-minor-mode.
To enable it for all .vcf and .gpg files, in your .emacs use something like:
(setq auto-mode-alist
(append
(list
'("\\.\\(vcf\\|gpg\\)$" . sensitive-minor-mode)
)
auto-mode-alist))
Alternatively, to protect only some files, like some .txt files, use a line like
// -*-mode:asciidoc; mode:sensitive-minor; fill-column:132-*-
in the file.
The accepted answer is
good, but it can be greatly improved by additionally backing up on
every save and backing up versioned files.
First, basic settings as described in the accepted
answer:
(setq version-control t ;; Use version numbers for backups.
kept-new-versions 10 ;; Number of newest versions to keep.
kept-old-versions 0 ;; Number of oldest versions to keep.
delete-old-versions t ;; Don't ask to delete excess backup versions.
backup-by-copying t) ;; Copy all files, don't rename them.
Next, also backup versioned files, which Emacs does not do by default
(you don't commit on every save, right?):
(setq vc-make-backup-files t)
Finally, make a backup on each save, not just the first. We make two
kinds of backups:
per-session backups: once on the first save of the buffer in each
Emacs session. These simulate Emac's default backup behavior.
per-save backups: once on every save. Emacs does not do this by
default, but it's very useful if you leave Emacs running for a long
time.
The backups go in different
places and Emacs
creates the backup dirs automatically if they don't exist:
;; Default and per-save backups go here:
(setq backup-directory-alist '(("" . "~/.emacs.d/backup/per-save")))
(defun force-backup-of-buffer ()
;; Make a special "per session" backup at the first save of each
;; emacs session.
(when (not buffer-backed-up)
;; Override the default parameters for per-session backups.
(let ((backup-directory-alist '(("" . "~/.emacs.d/backup/per-session")))
(kept-new-versions 3))
(backup-buffer)))
;; Make a "per save" backup on each save. The first save results in
;; both a per-session and a per-save backup, to keep the numbering
;; of per-save backups consistent.
(let ((buffer-backed-up nil))
(backup-buffer)))
(add-hook 'before-save-hook 'force-backup-of-buffer)
I became very interested in this topic after I wrote $< instead of
$# in my Makefile, about three hours after my previous commit :P
The above is based on an Emacs Wiki page I heavily
edited.
Another way of configuring backup options is via the Customize interface. Enter:
M-x customize-group
And then at the Customize group: prompt enter backup.
If you scroll to the bottom of the buffer you'll see Backup Directory Alist. Click Show Value and set the first entry of the list as follows:
Regexp matching filename: .*
Backup directory name: /path/to/your/backup/dir
Alternatively, you can turn backups off my setting Make Backup Files to off.
If you don't want Emacs to automatically edit your .emacs file you'll want to set up a customisations file.
You can disable them altogether by
(setq make-backup-files nil)
(setq delete-auto-save-files t)
deletes buffer's auto save file when it is saved or when killed with no changes in it. thus you still get some of the safety of auto save files, since they are only left around when a buffer is killed with unsaved changes or if emacs exits unexpectedly. if you want to do this, but really need history on some of your files, consider putting them under source control.
EDIT: It turns out that the second edit to my .emacs file actually works. (See the comments below this entry.)
I tried a couple of addition to the .emacs to make all txt files opened in emacs use orgmode. They did not work. How can I make it happen?
;;SET EMACS AS DEFAULT MAJOR MODE TO FOR ALL FILES WITH AN UNSPECIFIED MODE
(setq default-major-mode 'org-mode)
;;OPEN ALL TXT FILES IN ORGMODE
(add-to-list 'auto-mode-alist '("\\.txt$" . org-mode))
Additionally:
It would be even better to open only txt files in a certain directory orgmode. Any hint as to how that could be done would also be appreciated.
Another way to do this is using directory-local variables. This is nice because you can put a file in any directory where you want this behavior to engage, and it works recursively in any subdirectories.
Create a file called .dir-locals.el in the desired directory.
Here are the contents:
((nil (eval . (if (string-match ".txt$" (buffer-file-name))(org-mode)))))
Read this like so: for any major-mode (nil), evaluate the following form:
(if .... (org-mode))
The regex in auto-mode-alist could be something more complex, like "^/path/to/.*\\.txt$"
You can implement a hook which verifies the file directory and modifies the buffer mode:
(add-hook 'find-file-hooks
(lambda ()
(let ((file (buffer-file-name)))
(when (and file (equal (file-name-directory file) "c:/temp/"))
(org-mode)))))
As an alternative you can add the mode line in the beginning of your text file. In this case emacs will set the specified mode.
; -*- mode: org;-*-
* header 1
** header 2
I glued together some code from Oleg Pavliv's answer here, and from yibe's at elisp - File extension hook in Emacs - Stack Overflow
(defun use-org-mode-for-dot-txt-files-in-owncloud ()
(when (and (string-match owncloud buffer-file-name)
(string-match "\\.txt\\'" buffer-file-name))
(org-mode)))
(add-hook 'find-file-hook 'use-org-mode-for-dot-txt-files-in-owncloud)
This way, though ownCloud Web and phone apps are currently friendly only with .txt files, from my PC I can use Emacs' Org-mode for them.
(If I set all .txt files to use Org-mode, it breaks todotxt-mode.)
(Note that owncloud is a string variable equal to my ownCloud path.)
I am trying to create a backup for file after-save and before-save with the help of save hooks. Here is a code from .emacs
(defun force-backup-of-buffer ()
(interactive)
(setq buffer-backed-up nil)
(backup-buffer)
)
(add-hook 'before-save-hook 'force-backup-of-buffer)
(add-hook 'after-save-hook 'force-backup-of-buffer)
But this leads to deletion of original file. Can someone tell me why this happens?
My goal is to create two backup file everytime I save. I want to use same version control numbers as used by emacs. Hence I am using backup-buffer and not written my own hook to write file.
Check the documentation for backup-buffer, which points you to the make-backup-files variable: C-hv make-backup-files RET
By default, Emacs backs up by renaming the original file to the backup filename before saving the buffer to the original filename; hence your file disappearing.
I'm not sure that backup-buffer is really intended to be called elsewhere, however if you set it to backup by copying, you'll probably be okay.
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.
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
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)))
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.
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)