The simplest one I can think of is defining recommendations for both functions. Here it is for next-buffer . It would be the same for previous-buffer . You can also define a configuration variable for enabling / disabling behavior (or activating / deactivating advice):
(defadvice next-buffer (after avoid-messages-buffer-in-next-buffer) "Advice around `next-buffer' to avoid going into the *Messages* buffer." (when (string= "*Messages*" (buffer-name)) (next-buffer))) ;; activate the advice (ad-activate 'next-buffer)
Maybe you can compare the buffers in some other way instead of the string name, but that will work. The code for the previous buffer is almost the same. I also donβt know if there is a way to call the original function without calling the advice once in the advice itself, but again, the code will work even if the buffer name is checked later (it will fail if you just have one buffer, and this is a message buffer , and some code can check if there is only one buffer and not call next-buffer again).
If you want to use a standalone function that does the same thing:
(defun my-next-buffer () "next-buffer, only skip *Messages*" (interactive) (next-buffer) (when (string= "*Messages*" (buffer-name)) (next-buffer))) (global-set-key [remap next-buffer] 'my-next-buffer) (global-set-key [remap previous-buffer] 'my-next-buffer)
Diego sevilla
source share