Posts Tagged ‘version’

Mass buffer revert in emacs

Saturday, May 21st, 2011

One of the problems with doing branchy development in Emacs is that when you have lots of files open and then switch branches, the bufer contents stay on the previous branch. Thankfully Emacs will warn you that the file’s contents have changed before you edit/save the file, but calling M-x revert-buffer on lots of open files gets old very fast.

This being Emacs though, the natural solution to any irritation is to program your way out of it. M-x regexp-revert let’s you enter a regexp, and then reverts any buffer with a file location matching that regexp to the file’s current state on disk.

(defun regexp-revert (regexp)
  "Revert all buffers whose path matches regexp"
  (interactive "sPath Regexp: ")
  (dolist (buffer (buffer-list))
    (if (string-match-p regexp (or (buffer-file-name buffer) ""))
        (progn
          (set-buffer buffer)
          (revert-buffer nil t)
          (message "Reverting %s" (buffer-file-name buffer))))))

Love regards etc