Idiomdrottning’s homepage

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.

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.