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
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 ()
(interactive)
(split-window-below)
(balance-windows)
(other-window 1))
(global-set-key (kbd "C-x 2") 'split-and-follow-horizontally)
(defun split-and-follow-vertically ()
(interactive)
(split-window-right)
(balance-windows)
(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)
"..."
(interactive
(list (read-buffer-to-switch "Switch to buffer in other frame: ")))
(pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))