š¾ Archived View for idiomdrottning.org āŗ emacs-undo captured on 2023-07-22 at 16:35:52. Gemini links have been rewritten to link to archived content
ā¬ ļø Previous capture (2023-03-20)
ā”ļø Next capture (2024-03-21)
-=-=-=-=-=-=-
Emacs has a couple of different undo packages I could install if I wanted to. Trees, histories, I donāt know what. I havenāt tried āem. Here is a good starting point:
Maybe Iāll switch over to one of them one of these days (and knowing how I usually work, probably right after writing an essay like this where Iāve just been like āoh I for sure donāt use any of those packagesā and then three seconds later I get roped in (by myself if nothing else) to switching to one of them) but right now I use the same default way it works and has worked for twenty-five years.
In some weirdo chain my brain donāt fully understand but my fingers seem to know how to work. I can undo in one ādirectionā but then if I do anything else (just move the cursor or set the mark) it switches direction because the undos themselves are getting undone. Itās a mess but it somehow works, even for undos really far back.
But I would be dishonest if I didnāt also mention the other thing I do which sort of saves that messy system from being unusable: āsave statesā. I just save the file, usually with the default command, C-x C-s, but I also have mapped C-c A which saves a copy (to a standard location, always using the same name, it doesn't prompt) without saving the local buffer at all, and C-c r which reverts the file, and if I revert by mistake I can still undo the revert. Usually.
So Iām often saving and reverting as a complement to the normal Emacs undo mechanisms. That kind of goes to show that Iām not 100% comfy with undo. On the other hand, these āsave statesā are a sort of protection that works even through crashes.
(global-set-key (kbd "C-c r") #'(lambda () (interactive) (revert-buffer t t))) (defun save-a-copy () (interactive) (write-region (point-min) (point-max) "/tmp/saved-copy.txt")) (global-set-key (kbd "C-c A") 'save-a-copy)
Thereās no keyboard command for reverting from the copy, if I need to do that Iāll have to do it manually.
If a file is under git, which admittedly most of these normal essay files arenāt, but programs and other heavier stuff are, then thereās another thing thatās possible thatās even better than undo or revert: ādiscard hunkā from magit. Perfect when a particular change was misguided or a particular thought ended up going nowhere. It feels like a super powerful time machine. I can use the files themselves as scrap paper, writing all kinds of junk in there. Thatās why Iām terrified of using the āeverything gets autocommitted, there is no staging areaā philosophy of jujitsu, gitless, game of trees etc. Iām more scared of overcommitting than undercommitting. Slackerās manifesto in one sentence right there!
Or if I want to restore something, I can browse to a version that has that file and paste from there. A liāl fiddly but at least itās not lost. That has saved me a couple of times.