How can I switch focus after buffer split in emacs?
Asked Answered
S

6

47

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 ?

Sturrock answered 24/6, 2011 at 7:31 Comment(2)
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.Davita
C-x 4f isn't exactly what you're asking but it's a very convenient tip.Maramarabel
D
14

!!!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))
Davita answered 24/6, 2011 at 8:59 Comment(8)
As an observation, this would mess up ECB. At least in my setup.Sturrock
If you have 2 vertical splitted buffers, and then do "org-schedule" in one (org) - you'll have a bug with Calendar bufferKylakylah
@Kylakylah 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?Davita
@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)Kylakylah
retested, it appears to happen only after "split-window-right", when having 2(or more) buffers (one of them is org-file)Kylakylah
This 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.Jennet
@Jennet 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.Davita
This breaks anything, that has already implemented switching to created windows. E.g. navigation in customizing groups.Hypochlorite
C
69

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.

Christean answered 24/6, 2011 at 8:2 Comment(1)
Thanks for this. In my current setup I use S-<arrow_keys> to move around between the visible buffers, the automatic thing is what I'm interested in.Sturrock
C
40

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.

Crystie answered 24/6, 2011 at 8:45 Comment(2)
Small typo here. It's split-window-horizontally instead of split-window-horizonatally. Tried to edit but don't have permissions.Sturrock
Shouldn't it really be split-window-right and split-window-below?Jennet
D
14

!!!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))
Davita answered 24/6, 2011 at 8:59 Comment(8)
As an observation, this would mess up ECB. At least in my setup.Sturrock
If you have 2 vertical splitted buffers, and then do "org-schedule" in one (org) - you'll have a bug with Calendar bufferKylakylah
@Kylakylah 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?Davita
@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)Kylakylah
retested, it appears to happen only after "split-window-right", when having 2(or more) buffers (one of them is org-file)Kylakylah
This 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.Jennet
@Jennet 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.Davita
This breaks anything, that has already implemented switching to created windows. E.g. navigation in customizing groups.Hypochlorite
H
3

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

Hapte answered 9/10, 2015 at 23:18 Comment(0)
J
1

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

Jaramillo answered 8/12, 2016 at 6:49 Comment(0)
G
0

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

Granlund answered 10/8, 2021 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.