Assume I'm opening a new window with for example C-x 2 or C-x 3 then the buffer opens and I need to switch to it by hand with C-x o. Is there a build in solution to do this with one command? Or even modifying the above would be cool as well.


2 Answers 2


The following code written by Dawid Eckert and published in his github emacs.config moves the cursor to the newly created buffer:

(defun split-and-follow-horizontally ()
  (other-window 1))
(global-set-key (kbd "C-x 2") 'split-and-follow-horizontally)

(defun split-and-follow-vertically ()
  (other-window 1))
(global-set-key (kbd "C-x 3") 'split-and-follow-vertically)

C-x4b runs switch-to-buffer-other-window which switches to the specified buffer (but creates it only there's only one buffer displayed).

For more sophisticated behaviour, you'd have to implement the function yourself. You can look up the implementation of the before mentioned function for inspiration:

(defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
   (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
  (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))

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.