SuperElectric
SuperElectric

Reputation: 18914

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

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.

Upvotes: 31

Views: 20484

Answers (7)

oldfwemp
oldfwemp

Reputation: 1

;; 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
;;

Upvotes: 0

Francois G
Francois G

Reputation: 11985

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

Upvotes: 17

Jack Hamilton
Jack Hamilton

Reputation: 69

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'

Upvotes: 6

wsha
wsha

Reputation: 964

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

Upvotes: 0

Sean
Sean

Reputation: 29790

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)

Upvotes: 21

Femi
Femi

Reputation: 64700

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

Upvotes: 5

Marcin
Marcin

Reputation: 49886

Use split-window-horizontally.

Upvotes: -1

Related Questions