93

Quick question: How do I specify the number of characters in a split window? C-x-3 Splits my window into two windows evenly, but a subsequent split will split one of the windows in half. I'd like 3 equal sized windows. The documentation says that I should be able to specify the number of characters for the left buffer as a parameter, but I cant seem to get that to work. Any ideas for syntax?

Thanks.

Dirk
  • 6,774
  • 14
  • 51
  • 73

9 Answers9

299

C-x 3 twice followed by C-x + to equally size all windows.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Josh Matthews
  • 12,816
  • 7
  • 36
  • 39
28

To specify the number of characters in the split window, do:

C-u number-of-characters C-x 3

Nikwin
  • 6,576
  • 4
  • 35
  • 43
  • Window width too small (after splitting) – Mittenchops Dec 07 '13 at 07:45
  • 5
    The problem is it doesn't make the windows _even_, that's setting the width manually and so you'd have to count how many characters wide and tall the emacs window is to size them evenly. The answer below is more useful. – leinaD_natipaC Dec 14 '13 at 16:07
  • 28
    `C-x +` to make em evenly spaced – Josh.F Oct 11 '16 at 18:03
  • 2
    Downvoted because manual resizing. I don't want to count my terminal height and divide it by 3. – byxor Jan 24 '18 at 16:24
  • This answers the question, but Josh. F's comment and Josh Matthews' answer are what most people would like. – Zach Oct 29 '20 at 18:59
17

I have the following in my .emacs:

(defadvice split-window-horizontally (after rebalance-windows activate)
  (balance-windows))
(ad-activate 'split-window-horizontally)

this makes emacs call rebalance-windows (which is what C-x + is bound to by default) after every resize. It's not what I want all the time, but I want it much more often than the default behavior.

quodlibetor
  • 8,185
  • 4
  • 35
  • 48
  • 1
    thanks for this, but I'm not sure you need the call to `(ad-activate ...)` since you included `activate` in a separate form. – wpcarro Oct 10 '17 at 15:12
  • I use C-x 3 for split-window-right, and this worked for me without the ad-activate: `(defadvice split-window-right (after rebalance-windows activate) (balance-windows))` – beaslera Jul 12 '22 at 13:07
12

add in .emacs. I mapped to C-x 4, but anyone has a better idea?

(defun split-3-windows-horizontally-evenly ()
  (interactive)
  (command-execute 'split-window-horizontally)
  (command-execute 'split-window-horizontally)
  (command-execute 'balance-windows)
)

(global-set-key (kbd "C-x 4") 'split-3-windows-horizontally-evenly)
David J.
  • 31,569
  • 22
  • 122
  • 174
xosp7tom
  • 2,131
  • 1
  • 17
  • 26
  • 5
    I wouldn't assign that to `C-x 4`. That's the default prefix for a heap of other commands. See `C-x 4 C-h` (without setting your binding, obviously). – phils Dec 13 '11 at 23:20
  • 3
    @phils whoa after all these years and I never knew how to get a list of commands with some common prefix... I love emacs. – mgalgs Aug 16 '12 at 22:55
4
(defun wenshan-split-window-vertical (&optional wenshan-number)
"Split the current window into `wenshan-number' windows"
  (interactive "P")
  (setq wenshan-number (if wenshan-number
                           (prefix-numeric-value wenshan-number)
                         2))
  (while (> wenshan-number 1)
    (split-window-right)
    (setq wenshan-number (- wenshan-number 1)))
  (balance-windows))

This function can be used to split the current window into N windows, you can type "C-u 3 M-x wenshan-split-window-vertical" to achieve what you want.

Wenshan
  • 690
  • 6
  • 13
2

If you use evil do C-x 3 and then C-w =

endre
  • 1,363
  • 1
  • 11
  • 22
2

I liked @quodlibetor's solution, but it didn't work as written. This works for me (emacs 24.5)

(advice-add 'split-window-right :after #'balance-windows)
ianxm
  • 81
  • 6
1

One of the problems with many of the answers that use balance-windowshere is that they may not allow the window to split due to window-min-width or split-width-threshold even if everything would be fine after rebalancing. For example, I don't want windows less than 100 characters wide, but if I split my screen once I get two windows that are 160 characters wide and can't split again without resizing one of the windows. I haven't figured out how to determine whether a split is good yet, so I'll probably just dynamically bind those values to 0 while splitting, and maybe do a window-configuration-to-register beforehand just in case so that I can recover the old layout when things go wrong.

Dave Abrahams
  • 7,416
  • 5
  • 31
  • 19
0

Here is my solution, hope it helps:

(defun split-vertical-evenly ()
  (interactive)
  (command-execute 'split-window-vertically)
  (command-execute 'balance-windows))
(global-set-key (kbd "C-x 2") 'split-vertical-evenly)

(defun split-horizontal-evenly ()
  (interactive)
  (command-execute 'split-window-horizontally)
  (command-execute 'balance-windows))
(global-set-key (kbd "C-x 3") 'split-horizontal-evenly)
yaoya
  • 1
  • 1