Emacs can split a frame into two or many windows. Multiple windows can display parts of different buffers, or different parts of one buffer. Multiple frames always imply multiple windows, because each frame has its own set of windows. Each window belongs to one and only one frame.
Each Emacs window displays one Emacs buffer at any time. A single buffer may appear in more than one window; if it does, any changes in its text are displayed in all the windows where it appears. But these windows can show different parts of the buffer, because each window has its own value of point.
At any time, one Emacs window is the selected window; the buffer this window is displaying is the current buffer. On graphical displays, the point is indicated by a solid blinking cursor in the selected window, and by a hollow box in non-selected windows. On text terminals, the cursor is drawn only in the selected window. See Displaying the Cursor.
Commands to move point affect the value of point for the selected
Emacs window only. They do not change the value of point in other
Emacs windows, even those showing the same buffer. The same is true
for buffer-switching commands such as C-x b; they do not affect
other windows at all. However, there are other commands such as
C-x 4 b that select a different window and switch buffers in it.
Also, all commands that display information in a window, including
(for example) C-h f (describe-function) and C-x C-b
(list-buffers), usually work by displaying buffers in a
nonselected window without affecting the selected window.
When multiple windows show the same buffer, they can have different regions, because they can have different values of point. However, they all have the same value for the mark, because each buffer has only one mark position.
Each window has its own mode line, which displays the buffer name, modification status and major and minor modes of the buffer that is displayed in the window. The selected window’s mode line appears in a different color. See The Mode Line, for details.
Split the selected window into two windows, one above the other
(split-window-below).
Split the selected window into two windows, positioned side by side
(split-window-right).
In the mode line of a window, split that window.
C-x 2 (split-window-below) splits the selected window
into two windows, one above the other. After splitting, the selected
window is the upper one, and the newly split-off window is below.
Both windows have the same value of point as before, and display the
same portion of the buffer (or as close to it as possible). If
necessary, the windows are scrolled to keep point on-screen. By
default, the two windows each get half the height of the original
window. A positive numeric argument specifies how many lines to give
to the top window; a negative numeric argument specifies how many
lines to give to the bottom window.
If you change the variable split-window-keep-point to
nil, C-x 2 instead adjusts the portion of the buffer
displayed by the two windows, as well as the value of point in each
window, in order to keep the text on the screen as close as possible
to what it was before; furthermore, if point was in the lower half of
the original window, the bottom window is selected instead of the
upper one.
C-x 3 (split-window-right) splits the selected window
into two side-by-side windows. The left window is the selected one;
the right window displays the same portion of the same buffer, and has
the same value of point. A positive numeric argument specifies how
many columns to give the left window; a negative numeric argument
specifies how many columns to give the right window.
When you split a window with C-x 3, each resulting window
occupies less than the full width of the frame. If it becomes too
narrow, the buffer may be difficult to read if continuation lines are
in use (see Continuation Lines). Therefore, Emacs automatically
switches to line truncation if the window width becomes narrower than
50 columns. This truncation occurs regardless of the value of the
variable truncate-lines (see Line Truncation); it is
instead controlled by the variable
truncate-partial-width-windows. If the value of this variable
is a positive integer (the default is 50), that specifies the minimum
total width for a partial-width window before automatic line
truncation occurs; if the value is nil, automatic line
truncation is disabled; and for any other non-nil value, Emacs
truncates lines in every partial-width window regardless of its width.
The total width of a window is in column units as reported by
window-total-width (see Window Sizes in The Emacs
Lisp Reference Manual), it includes the fringes, the continuation and
truncation glyphs, the margins, and the scroll bar.
On text terminals, side-by-side windows are separated by a vertical
divider which is drawn using the vertical-border face.
If you click C-mouse-2 in the mode line of a window, that splits the window, putting a vertical divider where you click. Depending on how Emacs is compiled, you can also split a window by clicking C-mouse-2 in the scroll bar, which puts a horizontal divider where you click (this feature does not work when Emacs uses GTK+ scroll bars).
By default, when you split a window, Emacs gives each of the
resulting windows dimensions that are an integral multiple of the
default font size of the frame. That might subdivide the screen
estate unevenly between the resulting windows. If you set the
variable window-resize-pixelwise to a non-nil value,
Emacs will give each window the same number of pixels (give or take
one pixel if the initial dimension was an odd number of pixels). Note
that when a frame’s pixel size is not a multiple of the frame’s
character size, at least one window may get resized pixelwise even if
this option is nil.
Select another window (other-window).
Select another window, backwards (other-window-backward).
Scroll the next window upward (scroll-other-window).
Scroll the next window downward (scroll-other-window-down).
Recenter the next window (recenter-other-window).
mouse-1, in the text area of a window, selects the window and moves point to the position clicked. Clicking in the mode line selects the window without moving point in it.
With the keyboard, you can switch windows by typing C-x o
(other-window). That is an o, for “other”, not a zero.
When there are more than two windows, this command moves through all the
windows in a cyclic order, generally top to bottom and left to right.
After the rightmost and bottommost window, it goes back to the one at
the upper left corner. A numeric argument means to move several steps
in the cyclic order of windows. A negative argument moves around the
cycle in the opposite order. When the minibuffer is active, the
minibuffer window is the last window in the cycle; you can switch from
the minibuffer window to one of the other windows, and later switch
back and finish supplying the minibuffer argument that is requested.
See Editing in the Minibuffer. C-x O is similar, but defaults to the
opposite order.
The other-window command will normally only switch to the next
window in the current frame (unless otherwise configured). If you
work in a multi-frame environment and you want windows in all frames
to be part of the cycle, you can rebind C-x o to the
next-window-any-frame command. (See Changing Key Bindings Interactively, for how to
rebind a command.)
The usual scrolling commands (see Controlling the Display) apply to the selected
window only, but there are also commands to scroll the next window.
C-M-v (scroll-other-window) scrolls the window that
C-x o would select. In other respects, the command behaves like
C-v; both move the buffer text upward relative to the window, and
take positive and negative arguments. (In the minibuffer, C-M-v
scrolls the help window associated with the minibuffer, if any, rather
than the next window in the standard cyclic order; see Editing in the Minibuffer.) C-M-S-v (scroll-other-window-down) scrolls the
next window downward in a similar way. Likewise, C-M-S-l
(recenter-other-window) behaves like C-l
(recenter-top-bottom) in the next window.
If you set mouse-autoselect-window to a non-nil value,
moving the mouse over a different window selects that window. This
feature is off by default.
C-x 4 is a prefix key for a variety of commands that switch to
a buffer in a different window—either another existing window, or a
new window created by splitting the selected window. See How display-buffer works, for how Emacs picks or creates the window to use.
Select buffer bufname in another window
(switch-to-buffer-other-window). See Creating and Selecting Buffers.
Display buffer bufname in some window, without trying to select
it (display-buffer). See Displaying a Buffer in a Window, for details
about how the window is chosen.
Visit file filename and select its buffer in another window
(find-file-other-window). See Visiting Files.
Select a Dired buffer for directory directory in another window
(dired-other-window). See Dired, the Directory Editor.
Start composing a mail message, similar to C-x m (see Sending Mail), but in another window (compose-mail-other-window).
Find the definition of an identifier, similar to M-.
(see Find Identifier References), but in another window
(xref-find-definitions-other-window).
Visit file filename read-only, and select its buffer in another
window (find-file-read-only-other-window). See Visiting Files.
A more general prefix command affects the buffer displayed by a
subsequent command invoked after this prefix command
(other-window-prefix). It requests the buffer displayed by a
subsequent command to be shown in another window.
This general prefix command requests the buffer of the next command to be displayed in the same window.
Delete the selected window (delete-window).
Delete all windows in the selected frame except the selected window
(delete-other-windows).
Delete the selected window and kill the buffer that was showing in it
(kill-buffer-and-window). The last character in this key
sequence is a zero.
Delete windows showing the specified buffer.
Make selected window taller (enlarge-window).
Make selected window wider (enlarge-window-horizontally).
Make selected window narrower (shrink-window-horizontally).
Shrink this window if its buffer doesn’t need so many lines
(shrink-window-if-larger-than-buffer).
Balance the sizes of all the windows of the selected frame
(balance-windows).
To delete the selected window, type C-x 0
(delete-window). (That is a zero.) Once a window is deleted,
the space that it occupied is given to an adjacent window (but not the
minibuffer window, even if that is active at the time). Deleting the
window has no effect on the buffer it used to display; the buffer
continues to exist, and you can still switch to it with C-x b.
The option delete-window-choose-selected controls which
window is chosen as the new selected window instead (see Deleting
Windows in The Emacs Lisp Reference Manual).
C-x 4 0 (kill-buffer-and-window) is a stronger command
than C-x 0; it kills the current buffer and then deletes the
selected window.
C-x 1 (delete-other-windows) deletes all the windows,
except the selected one; the selected window expands to use the
whole frame. (This command cannot be used while the minibuffer window
is active; attempting to do so signals an error.)
M-x delete-windows-on deletes windows that show a specific buffer. It prompts for the buffer, defaulting to the current buffer. With prefix argument of zero, C-u 0, this command deletes windows only on the current display’s frames.
The command C-x ^ (enlarge-window) makes the selected
window one line taller, taking space from a vertically adjacent window
without changing the height of the frame. With a positive numeric
argument, this command increases the window height by that many lines;
with a negative argument, it reduces the height by that many lines.
If there are no vertically adjacent windows (i.e., the window is at the
full frame height), that signals an error. The command also signals
an error if you attempt to reduce the height of any window below a
certain minimum number of lines, specified by the variable
window-min-height (the default is 4).
Similarly, C-x } (enlarge-window-horizontally) makes
the selected window wider, and C-x {
(shrink-window-horizontally) makes it narrower. These commands
signal an error if you attempt to reduce the width of any window below
a certain minimum number of columns, specified by the variable
window-min-width (the default is 10).
Mouse clicks on the mode line (see Mode Line Mouse Commands) or on window dividers (see Window Dividers) provide another way to change window heights and to split or delete windows.
C-x - (shrink-window-if-larger-than-buffer) reduces the
height of the selected window, if it is taller than necessary to show
the whole text of the buffer it is displaying. It gives the extra
lines to other windows in the frame.
You can also use C-x + (balance-windows) to balance the
sizes of all the windows of the selected frame (with the exception of
the minibuffer window, see The Minibuffer). This command makes each
horizontal pair of adjacent windows the same height, and each vertical
pair of adjacent windows the same width.
It is a common Emacs operation to display or pop up some buffer in response to a user command. There are several different ways in which commands do this.
Many commands, like C-x C-f (find-file), by default
display the buffer by “taking over” the selected window, expecting
that the user’s attention will be diverted to that buffer.
Some commands try to display intelligently, trying not to take
over the selected window, e.g., by splitting off a new window and
displaying the desired buffer there. Such commands, which include the
various help commands (see Help), work by calling
display-buffer internally. See How display-buffer works, for details.
Other commands do the same as display-buffer, and
additionally select the displaying window so that you can begin
editing its buffer. The command M-g M-n (next-error) is
one example (see Compilation Mode). Such commands work by calling
the function pop-to-buffer internally. See Switching to a Buffer in a Window in The Emacs Lisp
Reference Manual.
Commands with names ending in -other-window behave like
display-buffer, except that they never display in the selected
window, perhaps splitting the selected window to create a new one
(see How display-buffer works). Several of these commands are bound in the
C-x 4 prefix key (see Displaying in Another Window).
Commands with names ending in -other-frame behave like
display-buffer, except that they (i) never display in the
selected window and (ii) prefer to either create a new frame or use a
window on some other frame to display the desired buffer. Several of
these commands are bound in the C-x 5 prefix key.
Sometimes, a window is “dedicated” to its current buffer.
See Dedicated Windows in The Emacs Lisp Reference Manual.
display-buffer will avoid reusing dedicated windows most of the
time. This is indicated by a ‘d’ in the mode line (see The Mode Line). A window can also be strongly dedicated, which prevents any
changes to the buffer displayed in the window. This is indicated by a
‘D’ in the mode line.
Usually, dedicated windows are used to display specialized buffers,
but dedication can sometimes be useful interactively. For example,
when viewing errors with M-g M-n next-error, newly
displayed source code may replace a buffer you want to refer to. If
you dedicate a window to that buffer, the command (through
display-buffer) will prefer to use a different window instead.
You can use the command C-x w d (toggle-window-dedicated)
to toggle whether the selected window is dedicated to the current
buffer. With a prefix argument, it makes the window strongly dedicated.
display-buffer works ¶The display-buffer command (as well as commands that call it
internally) chooses a window to display by following the steps given
below. See Choosing a Window for Displaying a
Buffer in The Emacs Lisp Reference Manual, for details about how
to alter this sequence of steps.
display-buffer-same-window action function
(see Action Functions for Buffer
Display in The Emacs Lisp Reference Manual) to the option
display-buffer-alist (see Choosing a Window
for Displaying a Buffer in The Emacs Lisp Reference Manual).
For example, to display the buffer *scratch* preferably in the
selected window write:
(setopt
display-buffer-alist
'(("\\*scratch\\*" (display-buffer-same-window))))
By default, display-buffer-alist is nil.
reusable-frames action alist entry
(see Action Alists for Buffer
Display in The Emacs Lisp Reference Manual). See the
next step for an example of how to do that.
display-buffer-base-action (see Choosing a Window for Displaying a Buffer in The Emacs
Lisp Reference Manual) as follows:
(setopt display-buffer-base-action '((display-buffer-reuse-window display-buffer-pop-up-frame) (reusable-frames . 0)))
This customization will also try to make the preceding step search for a reusable window on all visible or iconified frames.
The split can be either vertical or horizontal, depending on the
variables split-height-threshold and
split-width-threshold. These variables should have integer
values. If split-height-threshold is smaller than the chosen
window’s height, the split puts the new window below. Otherwise, if
split-width-threshold is smaller than the window’s width, the
split puts the new window on the right. If neither condition holds,
Emacs tries to split so that the new window is below—but only if the
window was not split before (to avoid excessive splitting). Whether
Emacs tries first to split vertically or horizontally when both
conditions hold is determined by the value of
split-window-preferred-direction. Its default is longest,
which means to split vertically if the window’s frame is taller than it
is wide (a portrait frame), and split horizontally if its wider
than it’s tall (a landscape frame). The values vertical
and horizontal always prefer, respectively, the vertical or the
horizontal split.
reusable-frames action alist entry (see above) the
window may be also on another frame.
Some buffers are shown in windows for perusal rather than for editing. Help commands (see Help) typically use a buffer called *Help* for that purpose, minibuffer completion (see Completion) uses a buffer called *Completions*, etc. Such buffers are usually displayed only for a short period of time.
Normally, Emacs chooses the window for such temporary displays via
display-buffer, as described in the previous subsection. The
*Completions* buffer, on the other hand, is normally displayed
in a window at the bottom of the selected frame, regardless of the
number of windows already shown on that frame.
If you prefer Emacs to display a temporary buffer in a different
fashion, customize the variable display-buffer-alist
(see Choosing a Window for Displaying a Buffer in The Emacs Lisp Reference Manual) appropriately. For example,
to display *Completions* always below the selected window, use
the following form in your initialization file (see The Emacs Initialization File):
(setopt
display-buffer-alist
'(("\\*Completions\\*" display-buffer-below-selected)))
The *Completions* buffer is also special in the sense that
Emacs usually tries to make its window just as large as necessary to
display all of its contents. To resize windows showing other
temporary displays, like, for example, the *Help* buffer, turn
on the minor mode (see Minor Modes) temp-buffer-resize-mode
(see Temporary Displays in The Emacs Lisp
Reference Manual).
The maximum size of windows resized by temp-buffer-resize-mode
can be controlled by customizing the options
temp-buffer-max-height and temp-buffer-max-width
(see Temporary Displays in The Emacs Lisp
Reference Manual), and cannot exceed the size of the containing frame.
Buffers showing warnings (such as byte-compilation warnings,
see Byte Compilation Functions in The
Emacs Lisp Reference Manual) are also by default shown in a window at
the bottom of the selected frame. You can control this using the
variable warning-display-at-bottom: if set to nil, Emacs
will use the normal logic of display-buffer (see How display-buffer works) instead, and you can customize that via
display-buffer-alist.
Winner mode is a global minor mode that records the changes in the
window configuration (i.e., how the frames are partitioned into
windows), so that you can undo them. You can toggle Winner mode
with M-x winner-mode, or by customizing the variable
winner-mode. When the mode is enabled, C-c left
(winner-undo) undoes the last window configuration change. If
you change your mind while undoing, you can redo the changes you had
undone using C-c right (M-x winner-redo). To prevent
Winner mode from binding C-c left and C-c right, you can
customize the variable winner-dont-bind-my-keys to a
non-nil value. By default, Winner mode stores a maximum of 200
window configurations per frame, but you can change that by modifying
the variable winner-ring-size. If there are some buffers whose
windows you wouldn’t want Winner mode to restore, add their names to
the list variable winner-boring-buffers or to the regexp
winner-boring-buffers-regexp.
Follow mode (M-x follow-mode) synchronizes several windows on the same buffer so that they always display adjacent sections of that buffer. See Follow Mode.
The Windmove package defines commands for moving directionally
between neighboring windows in a frame. M-x windmove-right
selects the window immediately to the right of the currently selected
one, and similarly for the left, up, and down counterparts.
windmove-default-keybindings binds these commands to
S-right etc.; doing so disables shift selection for those keys
(see Shift Selection). In the same way as key bindings can be
defined for commands that select windows directionally, you can use
windmove-display-default-keybindings to define keybindings for
commands that specify in what direction to display the window for the
buffer that the next command is going to display. Also there is
windmove-delete-default-keybindings to define keybindings for
commands that delete windows directionally, and
windmove-swap-states-default-keybindings that defines
key bindings for commands that swap the window contents of the selected
window with the window in the specified direction.
The command M-x compare-windows lets you compare the text shown in different windows. See Comparing Files.
Scroll All mode (M-x scroll-all-mode) is a global minor mode that causes scrolling commands and point motion commands to apply to every single window.
The command global-tab-line-mode toggles the display of
a tab line on the top screen line of each window. The Tab Line
shows special buttons (“tabs”) for each buffer that was displayed in
a window, and allows switching to any of these buffers by clicking the
corresponding button. Clicking on the + icon adds a new buffer
to the window-local tab line of buffers, and clicking on the x
icon of a tab deletes it. The mouse wheel on the tab line scrolls
the tabs horizontally.
Touch screen input (see Touchscreen Input and Virtual Keyboards) can also be used to interact with the “tab line”. Long-pressing (see Using Emacs on Touchscreens) a tab will display a context menu with items that operate on the tab that was pressed; tapping a tab itself will result in switching to that tab’s buffer, and tapping a button on the tab line will behave as if it was clicked with mouse-1.
Selecting the previous window-local tab is the same as typing C-x
LEFT (previous-buffer), selecting the next tab is the
same as C-x RIGHT (next-buffer). Both commands
support a numeric prefix argument as a repeat count.
You can customize the variable tab-line-tabs-function to define
the preferred contents of the tab line. By default, it displays all
buffers previously visited in the window, as described above. But you
can also set it to display a list of buffers with the same major mode
as the current buffer, or to display buffers grouped by their major
mode, where clicking on the mode name in the first tab displays a list
of all major modes where you can select another group of buffers.
Note that the Tab Line is different from the Tab Bar (see Tab Bars). Whereas tabs on the Tab Bar at the top of each frame are used to switch between window configurations containing several windows with buffers, tabs on the Tab Line at the top of each window are used to switch between buffers in the window.
Also note that the tab line displays in the same space as the window
tool bar, so only one of them can be displayed at any given time, unless
you customize the value of tab-line-format in Lisp to add
(:eval (tab-line-format)) to tab-line-format.
See Mode Line Format in The Emacs Lisp Reference Manual.
The command global-window-tool-bar-mode toggles the display of
a tool bar at the top of each window. When enabled, multiple windows
can display their own tool bar simultaneously. To conserve space, a
window tool bar is hidden if there are no buttons to show, i.e. if
tool-bar-map is nil.
If you want to toggle the display of a window tool bar for only some
buffers, run the command window-tool-bar-mode in those buffers.
This is useful to put in a mode hook. For example, if you want the window
tool bar to appear only for buffers that do not represent files and have
a custom tool bar, you could add the following code to your init file
(see The Emacs Initialization File):
(add-hook 'special-mode-hook 'window-tool-bar-mode)
On graphical displays the window tool bar can be displayed in several
different styles. By default, the window tool bar displays items as
just images. To impose a specific style, customize the variable
window-tool-bar-style.
On text-only displays the window tool bar only shows text for each button even if another style is specified.
Emacs can also display a single tool bar at the top of frames (see Tool Bars).
Note that the window tool bar displays in the same space as the tab
line, so only one of them can be displayed at any given time, unless you
customize the value of tab-line-format to add (:eval (window-tool-bar-string)) to tab-line-format. See Mode Line
Format in The Emacs Lisp Reference Manual.