1

I want to quickly alternate between the buffer I'm in and the last buffer I was in. Is there a built-in command for this?

I rolled my own, but I might rather use something built-in.

(defun alternate-buffer ()
  "Alternate between the current buffer and the previous."
  (interactive)
  (switch-to-buffer (caar (window-prev-buffers))))

I've checked M-x apropos and searched for "buffer", but I haven't located such a command.

| improve this question | | | | |
  • C-x b Enter not the same ? – fghj Nov 13 '15 at 4:11
  • Sure, but that's one extra keystroke. I want to go to the alternate buffer directly. – Jackson Nov 13 '15 at 4:13
  • To note, an argument of nil to switch-to-buffer switches to the previous buffer that you selected in the current frame. This is slightly different as your function seems to only consider the current window history. Is that what you intended? – whacka Nov 13 '15 at 5:15
  • Interesting, that is slightly different when multiple windows are open, and I think I prefer the behavior of alternate-buffer in that case, as it feels more "predictable" for the way that I use windows. – Jackson Nov 13 '15 at 5:48
2

I use this function

(defun switch-to-previous-buffer ()
  (interactive)
  (switch-to-buffer (other-buffer)))

Still custom elisp, but better that caar

| improve this answer | | | | |
0

This can be done easily with the modern completion systems: Helm or Ivy. Use helm-buffers-list or ivy-switch-buffer correspondingly.

Now here's the trick: repeatedly press C-M-n to switch to the next buffer without exiting the completion. Press C-M-p for the previous buffer. Finally, press C-m to exit completion.

| improve this answer | | | | |
0

How about?:

previous-buffer ["In selected window switch to previous buffer."]

and

next-buffer ["In selected window switch to next buffer."]

Both are built-in functions defined in window.el and come with their own keyboard shortcuts, which you can of course change or create additional ones.

| improve this answer | | | | |
  • I just saw that the other thread cited as a duplicate also included this solution, so I am respectfully voting to close as duplicate. – lawlist Nov 15 '15 at 17:04
-1

The command you're looking for is switch-to-other-buffer. I bind it to F9, and use it frequently.

Update: My bad - it's an elisp function co-opted from Xemacs:

;; Switch To Other Buffer, from Xemacs/files.el
(defun switch-to-other-buffer (arg)
 "Switch to the previous buffer.  With a numeric arg, n, switch to the nth
most recent buffer.  With an arg of 0, buries the current buffer at the
bottom of the buffer stack."
 (interactive "p")
 (if (eq arg 0)
 (bury-buffer (current-buffer)))
 (switch-to-buffer
  (if (<= arg 1) (other-buffer (current-buffer))
(nth (1+ arg) (buffer-list)))))
| improve this answer | | | | |
  • I'm not seeing this when I use M-x. Emacs version is GNU Emacs 24.5. – Jackson Nov 13 '15 at 20:59
  • There is no such command in vanilla Emacs (emacs -Q). You will need to specify the library that defines it etc. – Drew Nov 13 '15 at 23:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.