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.
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.
You can split a window horizontally or vertically by clicking C-Mouse-2 in the mode line or the scroll bar.
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.
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.
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)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With