Choosing a Window for Displaying a Buffer
The command display-buffer flexibly chooses a window for display, and displays a specified buffer in that window. It can be called interactively, via the key binding C-x 4 C-o. It is also used as a subroutine by many functions and commands, including switch-to-buffer and pop-to-buffer (see Switching to a Buffer in a Window).
This command performs several complex steps to find a window to display in. These steps are described by means of display actions, which have the form (functions . alist). Here, functions is either a single function or a list of functions, referred to as “action functions” (see Action Functions for Buffer Display); and alist is an association list, referred to as “action alist” (see Action Alists for Buffer Display). See The Zen of Buffer Display, for samples of display actions.
An action function accepts two arguments: the buffer to display and an action alist. It attempts to display the buffer in some window, picking or creating a window according to its own criteria. If successful, it returns the window; otherwise, it returns nil.
display-buffer works by combining display actions from several sources, and calling the action functions in turn, until one of them manages to display the buffer and returns a non-nil value.
- Command: display-buffer buffer-or-name &optional action frame
-
This command makes buffer-or-name appear in some window, without selecting the window or making the buffer current. The argument buffer-or-name must be a buffer or the name of an existing buffer. The return value is the window chosen to display the buffer, or
nilif no suitable window was found.The optional argument action, if non-
nil, should normally be a display action (described above).display-bufferbuilds a list of action functions and an action alist, by consolidating display actions from the following sources (in order of their precedence, from highest to lowest):- The variable
display-buffer-overriding-action. - The user option
display-buffer-alist. - The action argument.
- The user option
display-buffer-base-action. - The constant
display-buffer-fallback-action.
In practice this means that
display-bufferbuilds a list of all action functions specified by these display actions. The first element of this list is the first action function specified bydisplay-buffer-overriding-action, if any. Its last element isdisplay-buffer-pop-up-frame—the last action function specified bydisplay-buffer-fallback-action. Duplicates are not removed from this list—hence one and the same action function may be called multiple times during one call ofdisplay-buffer.display-buffercalls the action functions specified by this list in turn, passing the buffer as the first argument and the combined action alist as the second argument, until one of the functions returns non-nil. See Precedence of Action Functions, for examples how display actions specified by different sources are processed bydisplay-buffer.Note that the second argument is always the list of all action alist entries specified by the sources named above. Hence, the first element of that list is the first action alist entry specified by
display-buffer-overriding-action, if any. Its last element is the last alist entry ofdisplay-buffer-base-action, if any (the action alist ofdisplay-buffer-fallback-actionis empty).Note also, that the combined action alist may contain duplicate entries and entries for the same key with different values. As a rule, action functions always use the first association of a key they find. Hence, the association an action function uses is not necessarily the association provided by the display action that specified that action function,
The argument action can also have a non-
nil, non-list value. This has the special meaning that the buffer should be displayed in a window other than the selected one, even if the selected window is already displaying it. If called interactively with a prefix argument, action ist. Lisp programs should always supply a list value.The optional argument frame, if non-
nil, specifies which frames to check when deciding whether the buffer is already displayed. It is equivalent to adding an element(reusable-frames . frame)to the action alist of action (see Action Alists for Buffer Display). The frame argument is provided for compatibility reasons, Lisp programs should not use it. - The variable
- Variable: display-buffer-overriding-action
The value of this variable should be a display action, which is treated with the highest priority by
display-buffer. The default value is an empty display action, i.e.,(nil . nil).
- User Option: display-buffer-alist
-
The value of this option is an alist mapping conditions to display actions. Each condition is passed to
buffer-match-p(see The Buffer List), along with the buffer name and the action argument passed todisplay-buffer. If it returns a non-nilvalue, thendisplay-bufferuses the corresponding display action to display the buffer. Caveat: if you usederived-modeormajor-modeas condition,buffer-match-pcould fail to report a match ifdisplay-bufferis called before the major mode of the buffer is set.If the caller of
display-bufferpasses a category as a symbol in its action argument, then you can use the same category indisplay-buffer-alistto match buffers with different names, for example:(add-to-list 'display-buffer-alist '((category . comint) (display-buffer-same-window) (inhibit-same-window . nil))) (display-buffer (get-buffer-create "*my-shell*") '(nil (category . comint)))See
buffer-match-p.Regardless of the displayed buffer’s name the caller defines a category as a symbol
comint. Thendisplay-buffer-alistmatches this category for all buffers displayed with the same category. This avoids the need to construct a complex regular expression that matches a buffer name.You can also match on the command now being executed. This is useful when different commands display buffers with the same name, but you want different command’s buffers to be displayed differently. For example:
(add-to-list 'display-buffer-alist '((and "^\\*vc-diff\\*" (not (this-command . vc-root-diff))) nil (post-command-select-window . nil)))This means that windows displaying VC diffs, such as those generated by C-x v = (
vc-diff, see Old Revisions in the Emacs Manual) are not left selected, except for windows displaying buffers generated by C-x v D (vc-root-diff), which are left selected.
- User Option: display-buffer-base-action
The value of this option should be a display action. This option can be used to define a standard display action for calls to
display-buffer.
- Constant: display-buffer-fallback-action
This display action specifies the fallback behavior for
display-bufferif no other display actions are given.
Copyright © 1990-1996, 1998-2026 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Choosing-Window.html