Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a backup of .emacs every time Emacs starts

What is a good way to take a backup of my .emacs file each time Emacs starts? I want to keep multiple copies for when I need to get back to a previous version.

My first thought is to issue a shell command from within the .emacs file:

cp ~/.emacs ~/Backups/.emacs-yyyymmdd:hhmmss

... appending the current timestamp to get a unique filename. But as far as I know you can't issue shell commands from the .emacs file.

I've read about BackupEachSave and ForceBackups. Does anyone have experience with these? Do they work well?

EDIT:
Event_jr's answer about version control is a possible solution. I prefer using a shell command, though, because version control applies to all files and I don't need multiple backups of every single file.

I looked at the 'version control' variable. It's described in the Emacs manual:

Emacs can also make numbered backup files. Numbered backup file names contain ‘.~’, the >number, and another ‘~’ after the original file name. Thus, the backup files of eval.c >would be called eval.c.~1~, eval.c.~2~, and so on, all the way through names like eval.c.~259~ >and beyond.

The variable version-control determines whether to make single backup files or multiple >numbered backup files.

So, I added this to my .emacs:

; Version control and backups:
(setq version-control t)    

Works as advertised.

This section tells how to control backups on a per-file basis. I haven't explored it.

like image 380
user11583 Avatar asked Dec 21 '22 01:12

user11583


2 Answers

The question you should really be asking is how do I never lose a revision of any file I edit in Emacs, including ~/.emacs?

The answer is versioned backups. The variable that controls this feature is called version-control, which is kind of confusing, as it relates completely to backups, not VCS.

This is also a feature of Emacs; there is no additional package to install. Almost everything I work on is in VCS, but I still find it extremely useful to have all revisions of my work easily accessible. Storage is so cheap, so why not?

EDIT: describe the save-buffer aspect of backup every file.

You should read the documentation (C-h k C-x C-s) of save-buffer to understand the nuances, but basically passing it C-u C-u will force it to backup after every save. I actaully remap it to my own function

(defun le::save-buffer-force-backup (arg)
  "save buffer, always with a 2 \\[universal-argument]'s

see `save-buffer'

With ARG, don't force backup.
"
  (interactive "P")
  (if (consp arg)
      (save-buffer)
    (save-buffer 16)))
(global-set-key [remap save-buffer] 'le::save-buffer-force-backup)
like image 194
event_jr Avatar answered Dec 27 '22 09:12

event_jr


as far as I know you can't issue shell commands from the .emacs file.

Sure you can:

(shell-command "cp ~/.emacs ~/.emacs-`date +%Y%m%d:%H%M`")
like image 27
phils Avatar answered Dec 27 '22 11:12

phils