šŸ’¾ Archived View for idiomdrottning.org ā€ŗ emacs-undo captured on 2024-03-21 at 15:39:49. Gemini links have been rewritten to link to archived content

View Raw

More Information

ā¬…ļø Previous capture (2023-03-20)

šŸš§ View Differences

-=-=-=-=-=-=-

Emacs undo and me

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:

EmacsWiki: Category Undo

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.

The life-changing Magit of... Iā€™ll see myself out

If a file is under git, 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.