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.
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.
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.
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.
C-x b
callsswitch-to-buffer
interactively, where you can type the name of the buffer you want to switch to. Have you perhaps remapped it?