1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 2000, 2001
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Buffers, Windows, Files, Top
6 @chapter Using Multiple Buffers
9 The text you are editing in Emacs resides in an object called a
10 @dfn{buffer}. Each time you visit a file, a buffer is created to hold the
11 file's text. Each time you invoke Dired, a buffer is created to hold the
12 directory listing. If you send a message with @kbd{C-x m}, a buffer named
13 @samp{*mail*} is used to hold the text of the message. When you ask for a
14 command's documentation, that appears in a buffer called @samp{*Help*}.
16 @cindex selected buffer
17 @cindex current buffer
18 At any time, one and only one buffer is @dfn{current}. It is also
19 called the @dfn{selected buffer}. Often we say that a command operates on
20 ``the buffer'' as if there were only one; but really this means that the
21 command operates on the current buffer (most commands do).
23 When Emacs has multiple windows, each window has its own chosen
24 buffer and displays it; at any time, only one of the windows is
25 selected, and its chosen buffer is the current buffer. Each window's
26 mode line normally displays the name of the window's chosen buffer
29 Each buffer has a name, which can be of any length, and you can select
30 any buffer by giving its name. Most buffers are made by visiting files,
31 and their names are derived from the files' names. But you can also create
32 an empty buffer with any name you want. A newly started Emacs has a buffer
33 named @samp{*scratch*} which can be used for evaluating Lisp expressions in
34 Emacs. The distinction between upper and lower case matters in buffer
37 Each buffer records individually what file it is visiting, whether it is
38 modified, and what major mode and minor modes are in effect in it
39 (@pxref{Major Modes}). Any Emacs variable can be made @dfn{local to} a
40 particular buffer, meaning its value in that buffer can be different from
41 the value in other buffers. @xref{Locals}.
43 @cindex buffer size, maximum
44 A buffer's size cannot be larger than some maximum, which is defined
45 by the largest buffer position representable by the @dfn{Emacs integer}
46 data type. This is because Emacs tracks buffer positions using that
47 data type. For 32-bit machines, the largest buffer size is 128
51 * Select Buffer:: Creating a new buffer or reselecting an old one.
52 * List Buffers:: Getting a list of buffers that exist.
53 * Misc Buffer:: Renaming; changing read-onlyness; copying text.
54 * Kill Buffer:: Killing buffers you no longer need.
55 * Several Buffers:: How to go through the list of all buffers
56 and operate variously on several of them.
57 * Indirect Buffers:: An indirect buffer shares the text of another buffer.
58 * Buffer Convenience:: Convenience and customization features for
63 @section Creating and Selecting Buffers
64 @cindex change buffers
65 @cindex switch buffers
68 @item C-x b @var{buffer} @key{RET}
69 Select or create a buffer named @var{buffer} (@code{switch-to-buffer}).
70 @item C-x 4 b @var{buffer} @key{RET}
71 Similar, but select @var{buffer} in another window
72 (@code{switch-to-buffer-other-window}).
73 @item C-x 5 b @var{buffer} @key{RET}
74 Similar, but select @var{buffer} in a separate frame
75 (@code{switch-to-buffer-other-frame}).
79 @findex switch-to-buffer
80 To select the buffer named @var{bufname}, type @kbd{C-x b @var{bufname}
81 @key{RET}}. This runs the command @code{switch-to-buffer} with argument
82 @var{bufname}. You can use completion on an abbreviation for the buffer
83 name you want (@pxref{Completion}). An empty argument to @kbd{C-x b}
84 specifies the buffer that was current most recently among those not
85 now displayed in any window.
88 @findex switch-to-buffer-other-window
89 @vindex even-window-heights
90 To select a buffer in a window other than the current one, type
91 @kbd{C-x 4 b @var{bufname} @key{RET}}. This runs the command
92 @code{switch-to-buffer-other-window} which displays the buffer
93 @var{bufname} in another window. By default, if displaying the buffer
94 causes two vertically adjacent windows to be displayed, the heights of
95 those windows are evened out; to countermand that and preserve the
96 window configuration, set the variable @code{even-window-heights} to
100 @findex switch-to-buffer-other-frame
101 Similarly, @kbd{C-x 5 b @var{buffer} @key{RET}} runs the command
102 @code{switch-to-buffer-other-frame} which selects a buffer in another
105 @vindex display-buffer-reuse-frames
106 You can control how certain buffers are handled by these commands by
107 customizing the variables @code{special-display-buffer-names},
108 @code{special-display-regexps}, @code{same-window-buffer-names}, and
109 @code{same-window-regexps}. See @ref{Force Same Window}, and
110 @ref{Special Buffer Frames}, for more about these variables. In
111 addition, if the value of @code{display-buffer-reuse-frames} is
112 non-@code{nil}, and the buffer you want to switch to is already
113 displayed in some frame, Emacs will raise that frame.
115 Most buffers are created by visiting files, or by Emacs commands that
116 want to display some text, but you can also create a buffer explicitly
117 by typing @kbd{C-x b @var{bufname} @key{RET}}. This makes a new, empty
118 buffer that is not visiting any file, and selects it for editing. Such
119 buffers are used for making notes to yourself. If you try to save one,
120 you are asked for the file name to use. The new buffer's major mode is
121 determined by the value of @code{default-major-mode} (@pxref{Major
124 Note that @kbd{C-x C-f}, and any other command for visiting a file,
125 can also be used to switch to an existing file-visiting buffer.
128 Emacs uses buffer names that start with a space for internal purposes.
129 It treats these buffers specially in minor ways---for example, by
130 default they do not record undo information. It is best to avoid using
131 such buffer names yourself.
134 @section Listing Existing Buffers
138 List the existing buffers (@code{list-buffers}).
141 @cindex listing current buffers
144 To display a list of all the buffers that exist, type @kbd{C-x C-b}.
145 Each line in the list shows one buffer's name, major mode and visited
146 file. The buffers are listed in the order that they were current; the
147 buffers that were current most recently come first.
149 @samp{*} at the beginning of a line indicates the buffer is ``modified.''
150 If several buffers are modified, it may be time to save some with @kbd{C-x s}
151 (@pxref{Saving}). @samp{%} indicates a read-only buffer. @samp{.} marks the
152 current buffer. Here is an example of a buffer list:@refill
155 MR Buffer Size Mode File
156 -- ------ ---- ---- ----
157 .* emacs.tex 383402 Texinfo /u2/emacs/man/emacs.tex
158 *Help* 1287 Fundamental
159 files.el 23076 Emacs-Lisp /u2/emacs/lisp/files.el
160 % RMAIL 64042 RMAIL /u/rms/RMAIL
161 *% man 747 Dired /u2/emacs/man/
162 net.emacs 343885 Fundamental /u/rms/net.emacs
163 fileio.c 27691 C /u2/emacs/src/fileio.c
164 NEWS 67340 Text /u2/emacs/etc/NEWS
165 *scratch* 0 Lisp Interaction
169 Note that the buffer @samp{*Help*} was made by a help request; it is
170 not visiting any file. The buffer @code{man} was made by Dired on the
171 directory @file{/u2/emacs/man/}. You can list only buffers that are
172 visiting files by giving the command a prefix; for instance, by typing
175 @code{list-buffers} omits buffers whose name begins with a blank,
176 unless they visit files: such buffers are used internally by Emacs.
180 @section Miscellaneous Buffer Operations
184 Toggle read-only status of buffer (@code{toggle-read-only}).
185 @item M-x rename-buffer @key{RET} @var{name} @key{RET}
186 Change the name of the current buffer.
187 @item M-x rename-uniquely
188 Rename the current buffer by adding @samp{<@var{number}>} to the end.
189 @item M-x view-buffer @key{RET} @var{buffer} @key{RET}
190 Scroll through buffer @var{buffer}.
194 @vindex buffer-read-only
195 @cindex read-only buffer
196 A buffer can be @dfn{read-only}, which means that commands to change
197 its contents are not allowed. The mode line indicates read-only
198 buffers with @samp{%%} or @samp{%*} near the left margin. Read-only
199 buffers are usually made by subsystems such as Dired and Rmail that
200 have special commands to operate on the text; also by visiting a file
201 whose access control says you cannot write it.
203 @findex toggle-read-only
204 If you wish to make changes in a read-only buffer, use the command
205 @kbd{C-x C-q} (@code{toggle-read-only}). It makes a read-only buffer
206 writable, and makes a writable buffer read-only. This
207 works by setting the variable @code{buffer-read-only}, which has a local
208 value in each buffer and makes the buffer read-only if its value is
209 non-@code{nil}. If you have files under version control, you may find
210 it convenient to bind @kbd{C-x C-q} to @code{vc-toggle-read-only}
211 instead. Then, typing @kbd{C-x C-q} not only changes the read-only
212 flag, but it also checks the file in or out. @xref{Version
215 @findex rename-buffer
216 @kbd{M-x rename-buffer} changes the name of the current buffer. Specify
217 the new name as a minibuffer argument. There is no default. If you
218 specify a name that is in use for some other buffer, an error happens and
221 @kbd{M-x rename-uniquely} renames the current buffer to a similar name
222 with a numeric suffix added to make it both different and unique. This
223 command does not need an argument. It is useful for creating multiple
224 shell buffers: if you rename the @samp{*Shell*} buffer, then do @kbd{M-x
225 shell} again, it makes a new shell buffer named @samp{*Shell*};
226 meanwhile, the old shell buffer continues to exist under its new name.
227 This method is also good for mail buffers, compilation buffers, and most
228 Emacs features that create special buffers with particular names.
231 @kbd{M-x view-buffer} is much like @kbd{M-x view-file} (@pxref{Misc
232 File Ops}) except that it examines an already existing Emacs buffer.
233 View mode provides commands for scrolling through the buffer
234 conveniently but not for changing it. When you exit View mode with
235 @kbd{q}, that switches back to the buffer (and the position) which was
236 previously displayed in the window. Alternatively, if you exit View
237 mode with @kbd{e}, the buffer and the value of point that resulted from
238 your perusal remain in effect.
240 The commands @kbd{M-x append-to-buffer} and @kbd{M-x insert-buffer}
241 can be used to copy text from one buffer to another. @xref{Accumulating
245 @section Killing Buffers
247 @cindex killing buffers
248 If you continue an Emacs session for a while, you may accumulate a
249 large number of buffers. You may then find it convenient to @dfn{kill}
250 the buffers you no longer need. On most operating systems, killing a
251 buffer releases its space back to the operating system so that other
252 programs can use it. Here are some commands for killing buffers:
255 @item C-x k @var{bufname} @key{RET}
256 Kill buffer @var{bufname} (@code{kill-buffer}).
257 @item M-x kill-some-buffers
258 Offer to kill each buffer, one by one.
262 @findex kill-some-buffers
265 @kbd{C-x k} (@code{kill-buffer}) kills one buffer, whose name you
266 specify in the minibuffer. The default, used if you type just
267 @key{RET} in the minibuffer, is to kill the current buffer. If you
268 kill the current buffer, another buffer becomes current: one that was
269 current in the recent past but is not displayed in any window now. If
270 you ask to kill a file-visiting buffer that is modified (has unsaved
271 editing), then you must confirm with @kbd{yes} before the buffer is
274 The command @kbd{M-x kill-some-buffers} asks about each buffer, one by
275 one. An answer of @kbd{y} means to kill the buffer. Killing the current
276 buffer or a buffer containing unsaved changes selects a new buffer or asks
277 for confirmation just like @code{kill-buffer}.
279 The buffer menu feature (@pxref{Several Buffers}) is also convenient
280 for killing various buffers.
282 @vindex kill-buffer-hook
283 If you want to do something special every time a buffer is killed, you
284 can add hook functions to the hook @code{kill-buffer-hook} (@pxref{Hooks}).
286 @findex clean-buffer-list
287 If you run one Emacs session for a period of days, as many people do,
288 it can fill up with buffers that you used several days ago. The command
289 @kbd{M-x clean-buffer-list} is a convenient way to purge them; it kills
290 all the unmodified buffers that you have not used for a long time. An
291 ordinary buffer is killed if it has not been displayed for three days;
292 however, you can specify certain buffers that should never be killed
293 automatically, and others that should be killed if they have been unused
296 @cindex Midnight mode
297 @vindex midnight-mode
298 @vindex midnight-hook
299 You can also have this buffer purging done for you, every day at
300 midnight, by enabling Midnight mode. Midnight mode operates each day at
301 midnight; at that time, it runs @code{clean-buffer-list}, or whichever
302 functions you have placed in the normal hook @code{midnight-hook}
305 To enable Midnight mode, use the Customization buffer to set the
306 variable @code{midnight-mode} to @code{t}. @xref{Easy Customization}.
308 @node Several Buffers
309 @section Operating on Several Buffers
312 The @dfn{buffer-menu} facility is like a ``Dired for buffers''; it allows
313 you to request operations on various Emacs buffers by editing an Emacs
314 buffer containing a list of them. You can save buffers, kill them
315 (here called @dfn{deleting} them, for consistency with Dired), or display
319 @item M-x buffer-menu
320 Begin editing a buffer listing all Emacs buffers.
321 @item M-x buffer-menu-other-window.
322 Similar, but do it in another window.
326 @findex buffer-menu-other-window
327 The command @code{buffer-menu} writes a list of all Emacs
328 buffers@footnote{Buffers which don't visit files and whose names begin
329 with a space are omitted: these are used internally by Emacs.} into the
330 buffer @samp{*Buffer List*}, and selects that buffer in Buffer Menu
331 mode. The list in the @samp{*Buffer List*} buffer looks exactly as
332 described in @ref{List Buffers}. The buffer is read-only, and can be
333 changed only through the special commands described in this section.
334 The usual Emacs cursor motion commands can be used in the @samp{*Buffer
335 List*} buffer. The following commands apply to the buffer described on
340 Request to delete (kill) the buffer, then move down. The request
341 shows as a @samp{D} on the line, before the buffer name. Requested
342 deletions take place when you type the @kbd{x} command.
344 Like @kbd{d} but move up afterwards instead of down.
346 Request to save the buffer. The request shows as an @samp{S} on the
347 line. Requested saves take place when you type the @kbd{x} command.
348 You may request both saving and deletion for the same buffer.
350 Perform previously requested deletions and saves.
352 Remove any request made for the current line, and move down.
354 Move to previous line and remove any request made for that line.
357 The @kbd{d}, @kbd{C-d}, @kbd{s} and @kbd{u} commands to add or remove
358 flags also move down (or up) one line. They accept a numeric argument
361 These commands operate immediately on the buffer listed on the current
366 Mark the buffer ``unmodified.'' The command @kbd{~} does this
367 immediately when you type it.
369 Toggle the buffer's read-only flag. The command @kbd{%} does
370 this immediately when you type it.
372 Visit the buffer as a tags table. @xref{Select Tags Table}.
375 There are also commands to select another buffer or buffers:
379 Quit the buffer menu---immediately display the most recent formerly
380 visible buffer in its place.
383 Immediately select this line's buffer in place of the @samp{*Buffer
386 Immediately select this line's buffer in another window as if by
387 @kbd{C-x 4 b}, leaving @samp{*Buffer List*} visible.
389 Immediately display this line's buffer in another window, but don't
392 Immediately select this line's buffer in a full-screen window.
394 Immediately set up two windows, with this line's buffer in one, and the
395 previously current buffer (aside from the buffer @samp{*Buffer List*})
398 Bury the buffer listed on this line.
400 Mark this line's buffer to be displayed in another window if you exit
401 with the @kbd{v} command. The request shows as a @samp{>} at the
402 beginning of the line. (A single buffer may not have both a delete
403 request and a display request.)
405 Immediately select this line's buffer, and also display in other windows
406 any buffers previously marked with the @kbd{m} command. If you have not
407 marked any buffers, this command is equivalent to @kbd{1}.
410 All that @code{buffer-menu} does directly is create and switch to a
411 suitable buffer, and turn on Buffer Menu mode. Everything else
412 described above is implemented by the special commands provided in
413 Buffer Menu mode. One consequence of this is that you can switch from
414 the @samp{*Buffer List*} buffer to another Emacs buffer, and edit there.
415 You can reselect the @samp{*Buffer List*} buffer later, to perform the
416 operations already requested, or you can kill it, or pay no further
419 The only difference between @code{buffer-menu} and @code{list-buffers}
420 is that @code{buffer-menu} switches to the @samp{*Buffer List*} buffer
421 in the selected window; @code{list-buffers} displays it in another
422 window. If you run @code{list-buffers} (that is, type @kbd{C-x C-b})
423 and select the buffer list manually, you can use all of the commands
426 The buffer @samp{*Buffer List*} is not updated automatically when
427 buffers are created and killed; its contents are just text. If you have
428 created, deleted or renamed buffers, the way to update @samp{*Buffer
429 List*} to show what you have done is to type @kbd{g}
430 (@code{revert-buffer}) or repeat the @code{buffer-menu} command.
432 The command @code{buffer-menu-other-window} works the same as
433 @code{buffer-menu}, except that it displays the buffers list in
436 @node Indirect Buffers
437 @section Indirect Buffers
438 @cindex indirect buffer
441 An @dfn{indirect buffer} shares the text of some other buffer, which
442 is called the @dfn{base buffer} of the indirect buffer. In some ways it
443 is the analogue, for buffers, of a symbolic link between files.
446 @findex make-indirect-buffer
447 @item M-x make-indirect-buffer @key{RET} @var{base-buffer} @key{RET} @var{indirect-name} @key{RET}
448 Create an indirect buffer named @var{indirect-name} whose base buffer
449 is @var{base-buffer}.
450 @findex clone-indirect-buffer
451 @item M-x clone-indirect-buffer @key{RET}
452 Create an indirect buffer that is a twin copy of the current buffer.
455 @findex clone-indirect-buffer-other-window
456 Create an indirect buffer that is a twin copy of the current buffer, and
457 select it in another window (@code{clone-indirect-buffer-other-window}).
460 The text of the indirect buffer is always identical to the text of its
461 base buffer; changes made by editing either one are visible immediately
462 in the other. But in all other respects, the indirect buffer and its
463 base buffer are completely separate. They have different names,
464 different values of point, different narrowing, different markers,
465 different major modes, and different local variables.
467 An indirect buffer cannot visit a file, but its base buffer can. If
468 you try to save the indirect buffer, that actually works by saving the
469 base buffer. Killing the base buffer effectively kills the indirect
470 buffer, but killing an indirect buffer has no effect on its base buffer.
472 One way to use indirect buffers is to display multiple views of an
473 outline. @xref{Outline Views}.
475 @cindex multiple @samp{*info*} and @samp{*Help*} buffers
476 A quick and handy way to make an indirect buffer is with the command
477 @kbd{M-x clone-indirect-buffer}. It creates and selects an indirect
478 buffer whose base buffer is the current buffer. With a numeric
479 argument, it prompts for the name of the indirect buffer; otherwise it
480 defaults to the name of the current buffer, modifying it by adding a
481 @samp{<@var{n}>} prefix if required. @kbd{C-x 4 c}
482 (@code{clone-indirect-buffer-other-window}) works like @kbd{M-x
483 clone-indirect-buffer}, but it selects the cloned buffer in another
484 window. These commands come in handy if you want to create new
485 @samp{*info*} or @samp{*Help*} buffers, for example.
487 The more general way is with the command @kbd{M-x
488 make-indirect-buffer}. It creates an indirect buffer from buffer
489 @var{base-buffer}, under the name @var{indirect-name}. It prompts for
490 both @var{base-buffer} and @var{indirect-name} using the minibuffer.
492 @node Buffer Convenience
493 @section Convenience Features and Customization of Buffer Handling
495 This section describes several modes and features that make it more
496 convenient to switch between buffers.
499 * Uniquify:: Buffer names can contain directory parts.
500 * Iswitchb:: Switching between buffers with substrings.
501 * Buffer Menus:: Configurable buffer menu.
505 @subsection Making Buffer Names Unique
507 @cindex unique buffer names
508 @cindex directories in buffer names
509 When several buffers visit identically-named files, Emacs must give
510 the buffers distinct names. The usual method for making buffer names
511 unique adds @samp{<2>}, @samp{<3>}, etc. to the end of the buffer
512 names (all but one of them).
514 @vindex uniquify-buffer-name-style
515 Other methods work by adding parts of each file's directory to the
516 buffer name. To select one, customize the variable
517 @code{uniquify-buffer-name-style} (@pxref{Easy Customization}).
519 For instance, the @code{forward} naming method puts part of the
520 directory name at the beginning of the buffer name; using this method,
521 buffers visiting @file{/u/mernst/tmp/Makefile} and
522 @file{/usr/projects/zaphod/Makefile} would be named
523 @samp{tmp/Makefile} and @samp{zaphod/Makefile}, respectively (instead
524 of @samp{Makefile} and @samp{Makefile<2>}).
526 By contrast, the @code{post-forward} naming method would call the
527 buffers @samp{Makefile|tmp} and @samp{Makefile|zaphod}, and the
528 @code{reverse} naming method would call them @samp{Makefile\tmp} and
529 @samp{Makefile\zaphod}. The nontrivial difference between
530 @code{post-forward} and @code{reverse} occurs when just one directory
531 name is not enough to distinguish two files; then @code{reverse} puts
532 the directory names in reverse order, so that @file{/top/middle/file}
533 becomes @samp{file\middle\top}, while @code{post-forward} puts them in
534 forward order after the file name, as in @samp{file|top/middle}.
536 Which rule to follow for putting the directory names in the buffer
537 name is not very important if you are going to @emph{look} at the
538 buffer names before you type one. But as an experienced user, if you
539 know the rule, you won't have to look. And then you may find that one
540 rule or another is easier for you to remember and utilize fast.
543 @subsection Switching Between Buffers using Substrings
545 @findex iswitchb-mode
546 @cindex Iswitchb mode
547 @cindex mode, Iswitchb
548 @kindex C-x b @r{(Iswitchb mode)}
549 @kindex C-x 4 b @r{(Iswitchb mode)}
550 @kindex C-x 5 b @r{(Iswitchb mode)}
551 @kindex C-x 4 C-o @r{(Iswitchb mode)}
553 Iswitchb global minor mode provides convenient switching between
554 buffers using substrings of their names. It replaces the normal
555 definitions of @kbd{C-x b}, @kbd{C-x 4 b}, @kbd{C-x 5 b}, and @kbd{C-x
556 4 C-o} with alternative commands that are somewhat ``smarter.''
558 When one of these commands prompts you for a buffer name, you can
559 type in just a substring of the name you want to choose. As you enter
560 the substring, Iswitchb mode continuously displays a list of buffers
561 that match the substring you have typed.
563 At any time, you can type @key{RET} to select the first buffer in
564 the list. So the way to select a particular buffer is to make it the
565 first in the list. There are two ways to do this. You can type more
566 of the buffer name and thus narrow down the list, excluding unwanted
567 buffers above the desired one. Alternatively, you can use @kbd{C-s}
568 and @kbd{C-r} to rotate the list until the desired buffer is first.
570 @key{TAB} while entering the buffer name performs completion on the
571 string you have entered, based on the displayed list of buffers.
573 To enable Iswitchb mode, type @kbd{M-x iswitchb-mode}, or customize
574 the variable @code{iswitchb-mode} to @code{t} (@pxref{Easy
578 @subsection Customizing Buffer Menus
581 @cindex buffer list, customizable
584 Make a list of buffers similarly to @kbd{M-x list-buffers} but
588 @kbd{M-x bs-show} pops up a buffer list similar to the one normally
589 displayed by @kbd{C-x C-b} but which you can customize. If you prefer
590 this to the usual buffer list, you can bind this command to @kbd{C-x
591 C-b}. To customize this buffer list, use the @code{bs} Custom group
592 (@pxref{Easy Customization}).
598 @findex mouse-buffer-menu
599 @kindex C-Down-Mouse-1
600 MSB global minor mode (``MSB'' stands for ``mouse select buffer'')
601 provides a different and customizable mouse buffer menu which you may
602 prefer. It replaces the bindings of @code{mouse-buffer-menu},
603 normally on @kbd{C-Down-Mouse-1}, and the menu bar buffer menu. You
604 can customize the menu in the @code{msb} Custom group.