Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I switch focus after buffer split in emacs?

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 ?

like image 847
morgan freeman Avatar asked Jun 24 '11 07:06

morgan freeman


People also ask

How do I switch between buffers in Emacs?

To move between the buffers, type C-x b. Emacs shows you a default buffer name. Press Enter if that's the buffer you want, or type the first few characters of the correct buffer name and press Tab. Emacs fills in the rest of the name.

How do I close a buffer window in Emacs?

And a buffer can be closed by :q . Likewise in emacs, window can be split horizontally by C-x 2 , and vertically by C-x 3 . And close all other window by C-x 1 .

Can you only have one buffer open in Emacs at a time?

Each Emacs window displays one Emacs buffer at any time. A single buffer may appear in more than one window; if it does, any changes in its text are displayed in all the windows where it appears.

How do I get rid of split screen in Emacs?

You can use the C-x 0 key combination to delete the current window.


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

like image 176
Giann Avatar answered Oct 02 '22 11:10

Giann


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.

like image 21
Bozhidar Batsov Avatar answered Oct 02 '22 10:10

Bozhidar Batsov


!!!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))
like image 22
Thomas Avatar answered Oct 02 '22 12:10

Thomas


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

like image 28
edam Avatar answered Oct 02 '22 11:10

edam


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

like image 39
Nothus Avatar answered Oct 02 '22 11:10

Nothus