How to back up .emacs every time you start Emacs

What is a good way to back up my .emacs file every time Emacs starts? I want to save multiple copies when I need to revert to the previous version.

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

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

... adding the current timestamp to get a unique file name. But as far as I know, you cannot issue shell commands from a .emacs file.

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

EDIT:
The response to event_jr about version control is a possible solution. However, I prefer to use the shell command because version control applies to all files, and I do not need multiple backups of each individual file.

I looked at the variable "control version". This is described in the Emacs manual:

Emacs can also create numbered backup files. The names of the numbered backup files contain ". ~, The> number and more" after the name of the source file. Thus, the backup eval.c> files will call eval.c. ~ 1 ~, eval.c. ~ 2 ~, etc., all this happens through names like eval.c. ~ 259 ~> and beyond.

The version-control variable determines whether to create single backup files or multiple backup files with numbered files.

So, I added this to my .emacs:

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

It works as advertised.

This section describes how to manage backups for each file. I have not studied it.

+4
source share
4 answers

As far as I know, you cannot issue shell commands from a .emacs file.

Of course you can:

 (shell-command "cp ~/.emacs ~/.emacs-`date +%Y%m%d:%H%M`") 
+2
source

The question you really have to ask is how do I never lose revision of any file that I edit in Emacs, including ~ / .emacs?

Answer versiond backups . The variable that controls this function is called version-control , which is confusing because it refers entirely to backups, not VCS.

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

EDIT: describe the save-buffer aspect of backing up each file.

You should read the documentation ( Ch k Cx Cs ) save-buffer to understand the nuances, but basically transferring it to Cu Cu will force it to back up after each save. I activate it in 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) 
+6
source

The best solution is to use a version control system like git . This will be easier if you create the ~/.emacs.d and place your elisp files there:

 mkdir ~/.emacs.d mv .emacs ~/.emacs.d/init.el git init git add init.el git commit -m 'initial checkin' 

Now, every time you modify the init.el file, you can use the following to save the changes:

 git commit -a -m 'descriptive commit message here' 

Then you can add a function to the after-save-hook , for example, something like this gist to automatically add, commit and click when changing files . After clicking, you have a local copy and a remote copy (for example, on github).

Emacs also has integration with git through a package called magit .

In the long run, you will be very rewarded if you take the time to learn how to use DVCS (distributed version control system) and you will find that magit is very convenient to use git .

+2
source

You set the backup properties as a configuration; You can link here .

0
source

Source: https://habr.com/ru/post/1398436/


All Articles