2

I switch buffers with C-x b a lot.

C-x b switches to the most recently used buffer. What if I want to switch to the 2nd most recently used buffer (or the nth most recently used buffer). I tried the obvious C-u 2 C-x b but didn't work.

2
  • 1
    By default C-x b calls switch-to-buffer interactively, where you can type the name of the buffer you want to switch to. Have you perhaps remapped it?
    – glucas
    Dec 5, 2017 at 14:29
  • I'm using prelude defaults Jan 5, 2018 at 15:19

3 Answers 3

4

Try this:

(defun switch-to-other-buffer (arg)
  "Switch to last visited buffer. 

Use prefix arg to specify the order of the buffer, most to least
recent. Does not update buffer list, so, for example, after moving to the 
nth most recent buffer, use unprefixed command to get back to original 
buffer."
  (interactive "P")
  (switch-to-buffer (if arg
                        (elt (buffer-list) arg)
                      (other-buffer))
                    nil))

Press C-h k then C-x b. Whatever the name of the function that pops up, rename the above function to and evaluate it with C-x C-e.

Warning: untested.

2

I don't think there is a built-in command for this. You could write your own to call switch-to-buffer passing the nth element from buffer-list.

A more typical approach to this use case might be to use switch-to-buffer interactively to choose a specific buffer by name. If you use ido or ivy or helm or some other completion framework you can usually get to any available buffer in a couple key strokes.

1

I was getting fed up with switching buffers as well, but will you really remember what was the nth buffer? I found these keys to cycle through buffers: C-x <left> and C-x <right>. They call previous-buffer and next-buffer respectively.

1
  • Check out helm. It’s has nice functions for switching buffers
    – Dodgie
    Jan 5, 2018 at 17:00

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.