tomasz buchert's page

gcore(1) for the rescue

A very crude Emacs undo

Some time ago I have switched from Sublime Text to Emacs as my main text/code editor. The reason for the switch was purely philosophical (free vs. proprietary software) and I’m very happy with the result. I have to admit though that I’ve spent a lot of time configuring it. It’s actually so customized that an Emacs-purist wouldn’t know how to use it.

One nasty thing in the standard Emacs 24.3 installation is that if a file is modified outside Emacs then your buffer will be replaced (reverted) and undo history will be lost (along with the previous contents). Fortunately, there is a fix for that and apparently it is fixed in Emacs 24.4.

Anyway, when I was first hit by this, I thought that my two-hour-worth work is lost (the file I was working on was written from outside and I couldn’t undo). I was frustrated and angry, but then I thought: surely there is a way to get my buffer back, right?

Here is a funny way to do it: dump the whole memory of the running Emacs process and find your previous buffer contents (which, hopefully, were not overwritten by Emacs yet). Ladies and gentlemen, let me introduce gcore(1) and strings(1) if you haven’t met them before:

$ gcore -o memdump [PID of the emacs process]
$ strings memdump | grep -C 20 [part of the previous buffer]

A very useful trick which may save your work one day (and not only in Emacs).


inspired by classicthesis / 2024