emacs split into 3 even windows

Emacs

Emacs Problem Overview


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.

Emacs Solutions


Solution 1 - Emacs

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

Solution 2 - Emacs

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

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

Solution 3 - Emacs

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.

Solution 4 - Emacs

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)

Solution 5 - Emacs

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

Solution 6 - Emacs

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

Solution 7 - Emacs

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)

Solution 8 - Emacs

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.

Solution 9 - Emacs

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDirkView Question on Stackoverflow
Solution 1 - EmacsJosh MatthewsView Answer on Stackoverflow
Solution 2 - EmacsNikwinView Answer on Stackoverflow
Solution 3 - EmacsquodlibetorView Answer on Stackoverflow
Solution 4 - Emacsxosp7tomView Answer on Stackoverflow
Solution 5 - EmacsWenshanView Answer on Stackoverflow
Solution 6 - EmacsendreView Answer on Stackoverflow
Solution 7 - EmacsianxmView Answer on Stackoverflow
Solution 8 - EmacsDave AbrahamsView Answer on Stackoverflow
Solution 9 - EmacsyaoyaView Answer on Stackoverflow