46
votes

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
Morgan, 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
C-x 4f isn't exactly what you're asking but it's a very convenient tip.Raoul HATTERER

6 Answers

14
votes

!!!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))
67
votes

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.

39
votes

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.

3
votes

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.)

1
votes

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

0
votes

C-x o will help you switch to the "other" buffer.