2

Is there a command that allows me to have my cursor switch windows to a specified buffer? Something that looks like

(switch-to-specific-window-command "buffer-name")
2

Command switch-to-buffer, bound to C-x b, does what you're looking for. Likewise, switch-to-buffer-other-window, bound to C-x 4 b, and switch-to-buffer-other-frame, bound to C-x 5 b.

Command pop-to-buffer also does what you request, slightly differently.


From your comments, it's not clear what you want. The goalposts seem to be moving...

Now you seem to say you want to do nothing if the buffer is already in the selected-window? In that case, pop-to-buffer does just what you want, I think. Have you tried it?

Or if you don't want to use pop-to-buffer, but that is anyway what you want (do nothing if already visiting the buffer in the selected window), then this does that also:

(defun foo (buffer)
  (interactive "b")
  (unless (equal (get-buffer buffer) (window-buffer (selected-window)))
    (switch-to-buffer-other-window buffer)))

4
  • This would switch to the buffer in the current window. But what I would like to do is to go to the window that already is visiting buffer that I am specifying
    – Tian
    Nov 13 '21 at 17:56
  • 1
    No. pop-to-buffer uses another window, as does switch-to-buffer-other-window (C-x 4 b).
    – Drew
    Nov 13 '21 at 19:05
  • Sorry for the misunderstanding. I meant that switch-to-buffer would behave in the manner I stated in my comment. switch-to-buffer-other-window would not do the job, as if lets say i was already in the window of the buffer that i wanted to switch to. it would switch to another window and open that buffer there. What I want is for a function that would go to that already open window, regardless of whichever window is in focus
    – Tian
    Nov 13 '21 at 19:37
  • 2
    If you haven't tried pop-to-buffer, please try it. If you have tried it, in what way does it fall short?
    – NickD
    Nov 14 '21 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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