I would like that after splitting the window (C-x 3
or C-x 2
) to be able to automatically get to cursor in the new opened buffer (the other than the current). How can I achieve this behavior ?

- 6,041
- 3
- 25
- 32
-
1Morgan, my answer below can unfortunately lead to nasty side-effects, therefore I would like to retract it. Unfortunately, I cannot do so as long as it's the accepted answer. Could you please un-accept it, so I can delete it? Thanks. – Thomas Oct 21 '15 at 17:57
-
C-x 4f isn't exactly what you're asking but it's a very convenient tip. – Raoul HATTERER Sep 18 '18 at 16:44
6 Answers
You can switch between buffers with C-x o
. As to do that automatically I don't think there is an existing command for that.

- 3,142
- 3
- 23
- 33
-
Thanks for this. In my current setup I use S-
to move around between the visible buffers, the automatic thing is what I'm interested in. – morgan freeman Jun 24 '11 at 08:13
You can do it like this:
(global-set-key "\C-x2" (lambda () (interactive)(split-window-vertically) (other-window 1)))
(global-set-key "\C-x3" (lambda () (interactive)(split-window-horizontally) (other-window 1)))
In Emacs 24.3.1 it works if you change the argument 1
for 0
.

- 12,351
- 5
- 40
- 47

- 55,802
- 13
- 100
- 117
-
1Small typo here. It's `split-window-horizontally` instead of `split-window-horizonatally`. Tried to edit but don't have permissions. – morgan freeman Jun 30 '11 at 06:41
-
3Shouldn't it really be `split-window-right` and `split-window-below`? – asmeurer May 14 '13 at 01:43
!!!DO NOT USE THIS ANSWER!!! -- as pointed out in the comments, advising split-window
can lead to undesired side-effects.
I recommend Bozhidar Batsov's answer instead.
Put the following in your .emacs file:
(defadvice split-window (after move-point-to-new-window activate)
"Moves the point to the newly created window after splitting."
(other-window 1))

- 17,016
- 4
- 46
- 70
-
As an observation, this would mess up ECB. At least in my setup. – morgan freeman Jun 30 '11 at 06:35
-
If you have 2 vertical splitted buffers, and then do "org-schedule" in one (org) - you'll have a bug with Calendar buffer – Sergey Dec 30 '12 at 07:35
-
@Sergey What kind of bug? Do you get a different behavior if you do the buffer switch manually after the split, instead of using the above code? – Thomas Dec 30 '12 at 12:17
-
@Thomas, it opens Calendar, but the cursor is not visible in it's buffer, and it also replaces my org-file with 3 months of calendar buffer content (so I have 2-3 not working Calendar buffers in the end) – Sergey Dec 30 '12 at 12:22
-
retested, it appears to happen only after "split-window-right", when having 2(or more) buffers (one of them is org-file) – Sergey Dec 30 '12 at 12:28
-
1This breaks ispell-word (`M-$`), because it doesn't expect it to have the point in the ispell buffer it creates. I think Bozhidar Batsov's solution of only changing the user keyboard shortcuts is a much better way. – asmeurer May 28 '13 at 15:59
-
@asmeurer I agree. Unfortunately, now that the answer was accepted, I cannot delete it :-( So, to everyone: only use the above solution if it fits your workflow. – Thomas Nov 20 '14 at 09:18
-
This breaks anything, that has already implemented switching to created windows. E.g. navigation in customizing groups. – QwiglyDee Oct 21 '15 at 17:25
As well as splitting the frame manually with C-x 2 or C-x 3, buffers are also automatically "popped-up" some times. These are also not selected/active by default.
This can be fixed by changing the function used to split a window. It's set to split-window-sensibly
by default, but you can set it to your own function that calls split-window-sensibly
and then selects the buffer.
Unfortunately, though, this has the side-effect of selecting the *Completions*
buffer when you hit TAB in the minibuffer. So, it's worth checking to see if the minibuffer is active and not switching in this case. I'd bet there are other such undesirable scenarios as well. I'll try to update this post as and when I find them.
;; after splitting a frame automatically, switch to the new window (unless we
;; were in the minibuffer)
(setq split-window-preferred-function 'my/split-window-func)
(defun my/split-window-func (&optional window)
(let ((new-window (split-window-sensibly window)))
(if (not (active-minibuffer-window))
(select-window new-window))))
(Works with Emacs 24.5.1.)

- 718
- 2
- 8
- 13
My thought of when you would want to follow the window after a split-window
was when it had the same buffer like in the following code:
(defun split-window--select-window (orig-func &rest args)
"Switch to the other window after a `split-window'"
(let ((cur-window (selected-window))
(new-window (apply orig-func args)))
(when (equal (window-buffer cur-window) (window-buffer new-window))
(select-window new-window))
new-window))
(advice-add 'split-window :around #'split-window--select-window)
Simple

- 1,127
- 8
- 7