I have a lisp function that relies on other-buffer which refers to buffer-list. Calling previous-buffer on a window does not update the buffer-list so it breaks the function.

How do make it so previous-buffer updates buffer-list?



I have the following lisp function that toggles to eshell with current directory set to the file I am viewing and from eshell back to the previous file.

(defun eshell-here ()
  "Go to eshell and set current directory to the buffer's directory.
   If already on eshell, go to last buffer"
  (if (equal major-mode 'eshell-mode)
      (switch-to-buffer (other-buffer (current-buffer) nil))
    (let ((dir (file-name-directory (or (buffer-file-name)
      (if (not (equal (expand-file-name (concat (eshell/pwd) "/"))
                      (expand-file-name dir)))
            (eshell/pushd ".")
            (cd dir)
            (goto-char (point-max))

(global-set-key (kbd "<f1>") 'eshell-here)

The call to other-buffer relies on the most recently viewed buffer which is stored in the buffer-list.

previous-buffer and next-buffer does not update the buffer-list so I lose the ability to get back to the file I was viewing from eshell.

The solution I have come up with is to call switch-to-buffer after previous-buffer/next-buffer to indirectly update the buffer list.

Do you think there is a better solution?

  • Please specify what you mean by "does not update the buffer-list" and, more importantly, what you would want to happen instead. What is the ultimate behavior that you want? So far, this looks like an X Y problem.
    – Drew
    May 3, 2017 at 13:37
  • @Drew I would like to be able to get back to the buffer I was viewing with previous-buffer along with the call to other-buffer.
    – Kevin
    May 4, 2017 at 2:06

1 Answer 1


Seems buffer-list cannot be directly manipulated so modified previous-buffer to call (switch-to-buffer (buffer-name)) to indirectly update the buffer-list.

  • Note that if you only care about interactive calls to previous-buffer then you should remap the bindings for that command to a new custom command which does what you want. e.g. (define-key KEYMAP [remap previous-buffer] 'my-previous-buffer)
    – phils
    May 3, 2017 at 12:43

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.