Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 968
SuperElectric Avatar asked Jul 14 '11 17:07

SuperElectric


People also ask

How do I open two Emacs Files?

Much better to use the multiple buffer feature of emacs. If you are editing the first file and want to start editing the second file, simply use the hot key C-x C-f or the menu selection File->Open File to start the second file. The second file is loaded into its own buffer.

How do I split the screen in Emacs?

You can split a window horizontally or vertically by clicking C-Mouse-2 in the mode line or the scroll bar.

How do I unsplit windows Emacs?

Use the C-x b command or the Buffers menu. Or if you haven't opened the file, yet, use the C-x C-f command to open the new file in a new buffer. To unsplit the window, use the command C-x 1 or right-click on either title bar. The inactive panel disappears.

What is a buffer in Emacs?

Buffers in Emacs editing are objects that have distinct names and hold text that can be edited. Buffers appear to Lisp programs as a special data type. You can think of the contents of a buffer as a string that you can extend; insertions and deletions may occur in any part of the buffer.


2 Answers

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) 
like image 134
Sean Avatar answered Sep 25 '22 08:09

Sean


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

like image 25
Francois G Avatar answered Sep 26 '22 08:09

Francois G