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 ?
!!!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))
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 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.
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
.
split-window-horizontally
instead of split-window-horizonatally
. Tried to edit but don't have permissions. –
Sturrock split-window-right
and split-window-below
? –
Jennet !!!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))
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 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.)
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
© 2022 - 2024 — McMap. All rights reserved.