GNU Emacs
Emacs
Dashboard

19.1 Creating and Selecting Buffers

C-x b buffer RET

Select or create a buffer named buffer ( switch-to-buffer).

C-x 4 b buffer RET

Similar, but select buffer in another window ( switch-to-buffer-other-window).

C-x 5 b buffer RET

Similar, but select buffer in a separate frame ( switch-to-buffer-other-frame).

C-x LEFT

Select the previous buffer in the buffer list ( previous-buffer).

C-x RIGHT

Select the next buffer in the buffer list ( next-buffer).

C-u M-g M-g``C-u M-g g

Read a number n and move to line n in the most recently selected buffer other than the current buffer, in another window.

The C-x b ( switch-to-buffer) command reads a buffer name using the minibuffer. Then it makes that buffer current, and displays it in the currently-selected window. An empty input specifies the buffer that was current most recently among those not now displayed in any window.

While entering the buffer name, you can use the usual completion and history commands (see The Minibuffer). Note that C-x b, and related commands, use permissive completion with confirmation for minibuffer completion: if you type RET when the minibuffer text names a nonexistent buffer, Emacs prints ‘ [Confirm]’ and you must type a second RET to submit that buffer name. See Completion Exit, for details. For other completion options and features, see Completion Options.

If you specify a buffer that does not exist, C-x b creates a new, empty buffer that is not visiting any file, and selects it for editing. The default value of the variable major-mode determines the new buffer’s major mode; the default value is Fundamental mode. See Major Modes. One reason to create a new buffer is to use it for making temporary notes. If you try to save it, Emacs asks for the file name to use, and the buffer’s major mode is re-established taking that file name into account (see Choosing File Modes).

For conveniently switching between a few buffers, use the commands C-x LEFT and C-x RIGHT. C-x LEFT ( previous-buffer) selects the previous buffer (following the order of most recent selection in the current frame), while C-x RIGHT ( next-buffer) moves through buffers in the reverse direction. Both commands support a numeric prefix argument that serves as a repeat count.

To select a buffer in a window other than the current one (see Multiple Windows), type C-x 4 b ( switch-to-buffer-other-window). This prompts for a buffer name using the minibuffer, displays that buffer in another window, and selects that window.

Similarly, C-x 5 b ( switch-to-buffer-other-frame) prompts for a buffer name, displays that buffer in another frame (see Frames and Graphical Displays), and selects that frame. If the buffer is already being shown in a window on another frame, Emacs selects that window and frame instead of creating a new frame.

See Displaying a Buffer in a Window, for how the C-x 4 b and C-x 5 b commands get the window and/or frame to display in.

In addition, C-x C-f, and any other command for visiting a file, can also be used to switch to an existing file-visiting buffer. See Visiting Files.

C-u M-g M-g, that is goto-line with a plain prefix argument, reads a number n using the minibuffer, selects the most recently selected buffer other than the current buffer in another window, and then moves point to the beginning of line number n in that buffer. This is mainly useful in a buffer that refers to line numbers in another buffer: if point is on or just after a number, goto-line uses that number as the default for n. Note that prefix arguments other than just C-u behave differently. C-u 4 M-g M-g goes to line 4 in the current buffer, without reading a number from the minibuffer. (Remember that M-g M-g without prefix argument reads a number n and then moves to line number n in the current buffer. See Changing the Location of Point.)

Emacs uses buffer names that start with a space for internal purposes. It treats these buffers specially in minor ways—for example, by default they do not record undo information. It is best to avoid using such buffer names yourself.