I'm using doom Emacs in case that's relevant.

I want a key binding to go back to the most recently used buffer in the window. When I'm in that buffer and hit the key binding, it should go back to the buffer I stared out in. So hitting the key binding repeatedly should toggle between the 2 most recently used buffers.

So far I've tried several approaches from what I've found on the internet. next-buffer and privious-buffer both do nothing.

I've also tried this function:

(defun switch-to-previous-buffer ()
  (switch-to-buffer (other-buffer (current-buffer) 1)))

When I call this function, no matter what buffers I've had opened previously, it goes to the scratch buffer (Fallback buffer), and hitting it again does nothing.

I'm now looking for an alternative, I'm new to Emacs and a total noob in elisp, so a brief explanation would be appreciated.


The function I gave as an example actually does work, the problem is in fact with doom Emacs, more specifically with org-roam. When org-roam is installed, and I open emacsclient, I don't get a dashboard, which is part of the issue. For now, I've removed org-roam, this has solved the issue.

  • that function works for me just as you described, not sure why it's not working for you. what does your key binding look like?
    – jdigital
    Commented Dec 28, 2020 at 2:33
  • @jdigital I didn't even try it with a key bindings, I just called the function manually with M-x. I'm beginning to think there's something wrong with the rest of my config or with doom emacs since next-buffer and previous-buffer both do nothing for me.
    – b3nj4m1n
    Commented Dec 28, 2020 at 14:17

3 Answers 3


I use the built-in mode-line-other-buffer command to toggle between the two most recent buffers, with a convenient key binding since I use this all the time.

You may want to compare what that function is doing with your own attempt. Note that other-buffer does what I believe you want without the additional arguments you are passing in your attempt.

  • 1
    I see from comments / updates you found a conflict with org-roam, so never mind. :-) I'll leave my answer here since it may be useful for folks who want to bind the build-in command rather than create their own to do the same thing.
    – glucas
    Commented Dec 28, 2020 at 17:16
  • Just what I was looking for. Thank you @glucas! Commented Oct 23, 2021 at 6:07

Does this do what you want?

(defun my-switch-buf (buffer)
  "Switch to BUFFER.
Like `switch-to-buffer', but don't change buffer-list order."
  (interactive "BBuf: ")
  (switch-to-buffer buffer 'no-record))

(global-set-key (kbd "C-x b") 'my-switch-buf)

C-h f switch-to-buffer says this about optional arg NORECORD (which you used in your trial command):

If optional argument NORECORD is non-nil, do not put the buffer at the front of the buffer list, and do not make the window displaying it the most recently selected one.

But maybe you don't want to have to hit RET. This simple command makes you hit RET, to accept the default value. So to go back and forth you'd use C-x b RET, not just C-x b.

  • It seems like this also picks the scratch buffer as the default option, and it should definitely be one function to instantly switch, without the need to have to hit return.
    – b3nj4m1n
    Commented Dec 27, 2020 at 4:39

I have this bound to F8:

(defun ph/flip-window ()
  "Flips to the last-visited buffer in this window."
  (switch-to-buffer (other-buffer (current-buffer))))

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.