Emacs: Next/Previous User Buffer

By Xah Lee. Date: . Last updated: .

emacs has commands to switch to prev/next buffer.

One problem is that you cannot hold down the key for the command to repeat.

Another problem is that they will go thru many buffers user are not interested in cycling thru.

Examples of emacs generated buffers:

Cycle Thru User Buffers

Here's commands to cycle thru user buffers.

(defun xah-next-user-buffer ()
  "Switch to the next user buffer.
“user buffer” is determined by `xah-user-buffer-q'.
URL `http://ergoemacs.org/emacs/elisp_next_prev_user_buffer.html'
Version 2016-06-19"
  (interactive)
  (next-buffer)
  (let ((i 0))
    (while (< i 20)
      (if (not (xah-user-buffer-q))
          (progn (next-buffer)
                 (setq i (1+ i)))
        (progn (setq i 100))))))

(defun xah-previous-user-buffer ()
  "Switch to the previous user buffer.
“user buffer” is determined by `xah-user-buffer-q'.
URL `http://ergoemacs.org/emacs/elisp_next_prev_user_buffer.html'
Version 2016-06-19"
  (interactive)
  (previous-buffer)
  (let ((i 0))
    (while (< i 20)
      (if (not (xah-user-buffer-q))
          (progn (previous-buffer)
                 (setq i (1+ i)))
        (progn (setq i 100))))))

You need the following to determine what's user buffer. For example, do you want to cycle thru dired buffer? How about shell?

Just override this function if you want to customize it.

(defun xah-user-buffer-q ()
  "Return t if current buffer is a user buffer, else nil.
Typically, if buffer name starts with *, it's not considered a user buffer.
This function is used by buffer switching command and close buffer command, so that next buffer shown is a user buffer.
You can override this function to get your idea of “user buffer”.
version 2016-06-18"
  (interactive)
  (if (string-equal "*" (substring (buffer-name) 0 1))
      nil
    (if (string-equal major-mode "dired-mode")
        nil
      t
      )))

Cycle Thru Emacs Buffers

Here's commands to cycle thru emacs's buffers.

(defun xah-next-emacs-buffer ()
  "Switch to the next emacs buffer.
“emacs buffer” here is buffer whose name starts with *.
URL `http://ergoemacs.org/emacs/elisp_next_prev_user_buffer.html'
Version 2016-06-19"
  (interactive)
  (next-buffer)
  (let ((i 0))
    (while (and (not (string-equal "*" (substring (buffer-name) 0 1))) (< i 20))
      (setq i (1+ i)) (next-buffer))))

(defun xah-previous-emacs-buffer ()
  "Switch to the previous emacs buffer.
“emacs buffer” here is buffer whose name starts with *.
URL `http://ergoemacs.org/emacs/elisp_next_prev_user_buffer.html'
Version 2016-06-19"
  (interactive)
  (previous-buffer)
  (let ((i 0))
    (while (and (not (string-equal "*" (substring (buffer-name) 0 1))) (< i 20))
      (setq i (1+ i)) (previous-buffer))))

Latest code in xah fly keys or ergoemacs-mode.

Be sure to set a easy key for them. 〔►see Emacs: How to Define Keys〕 Here's example:

(global-set-key (kbd "<f11>") 'xah-previous-user-buffer)
(global-set-key (kbd "<f12>") 'xah-next-user-buffer)

(global-set-key (kbd "<S-f11>") 'xah-previous-emacs-buffer)
(global-set-key (kbd "<S-f12>") 'xah-next-emacs-buffer)

Open File topics

  1. Emacs: File Manager, dired
  2. Emacs: Bookmark Tutorial
  3. Emacs: Open Recently Opened File
  4. Emacs: Open File Fast
  5. Emacs: Open Last Closed File
  6. Emacs: Open File Path Under Cursor

Buffer topics

  1. Emacs: List Buffers
  2. Emacs: Switch Buffer
  3. Emacs: New Empty Buffer
  4. Emacs: Next/Previous User Buffer
Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.