how to split windows vertically and horizontally in emacs

One of the most useful features of modern text editors and IDEs is the ability to split the screen into multiple windows and edit/view different files in them. Emacs is no exception and makes it easy to do so.

split window horizontally

In order to split the window horizontally, use the command split-window-horizontally. This will split the currently active buffer into two horizontally. You can also use the command split-window-right.

M-x split-window-horizontally

The command is mapped to C-x 3 by default. Each of the split buffer will display the same buffer. You can now open a different file in the new buffer if you want to show a different buffer.

split window vertically

To split the window vertically, use the command split-window-vertically. This will split the current buffer into two vertically. You can use the command split-window-below as well.

M-x split-window-vertically

The keyboard shortcut for the command is C-x 2 by default. By default, the new window or buffer will display the currently active buffer.

split window with a different file or buffer

Most times I need to display a different buffer than the current buffer when I am splitting the window, which means I have to open or switch to a different buffer after splitting. You can choose to display a different buffer in the new split window with the following configuration.

(defun vsplit-last-buffer ()
(interactive)
(split-window-vertically)
(other-window 1 nil)
(switch-to-next-buffer))
(defun hsplit-last-buffer ()
(interactive)
(split-window-horizontally)
(other-window 1 nil)
(switch-to-next-buffer))
(global-set-key (kbd "C-x 2") 'vsplit-last-buffer)
(global-set-key (kbd "C-x 3") 'hsplit-last-buffer)

The above code will split the window and show the buffer that was active before the current buffer. I find that to be more useful than to display the same buffer. But some times the you might need to show the same buffer and scroll them independently in which case this may not be helpful.

switching between windows

Once you have split the window or buffer, you will want to switch between them. The command to switch between the displayed buffer is other-window and is mapped to the shortcut C-x o.

M-x other-window

There are several packages that make switching between windows easier if you are happen to have several windows open at the same time. I use ace-window which is quite nice.

resizing the split windows

If you are able to use the mouse, then you can resize the buffers by clicking on the separator between the two buffers and dragging them. This seems to be the easiest and most effective if you don’t have to do it often.

The shrink-window-horizontally and enlarge-window-horizontally are two commands that are useful to adjust the width of the windows. There are mapped to C-x { and C-x } respectively.

If you want to adjust the height, then the two commands to do that are shrink-window and enlarge-window. By default, the enlarge-window is bound to C-x ^. I did not find any binding for shrink-window but you could add one if needed.

closing the split windows

Once you have several split windows open, at some point you will need to close some of them if not all of them. The easiest is to close all except the current buffer. The command to do that is delete-other-windows. It is bound to the C-x 1 by default.

The command to close just the current window is delete-window and this is bound to C-x 0. This will leave the other windows intact and just close the current or active window.