ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

23.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. While entering the buffer name, you can use the usual minibuffer completion and history commands (see Minibuffer). An empty argument to C-x b specifies the buffer that was current most recently among those not now displayed in any window.

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. First, however, Emacs might prompt you for confirmation, in case you entered the wrong buffer name. Emacs asks for confirmation only if the last key you typed, before submitting the minibuffer input with <RET>, was <TAB> (minibuffer-complete). This catches a common mistake, in which one types <RET> before realizing that <TAB> did not complete far enough to yield the desired buffer name (see Completion). Emacs asks for confirmation by putting the message ‘[Confirm]’ in the minibuffer; type <RET> again to confirm and visit the buffer.

The variable confirm-nonexistent-file-or-buffer controls whether Emacs asks for confirmation before visiting a buffer that did not previously exist. The default value, after-completion, gives the behavior we have just described. If the value is nil, Emacs never asks for confirmation; for any other non-nil value, Emacs always asks for confirmation. This variable also affects the find-file command (see Visiting).

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. The default value of the variable major-mode determines the new buffer's major mode; the default value is Fundamental mode. See Major Modes.

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 (switch-to-buffer-other-window). This prompts for a buffer name using the minibuffer, displays that buffer in another window, and selects that 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 (switch-to-buffer-other-frame) prompts for a buffer name, displays that buffer in another frame, and selects that frame.

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.

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 just raise that frame.

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 Moving 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.

blog comments powered by Disqus