Many procedures in Emacs write to the *Messages* buffer. I usually kill it from time to time. My problem is that the content gets deleted once the buffer get killed. Is there a way to preserve the contents of the *Messages* buffer throughout an Emacs session or even throughout multiple sessions?

I found a related topic here, but was unable to fit it to the *Messages* buffer?
How can I save my mini-buffer history in Emacs?

2 Answers 2


You can use kill-buffer-query-functions to prevent certain buffers from being killed inadvertently. I use something like this:

(defcustom buffer-protect-buffers '("*scratch*" "*Messages*")
  "List of buffer names to protect."
  :type '(repeat string)
  :group 'buffer-protect)

(defun buffer-protect-protected-p (buffer)
  "Return non-nil if BUFFER is protected."
  (member buffer buffer-protect-buffers))

(defun buffer-protect-kill-buffer-query-function ()
  "Bury a protected buffer and don't allow it to be killed."
  (let ((buf (buffer-name)))
    (if (buffer-protect-protected-p buf)
        (with-current-buffer buf
          (progn (bury-buffer) nil))

(add-hook 'kill-buffer-query-functions #'buffer-protect-kill-buffer-query-function)

With this setup the scratch and message buffers will not be killed during your Emacs session. You can customize the buffer-protect-buffers list to protect other things.

I don't believe you can maintain your *Messages* buffer across sessions. You can save the buffer to a text file using the normal write-file command, but there is no way to restore its content to the actual *Messages* buffer.

  • 1
    I went ahead and pulled buffer-protect out of my config in to a separate package: github.com/glucas/buffer-protect, in case it's useful to others.
    – glucas
    Commented Apr 3, 2017 at 17:08

You should be able to save it to a file just like any other buffer - switch to it with C-h e and then save it to a named file with C-x C-w. This will save the current contents of the buffer but note that as it continues to update those changes won't appear in the saved file. If you really wanted to save it all (although I'm not really sure why this would be useful) you could add a function to do this to kill-emacs-hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.