(or emacs irrelevant)

Reverting nonsense

It happens to me frequently when making a demo, or just experimenting around that I want to revert the buffer to the last saved state. Obviously such a command exists in Emacs, and it's unsurprisingly called revert-buffer:

Replace current buffer text with the text of the visited file on disk. This undoes all changes since the file was visited or saved.

But the poor old revert-buffer isn't bound by default in Emacs. The perfect binding for it is C-x C-r:

 (kbd "C-x C-r")
 (lambda () (interactive) (revert-buffer nil t)))

I believe the lambda is used to get the "no questions asked" treatment. Anyway, by default Emacs binds C-x C-r to find-file-read-only, which really is a trash-tier command.

Why is find-file-read-only useless?

Because C-x C-q is bound to read-only-mode - a really good command (made even better with wdired and wgrep). So find-file-read-only is just C-x C-f C-x C-q. And I don't ever recall needing to use that, since v (dired-view-file) is almost equivalent if not better.

As a bonus, C-x C-r is mnemonic for "revert".


While on the topic, let me mention auto-revert-mode. I have this in my config:

(global-auto-revert-mode 1)

This way, if any opened and saved file was modified outside of Emacs, it will be updated in a short while. I was able to use this to a great advantage when writing the SVG picture for the Xmodmap post:

  • I opened the SVG in the default image mode in one Emacs instance
  • and opened the same document in XML mode in another (mode is toggled with C-c C-c)

With that setup, I was able to see the picture update as I was inputting the XML. Here's some Elisp for XML generation, if you're interested

(defun make-row (row-str ix iy fill)
   (lambda (x)
      "<g><text x=\"%d\" y=\"%d\" style=\"font-size:10px;fill:%s;font-family:Deja Vu Sans Mono\">%s</text></g>"
      (+ ix (* x 30))
      (let ((y (elt row-str x)))
        (if (stringp y)
          (make-string 1 y)))))
   (number-sequence 0 (1- (length row-str)))

 95 140
;; =>

The last statement would generate: Q W E R T Y U I O P.