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)
(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)
(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