As many of us know, Emacs has a habit (annoying for most people, I guess) of making backup copies of files it saves.
Happily, it doesn’t kick in in version-controlled files (and most of my files fall into this category nowadays).
But there are situations when I actually do want the backup copies. One of them is the .env
file I have in a few projects, which contains some settings which must not be committed to a VC repository (like credentials), but breaking those settings must be really avoided.
I often resorted to looking into .env~
because of that, but sometimes an error became apparent after some more time. Then, the last version may be inadequate, since the working version was one of the previous ones.
Again happily, Emacs has you covered. You can set Emacs variable version-control
to t
, and then Emacs starts to make numbered backups, like .env.~1~
, .env.~2~
, .env.~3~
etc. (You might want to use file-local variables to set this variable for an individual file, either manually, or by means of add-file-local-variable
or add-file-local-variable-prop-line
command.)
Of course, you probably don’t want to have more than, say, 5–10 of those, so Emacs has two more variables: kept-old-versions
and kept-new-versions
, whose names are pretty self-explanatory. They default to 2, so you might want to increase that if you are a bit paranoiac.
Also, Emacs will by default ask you each time it is going to delete old backup files. If you don’t want to be bothered by that, you may set delete-old-versions
to t
.
Actually, there is more to backup files than that. For instance, you can use a completely different naming scheme for them. Consult the manual for more details – as usual, it is extensive and well-written.