31

Sometimes I launch emacs from the command line with 2 files, as follows:

emacs foo.txt bar.txt

This opens the emacs window, split vertically:

foo.txt
-------
bar.txt

How can I edit my .emacs file so that they show up side-by-side, like this?:

        |
foo.txt | bar.txt
        |

EDIT: To clarify, I know how to make this happen after emacs has launched (M-x 0, M-x 3, then re-visit bar.txt in the right window). I just want emacs to split side-by-side by default when I launch it, so I don't have to.

SuperElectric
  • 17,548
  • 10
  • 52
  • 69
  • 2
    I just asked a very similar question and got an answer: http://stackoverflow.com/questions/6683039/opening-more-than-one-file-split-vertically-through-emacs-nw Enjoy. – yarian Jul 17 '11 at 18:31

7 Answers7

20

Here's a function that will change a pair of vertical windows to a pair of horizontal windows:

(defun 2-windows-vertical-to-horizontal ()
  (let ((buffers (mapcar 'window-buffer (window-list))))
    (when (= 2 (length buffers))
      (delete-other-windows)
      (set-window-buffer (split-window-horizontally) (cadr buffers)))))

To do this automatically on startup, add this function to emacs-startup-hook:

(add-hook 'emacs-startup-hook '2-windows-vertical-to-horizontal)
Sean
  • 29,130
  • 4
  • 80
  • 105
  • 3
    I originally chose one @huitseeker's answer for its simplicity, but it had one drawback. When emacs temporarily splits a window to show, for example, possible filename tab-completions, it would split the current window side-by-side. This is pretty horrible when the current window is only 80 chars long. Sean's answer splits windows side-by-side when you open multiple files on emacs launch, but otherwise lets emacs retain its usual behavior of opening temporary buffers by splitting the current window vertically. – SuperElectric Jul 26 '11 at 19:46
  • a small comment like `"Transform vertical split to horizontal split."` to avoid warning of a given lisp checker could be nice. – Welgriv Jun 11 '20 at 14:46
17

The following (to add to your .emacs) makes the window splitting default result in side-by-side buffers (rather than one above the other):

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

This default will also apply when you run a command such as find-file-other-window (Ctrlx4f).

(On the other hand, to manually split your window to get two side-by-side buffers, consider this answer).

Welgriv
  • 714
  • 9
  • 24
Francois G
  • 11,957
  • 54
  • 59
  • Six years old answer but it worked for me on Emacs 25.1.1. – Lalylulelo Jul 26 '17 at 09:06
  • This doesn't always work, depending on the current state of split-window-sensibly, inline documentation says ".. prefer either vertical or horizontal splitting" – dturvene Jul 30 '19 at 12:05
6

This has worked well for me. Use the -f function-name from the command-line to have it set up your emacs split-screen workspace as you like. This gives me a 2 x 2 grid of my financial files that I update every day and sets the cursor on the appropriate window at the end. I save this to .bashrc as an alias so I can pull it up with one command (doc_financial).

alias doc_financial='emacs -nw financial_accounts.txt -f split-window-horizontally financial_budget.txt -f split-window-vertically financial_taxes.txt -f other-window -f split-window-vertically financial_tasks.txt -f other-window -f other-window -f other-window'
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
Jack Hamilton
  • 59
  • 1
  • 2
5

Use M-x split-window-horizontally or Ctrl-x 3.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • 4
    I know how to do it manually once emacs has launched; I just want emacs to use side-by-side splitting by default when opening multiple files. Editing question to clarify. – SuperElectric Jul 14 '11 at 18:11
0
;; 15-Oct-20  open first file in left window
(defun my-startup-layout ()
  (let ((buffers (mapcar 'window-buffer (window-list))))
    (when (= 2 (length buffers))
      (delete-other-windows)
      (set-window-buffer (split-window-horizontally) (cadr buffers))))
)

(add-hook 'emacs-startup-hook 'my-startup-layout)
(add-hook 'emacs-startup-hook 'next-multiframe-window)
;; end of customization to open first file on left
;;
David Buck
  • 3,752
  • 35
  • 31
  • 35
oldfwemp
  • 1
  • 1
-1

Ctrl -x 2 Split window, above and below

Buffer 1 (above)
Buffer 2 (Below)

Ctrl -x 3 Split window, Side by Side

Buffer 1(left) | Buffer 2 (right)

C-M-v Scroll other window

wsha
  • 874
  • 2
  • 9
  • 25
-1

Use split-window-horizontally.

Marcin
  • 48,559
  • 18
  • 128
  • 201