Next: , Up: Buffers


24.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 list of existing buffers.
C-x <RIGHT>
Select the next buffer in the list of existing buffers.
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.

To select the buffer named bufname, type C-x b bufname <RET>. This runs the command switch-to-buffer with argument bufname. You can use completion on an abbreviation for the buffer name you want (see Completion). An empty argument to C-x b specifies the buffer that was current most recently among those not now displayed in any window.

For conveniently switching between a few buffers, use the commands C-x <LEFT> and C-x <RIGHT>. C-x <RIGHT> (previous-buffer) selects the previous buffer (following the order of most recent selection in the current frame), while C-x <LEFT> (next-buffer) moves through buffers in the reverse direction.

To select a buffer in a window other than the current one, type C-x 4 b bufname <RET>. This runs the command switch-to-buffer-other-window which displays the buffer bufname in another window. By default, if displaying the buffer causes two vertically adjacent windows to be displayed, the heights of those windows are evened out; to countermand that and preserve the window configuration, set the variable even-window-heights to nil.

Similarly, C-x 5 b buffer <RET> runs the command switch-to-buffer-other-frame which selects a buffer in another frame.

You can control how certain buffers are handled by these commands by customizing the variables special-display-buffer-names, special-display-regexps, same-window-buffer-names, and same-window-regexps. See Force Same Window, and Special Buffer Frames, for more about these variables. In addition, if the value of display-buffer-reuse-frames is non-nil, and the buffer you want to switch to is already displayed in some frame, Emacs will raise that frame.

Most buffers are created by visiting files, or by Emacs commands that want to display some text, but you can also create a buffer explicitly by typing C-x b bufname <RET>. This makes a new, empty buffer that is not visiting any file, and selects it for editing. Such buffers are used for making notes to yourself. If you try to save one, you are asked for the file name to use. The new buffer's major mode is determined by the value of default-major-mode (see Major Modes).

Note that 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.

C-u M-g M-g, that is goto-line with a prefix argument of just C-u, 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.)

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.