6

When I do M-x compile, emacs will split my window to launch compilation and show its logs in a new window. As I understand, it has some algorithm to decide how to split the window.

So, when I have just one window with code opened on my big monitor, for some reason, instead of more-convenient horizontal split (showing compile-log side-by-side on the right and code on left), it splits window vertically. How could that be tweaked? Thank you.

3 Answers3

5

The function that splits the window is split-window-sensibly, which can be tweaked by split-width-threshold and split-height-threshold, as Stefan suggested.

Alternatively, you can tell Emacs to use a different function to split the window by setting split-window-preferred-function (the default is split-window-sensibly). For more details see C-h v split-window-sensibly RET.

bmag
  • 1,703
  • 10
  • 12
  • 3
    This helped me out too, but It seems like the thresholds should be in the reverse order. For example, I would like to be able to say "if this window is really wide, split it vertically so things don't get wrapped/cutoff, and if it is really long, split it horizontally". This seems more "sensible" to me, but am I missing something in the rational for having the settings work the way they do? – elethan Nov 24 '15 at 16:10
  • 1
    @elethan absolutely agree on this. Code/text is usually located in left part of your buffer, so splitting it vertically doesn't cut amount of code you see and must go by default if it's possible, otherwise (when it's too narrow) – go with splitting vertically, cutting amount of code you see in half. – Kostiantyn Rybnikov Nov 25 '15 at 10:57
  • Looking at the source code for `split-window-sensibly`, there's no way to reverse the order. You will have to write (or find) a new function and set it as `split-window-preferred-function`. – bmag Nov 25 '15 at 17:57
  • I used [this answer](http://stackoverflow.com/questions/23659909/reverse-evaluation-order-of-split-height-threshold-and-split-width-threshold-in) to fix it for myself. – Matthew Piziak Oct 09 '16 at 12:22
4

C-h v split-width-threshold RET should give you some useful info about that. There's also split-height-threshold, of course.

Stefan
  • 26,154
  • 3
  • 46
  • 84
1

I haven't found any answer to the original post, which I take to be: “How can I make sure that my window is always split horizontally, no matter what”, so here is what's worked for me:

(setq split-width-threshold nil)

It is strange that this works, as – according to the documentation of the variable split-width-threshold – this should have exactly the opposite effect:

“If this is nil, ‘split-window-sensibly’ is not allowed to split a window horizontally.”

So maybe this is a result of some bug, but it works for me, unlike other methods I tried. In particular, the following, suggested by the documentation of split-window-sensibly, does not work for me:

(setq split-height-threshold nil)
(setq split-width-threshold 0)

HTH.

P.S. My emacs version: GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.14) of 2020-03-26, modified by Debian