reddit / emacs

Today Emacs saved my life

fuzunspm · 2022-05-27 20:50
I have a json file which is not included in backup and stored external drive. It's not included in any type of repo or vc. But it got corrupted today and realized that it's a very important file. I just hit undo-tree mode and revert it back to a previous state. This is awesome :) Thank you very much everyone involved in Emacs
Primary-Wave2 · 2022-05-27 21:42
Smart
helpneeded8578 · 2022-05-27 21:54
I don’t understand. Could you give more detail on how undo-tree allowed you to revert a corrupted file to a previous state? I always thought undo-tree was just for files that you were editing. Not saying I don’t believe you, just saying I don’t understand how exactly you did this.
NoisyFrequency · 2022-05-27 21:59
Maybe a case of `M-x recover-this-file` under the hood?
jimehgeek · 2022-05-27 22:04
Undo-tree by default persists undo history to disk, and attempts to restore the undo history when you open a file. There’s a few other packages that does this too. There’s also a built-in mode that saves your cursor position in files, and restores it when you open the file again. With that too, it can really feel like closing and re-opening files is a non-event :)
helpneeded8578 · 2022-05-27 22:05
Is that actually a thing, or are you clowning me? If that’s real, Emacs is actually crazier than I realized!!
NoisyFrequency · 2022-05-27 22:17
Emacs is that magic! It'll try to reach for backup files when the current file has been modified and "something" bad happened. https://imgur.com/a/OIwsWxa
helpneeded8578 · 2022-05-27 22:33
Nice! Thanks for sharing! Now I need to try it out when I get back to my computer.
helpneeded8578 · 2022-05-27 22:34
That’s pretty awesome. I was wondering why I keep seeing all these duplicate files with the ~ in front of the filename.
R3tr0_Gam3r · 2022-05-27 23:00
If you think that's crazy, wait till I tell you that I have Emacs setup so that everytime I save a file, It takes a backup of that file by making a copy of it, and places that copy in my backup directory. Is it a little redundant? - Yes. Is it crazy cool? - Also yes.
helpneeded8578 · 2022-05-27 23:11
Nice. Does it create additional backup files each time or does it simply overwrite the existing backup file (if it already exists).
jimehgeek · 2022-05-27 23:23
Actually I believe that’s backup files. I think the built-in backup feature defaults to keeping backup files in the same directory. But you can configure a specific dedicated directory for all backup files if you don’t want to clutter your file system with emacs backup file everywhere.
helpneeded8578 · 2022-05-27 23:28
Ok, so there’s multiple backup type features going on. The backup feature you just described, and then undo-mode which sounds much more granular — and which I never realized was saved to disk.
jimehgeek · 2022-05-27 23:47
Yep. Backup files are built-in and part of emacs though. Persisting and restoring undo history on disk involves an external package. I know of three packages that all offer it: - `undo-tree` - `undohist` - `undo-fu-session`
MrTinyToes · 2022-05-27 23:48
All of that is customizable, up to how many new versions are saved, how many old versions, auto deleting after a certain age, etc.
helpneeded8578 · 2022-05-28 02:11
Wow that IS crazy cool!
helpneeded8578 · 2022-05-28 02:13
Makes sense. I’m definitely installing one of these for my org files!
Thaodan · 2022-05-28 03:02
Just make sure that the location is properly secured e.g. if you edit files with a higher security level. The backup store could be a place of attack.
maxle0312 · 2022-05-28 04:16
undo-tree is an absolute god send from emacs. Beside the built in backup, it is soooo much easier to visualize your undos.
onetom · 2022-05-28 04:24
I remember Mike Zamansky mentioned in one of his Emacs videos, that he keeps every version of every file he has ever saved, in timestamped backup files, which form a version *history*. it doesn't obviate proper version *control*, which allows branching, more meta data about the file changes and switching between different version-sets of files. this simple system however, still makes it possible to do some forensics on the individual file level, at the time-granularity implied by how often have you saved a file. you can grep subtrees of this backup directory hierarchy to find pieces of text, you remember having at some point. if u use something like swiper, consult-ripgrep, etc, then u can even conveniently live-preview your results. then you can also use lower-level Emacs built-ins, like compare-windows, diff-buffers and similar stuff in dired, if you found a specific version of your file, to see what else was different at that time in that file, compared to your current state. IntelliJ has a similar feature, called "Local History", which I think is even more granular, than "every time your save", but I never looked into how they persist this information, because they provide a very nice visual browser dialog for navigating the time dimension, even for only selected parts of a file. it's also a bit similar to how venti and/or fossil worked in Plan9; making hourly, timestamped backups of complete drives, but efficiently, by hard-linking unchanged files. AppleI's time machine works in a similar fashion, iirc. it's not a common or default backup configuration approach, because - it constantly consumes more and more disk space - requires the user to know how to efficiently search and navigate sprawling file hierarchies however, disk space became cheap enough for most people over the last few decades, which makes this a feasible approach for allowing persistent infinite undo of the changes *you* make to files. Emacs can't do much about file changes, which happen when Emacs is not running and you don't have that changing file open in Emacs. if a file is open though, using global-auto-revert mode can help to pick up on external changes. then that change can also end up in your backup history, if it gets saved somehow. eg. automatically by auto-save or by some "save-on-focus-lost" feature or if u explicitly save, eg. via `save-some-buffers` C-x s
TunaCowboy · 2022-05-28 06:30
`(setq backup-directory-alist '(("." . "~/.emacs.d/backup/")))` `(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))`
comproprasad · 2022-05-28 06:54
On an unrelated note. I am currently using vundo instead of undo-tree.
lrochfort · 2022-05-28 07:23
This has saved me on more than one occasion
agumonkey · 2022-05-28 08:23
IIRC Eclipse also has an independent local history.
agumonkey · 2022-05-28 08:23
same, the UX is very fun
trepliev · 2022-05-28 09:26
Can you please share your config of this feature? Edit: I think this is the right answer https://www.emacswiki.org/emacs/BackupDirectory
akho_ · 2022-05-28 09:50
Have you backed it up after that?
7890yuiop · 2022-05-28 12:51
The risk of death in this story does not seem as high as I was led to believe.
_viz_ · 2022-05-28 13:11
vc-backup improves the backup experience by a large margin by letting you diff between versions, checking when the backups were made, etc. This ui is a bit nicer to work with than plain grep and friends.
onetom · 2022-05-28 13:15
it seems, this is the package you are referring to: https://elpa.gnu.org/packages/vc-backup.html haven't heard about it before, but sounds really great!
helpneeded8578 · 2022-05-28 13:41
Appreciate that. Adding to my init file immediately. Do you happen to know why the `”.”` is consed to the file path in the code you posted? Does that mean it just appends to the existing `backup-directory-alist` and `undo-tree-history-directory-alist`? I understand elisp at a basic but not very well.
R3tr0_Gam3r · 2022-05-28 16:56
Additional backup files each time.
TunaCowboy · 2022-05-28 17:15
https://www.gnu.org/software/emacs/manual/html_node/elisp/Association-Lists.html
helpneeded8578 · 2022-05-28 17:20
Thanks
[deleted] · 2022-05-29 16:27
[deleted]
ClassicAfternoon3548 · 2022-06-02 21:28
I know, right? I would have guessed an assassin broke into OP's apartment and Emacs materialized a corporal body in order to defend the human that configured it.