When opening 2 files in emacs, how can I have them appear side-by-side?

18,297

Solution 1

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)

Solution 2

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

Solution 3

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'

Solution 4

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

Share:
18,297
SuperElectric
Author by

SuperElectric

Updated on June 22, 2022

Comments

  • SuperElectric
    SuperElectric about 2 years

    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.