*** empty log message ***
[bpt/emacs.git] / man / buffers.texi
CommitLineData
6bf7aab6 1@c This is part of the Emacs manual.
d04efc64
DL
2@c Copyright (C) 1985, 86, 87, 93, 94, 95, 97, 2000
3@c Free Software Foundation, Inc.
6bf7aab6
DL
4@c See file emacs.texi for copying conditions.
5@node Buffers, Windows, Files, Top
6@chapter Using Multiple Buffers
7
8@cindex 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
11file's text. Each time you invoke Dired, a buffer is created to hold the
12directory 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
14command's documentation, that appears in a buffer called @samp{*Help*}.
15
16@cindex selected buffer
17@cindex current buffer
18 At any time, one and only one buffer is @dfn{selected}. It is also
19called the @dfn{current buffer}. Often we say that a command operates on
20``the buffer'' as if there were only one; but really this means that the
21command operates on the selected buffer (most commands do).
22
23 When Emacs has multiple windows, each window has a chosen buffer which
24is displayed there, but at any time only one of the windows is selected and
25its chosen buffer is the selected buffer. Each window's mode line displays
26the name of the buffer that the window is displaying (@pxref{Windows}).
27
28 Each buffer has a name, which can be of any length, and you can select
29any buffer by giving its name. Most buffers are made by visiting files,
30and their names are derived from the files' names. But you can also create
31an empty buffer with any name you want. A newly started Emacs has a buffer
32named @samp{*scratch*} which can be used for evaluating Lisp expressions in
33Emacs. The distinction between upper and lower case matters in buffer
34names.
35
36 Each buffer records individually what file it is visiting, whether it is
37modified, and what major mode and minor modes are in effect in it
38(@pxref{Major Modes}). Any Emacs variable can be made @dfn{local to} a
39particular buffer, meaning its value in that buffer can be different from
40the value in other buffers. @xref{Locals}.
41
42@menu
43* Select Buffer:: Creating a new buffer or reselecting an old one.
44* List Buffers:: Getting a list of buffers that exist.
45* Misc Buffer:: Renaming; changing read-onlyness; copying text.
46* Kill Buffer:: Killing buffers you no longer need.
47* Several Buffers:: How to go through the list of all buffers
48 and operate variously on several of them.
49* Indirect Buffers:: An indirect buffer shares the text of another buffer.
b54346bc
DL
50* Buffer Convenience:: Convenience and customization features for
51 buffer handling.
6bf7aab6
DL
52@end menu
53
54@node Select Buffer
55@section Creating and Selecting Buffers
56@cindex change buffers
57@cindex switch buffers
58
59@table @kbd
60@item C-x b @var{buffer} @key{RET}
61Select or create a buffer named @var{buffer} (@code{switch-to-buffer}).
62@item C-x 4 b @var{buffer} @key{RET}
63Similar, but select @var{buffer} in another window
64(@code{switch-to-buffer-other-window}).
65@item C-x 5 b @var{buffer} @key{RET}
66Similar, but select @var{buffer} in a separate frame
67(@code{switch-to-buffer-other-frame}).
68@end table
69
70@kindex C-x 4 b
71@findex switch-to-buffer-other-window
72@kindex C-x 5 b
73@findex switch-to-buffer-other-frame
74@kindex C-x b
75@findex switch-to-buffer
76 To select the buffer named @var{bufname}, type @kbd{C-x b @var{bufname}
77@key{RET}}. This runs the command @code{switch-to-buffer} with argument
78@var{bufname}. You can use completion on an abbreviation for the buffer
79name you want (@pxref{Completion}). An empty argument to @kbd{C-x b}
80specifies the most recently selected buffer that is not displayed in any
81window.@refill
82
83 Most buffers are created by visiting files, or by Emacs commands that
84want to display some text, but you can also create a buffer explicitly
85by typing @kbd{C-x b @var{bufname} @key{RET}}. This makes a new, empty
86buffer that is not visiting any file, and selects it for editing. Such
87buffers are used for making notes to yourself. If you try to save one,
88you are asked for the file name to use. The new buffer's major mode is
89determined by the value of @code{default-major-mode} (@pxref{Major
90Modes}).
91
92 Note that @kbd{C-x C-f}, and any other command for visiting a file,
93can also be used to switch to an existing file-visiting buffer.
94@xref{Visiting}.
95
96 Emacs uses buffer names that start with a space for internal purposes.
97It treats these buffers specially in minor ways---for example, by
98default they do not record undo information. It is best to avoid using
99such buffer names yourself.
100
101@node List Buffers
102@section Listing Existing Buffers
103
104@table @kbd
105@item C-x C-b
106List the existing buffers (@code{list-buffers}).
107@end table
108
109@cindex listing current buffers
110@kindex C-x C-b
111@findex list-buffers
112 To display a list of all the buffers that exist, type @kbd{C-x C-b}.
113Each line in the list shows one buffer's name, major mode and visited
114file. The buffers are listed in the order that they were current; the
115buffers that were current most recently come first.
116
117 @samp{*} at the beginning of a line indicates the buffer is ``modified.''
118If several buffers are modified, it may be time to save some with @kbd{C-x s}
119(@pxref{Saving}). @samp{%} indicates a read-only buffer. @samp{.} marks the
120selected buffer. Here is an example of a buffer list:@refill
121
122@smallexample
123 MR Buffer Size Mode File
124 -- ------ ---- ---- ----
125.* emacs.tex 383402 Texinfo /u2/emacs/man/emacs.tex
126 *Help* 1287 Fundamental
127 files.el 23076 Emacs-Lisp /u2/emacs/lisp/files.el
128 % RMAIL 64042 RMAIL /u/rms/RMAIL
129 *% man 747 Dired /u2/emacs/man/
130 net.emacs 343885 Fundamental /u/rms/net.emacs
131 fileio.c 27691 C /u2/emacs/src/fileio.c
132 NEWS 67340 Text /u2/emacs/etc/NEWS
133 *scratch* 0 Lisp Interaction
134@end smallexample
135
136@noindent
137Note that the buffer @samp{*Help*} was made by a help request; it is not
138visiting any file. The buffer @code{man} was made by Dired on the
e469e878
GM
139directory @file{/u2/emacs/man/}. You can list buffers visiting files
140only by giving the command a prefix, i.e. type @kbd{C-u C-x C-b}.
6bf7aab6
DL
141
142@need 2000
143@node Misc Buffer
144@section Miscellaneous Buffer Operations
145
146@table @kbd
147@item C-x C-q
148Toggle read-only status of buffer (@code{vc-toggle-read-only}).
149@item M-x rename-buffer @key{RET} @var{name} @key{RET}
150Change the name of the current buffer.
151@item M-x rename-uniquely
152Rename the current buffer by adding @samp{<@var{number}>} to the end.
153@item M-x view-buffer @key{RET} @var{buffer} @key{RET}
154Scroll through buffer @var{buffer}.
155@end table
156
157@kindex C-x C-q
158@findex vc-toggle-read-only
159@vindex buffer-read-only
160@cindex read-only buffer
161 A buffer can be @dfn{read-only}, which means that commands to change
162its contents are not allowed. The mode line indicates read-only buffers
163with @samp{%%} or @samp{%*} near the left margin. Read-only buffers are
164usually made by subsystems such as Dired and Rmail that have special
165commands to operate on the text; also by visiting a file whose access
a9fff306
EZ
166control says you cannot write it. However, if the variable
167@code{kill-read-only-ok} is set to a non-@code{nil} value, you can kill
168(a.k.a.@: cut) read-only text, see @ref{Killing}.
6bf7aab6
DL
169
170 If you wish to make changes in a read-only buffer, use the command
171@kbd{C-x C-q} (@code{vc-toggle-read-only}). It makes a read-only buffer
172writable, and makes a writable buffer read-only. In most cases, this
173works by setting the variable @code{buffer-read-only}, which has a local
174value in each buffer and makes the buffer read-only if its value is
175non-@code{nil}. If the file is maintained with version control,
176@kbd{C-x C-q} works through the version control system to change the
177read-only status of the file as well as the buffer. @xref{Version
178Control}.
179
180@findex rename-buffer
181 @kbd{M-x rename-buffer} changes the name of the current buffer. Specify
182the new name as a minibuffer argument. There is no default. If you
183specify a name that is in use for some other buffer, an error happens and
184no renaming is done.
185
186 @kbd{M-x rename-uniquely} renames the current buffer to a similar name
187with a numeric suffix added to make it both different and unique. This
188command does not need an argument. It is useful for creating multiple
189shell buffers: if you rename the @samp{*Shell*} buffer, then do @kbd{M-x
190shell} again, it makes a new shell buffer named @samp{*Shell*};
191meanwhile, the old shell buffer continues to exist under its new name.
192This method is also good for mail buffers, compilation buffers, and most
193Emacs features that create special buffers with particular names.
194
195@findex view-buffer
196 @kbd{M-x view-buffer} is much like @kbd{M-x view-file} (@pxref{Misc
197File Ops}) except that it examines an already existing Emacs buffer.
198View mode provides commands for scrolling through the buffer
199conveniently but not for changing it. When you exit View mode with
200@kbd{q}, that switches back to the buffer (and the position) which was
201previously displayed in the window. Alternatively, if you exit View
202mode with @kbd{e}, the buffer and the value of point that resulted from
203your perusal remain in effect.
204
205 The commands @kbd{M-x append-to-buffer} and @kbd{M-x insert-buffer}
206can be used to copy text from one buffer to another. @xref{Accumulating
207Text}.@refill
208
209@node Kill Buffer
210@section Killing Buffers
211
212@cindex killing buffers
213 If you continue an Emacs session for a while, you may accumulate a
214large number of buffers. You may then find it convenient to @dfn{kill}
215the buffers you no longer need. On most operating systems, killing a
216buffer releases its space back to the operating system so that other
217programs can use it. Here are some commands for killing buffers:
218
219@c WideCommands
220@table @kbd
221@item C-x k @var{bufname} @key{RET}
222Kill buffer @var{bufname} (@code{kill-buffer}).
223@item M-x kill-some-buffers
224Offer to kill each buffer, one by one.
225@end table
226
227@findex kill-buffer
228@findex kill-some-buffers
229@kindex C-x k
230
231 @kbd{C-x k} (@code{kill-buffer}) kills one buffer, whose name you
232specify in the minibuffer. The default, used if you type just @key{RET}
233in the minibuffer, is to kill the current buffer. If you kill the
234current buffer, another buffer is selected; one that has been selected
235recently but does not appear in any window now. If you ask to kill a
236file-visiting buffer that is modified (has unsaved editing), then you
237must confirm with @kbd{yes} before the buffer is killed.
238
239 The command @kbd{M-x kill-some-buffers} asks about each buffer, one by
240one. An answer of @kbd{y} means to kill the buffer. Killing the current
241buffer or a buffer containing unsaved changes selects a new buffer or asks
242for confirmation just like @code{kill-buffer}.
243
244 The buffer menu feature (@pxref{Several Buffers}) is also convenient
245for killing various buffers.
246
247@vindex kill-buffer-hook
248 If you want to do something special every time a buffer is killed, you
249can add hook functions to the hook @code{kill-buffer-hook} (@pxref{Hooks}).
250
251@findex clean-buffer-list
252 If you run one Emacs session for a period of days, as many people do,
253it can fill up with buffers that you used several days ago. The command
254@kbd{M-x clean-buffer-list} is a convenient way to purge them; it kills
255all the unmodified buffers that you have not used for a long time. An
256ordinary buffer is killed if it has not been displayed for three days;
257however, you can specify certain buffers that should never be killed
258automatically, and others that should be killed if they have been unused
259for a mere hour.
260
261@cindex Midnight mode
262@vindex midnight-mode
263@vindex midnight-hook
264 You can also have this buffer purging done for you, every day at
265midnight, by enabling Midnight mode. Midnight mode operates each day at
266midnight; at that time, it runs @code{clean-buffer-list}, or whichever
267functions you have placed in the normal hook @code{midnight-hook}
268(@pxref{Hooks}).
269
270 To enable Midnight mode, use the Customization buffer to set the
271variable @code{midnight-mode} to @code{t}. @xref{Easy Customization}.
272
273@node Several Buffers
274@section Operating on Several Buffers
275@cindex buffer menu
276
277 The @dfn{buffer-menu} facility is like a ``Dired for buffers''; it allows
278you to request operations on various Emacs buffers by editing an Emacs
279buffer containing a list of them. You can save buffers, kill them
280(here called @dfn{deleting} them, for consistency with Dired), or display
281them.
282
283@table @kbd
284@item M-x buffer-menu
285Begin editing a buffer listing all Emacs buffers.
286@end table
287
288@findex buffer-menu
289 The command @code{buffer-menu} writes a list of all Emacs buffers into
290the buffer @samp{*Buffer List*}, and selects that buffer in Buffer Menu
291mode. The buffer is read-only, and can be changed only through the
292special commands described in this section. The usual Emacs cursor
293motion commands can be used in the @samp{*Buffer List*} buffer. The
294following commands apply to the buffer described on the current line.
295
296@table @kbd
297@item d
298Request to delete (kill) the buffer, then move down. The request
299shows as a @samp{D} on the line, before the buffer name. Requested
300deletions take place when you type the @kbd{x} command.
301@item C-d
302Like @kbd{d} but move up afterwards instead of down.
303@item s
304Request to save the buffer. The request shows as an @samp{S} on the
305line. Requested saves take place when you type the @kbd{x} command.
306You may request both saving and deletion for the same buffer.
307@item x
308Perform previously requested deletions and saves.
309@item u
310Remove any request made for the current line, and move down.
311@item @key{DEL}
312Move to previous line and remove any request made for that line.
313@end table
314
315 The @kbd{d}, @kbd{C-d}, @kbd{s} and @kbd{u} commands to add or remove
316flags also move down (or up) one line. They accept a numeric argument
317as a repeat count.
318
319 These commands operate immediately on the buffer listed on the current
320line:
321
322@table @kbd
323@item ~
324Mark the buffer ``unmodified.'' The command @kbd{~} does this
325immediately when you type it.
326@item %
327Toggle the buffer's read-only flag. The command @kbd{%} does
328this immediately when you type it.
329@item t
330Visit the buffer as a tags table. @xref{Select Tags Table}.
331@end table
332
333 There are also commands to select another buffer or buffers:
334
335@table @kbd
336@item q
337Quit the buffer menu---immediately display the most recent formerly
338visible buffer in its place.
339@item @key{RET}
340@itemx f
341Immediately select this line's buffer in place of the @samp{*Buffer
342List*} buffer.
343@item o
344Immediately select this line's buffer in another window as if by
345@kbd{C-x 4 b}, leaving @samp{*Buffer List*} visible.
346@item C-o
347Immediately display this line's buffer in another window, but don't
348select the window.
349@item 1
350Immediately select this line's buffer in a full-screen window.
351@item 2
352Immediately set up two windows, with this line's buffer in one, and the
353previously selected buffer (aside from the buffer @samp{*Buffer List*})
354in the other.
355@item b
356Bury the buffer listed on this line.
357@item m
358Mark this line's buffer to be displayed in another window if you exit
359with the @kbd{v} command. The request shows as a @samp{>} at the
360beginning of the line. (A single buffer may not have both a delete
361request and a display request.)
362@item v
363Immediately select this line's buffer, and also display in other windows
364any buffers previously marked with the @kbd{m} command. If you have not
365marked any buffers, this command is equivalent to @kbd{1}.
366@end table
367
368 All that @code{buffer-menu} does directly is create and switch to a
369suitable buffer, and turn on Buffer Menu mode. Everything else
370described above is implemented by the special commands provided in
371Buffer Menu mode. One consequence of this is that you can switch from
372the @samp{*Buffer List*} buffer to another Emacs buffer, and edit there.
373You can reselect the @samp{*Buffer List*} buffer later, to perform the
374operations already requested, or you can kill it, or pay no further
375attention to it.
376
377 The only difference between @code{buffer-menu} and @code{list-buffers}
378is that @code{buffer-menu} switches to the @samp{*Buffer List*} buffer
379in the selected window; @code{list-buffers} displays it in another
380window. If you run @code{list-buffers} (that is, type @kbd{C-x C-b})
381and select the buffer list manually, you can use all of the commands
382described here.
383
384 The buffer @samp{*Buffer List*} is not updated automatically when
385buffers are created and killed; its contents are just text. If you have
386created, deleted or renamed buffers, the way to update @samp{*Buffer
387List*} to show what you have done is to type @kbd{g}
388(@code{revert-buffer}) or repeat the @code{buffer-menu} command.
389
390@node Indirect Buffers
391@section Indirect Buffers
392@cindex indirect buffer
393@cindex base buffer
394
395 An @dfn{indirect buffer} shares the text of some other buffer, which
396is called the @dfn{base buffer} of the indirect buffer. In some ways it
397is the analogue, for buffers, of a symbolic link between files.
398
399@table @kbd
400@findex make-indirect-buffer
401@item M-x make-indirect-buffer @var{base-buffer} @key{RET} @var{indirect-name} @key{RET}
402Create an indirect buffer named @var{indirect-name} whose base buffer
403is @var{base-buffer}.
f16874ce
EZ
404@findex clone-indirect-buffer
405@item M-x clone-indirect-buffer @key{RET}
406Create an indirect buffer that is a twin copy of the current buffer.
407@kindex C-x 4 c
408@findex clone-indirect-buffer-other-window
409Create an indirect buffer that is a twin copy of the current buffer, and
410select it in another window (@code{clone-indirect-buffer-other-window}).
6bf7aab6
DL
411@end table
412
413 The text of the indirect buffer is always identical to the text of its
414base buffer; changes made by editing either one are visible immediately
415in the other. But in all other respects, the indirect buffer and its
416base buffer are completely separate. They have different names,
417different values of point, different narrowing, different markers,
418different major modes, and different local variables.
419
420 An indirect buffer cannot visit a file, but its base buffer can. If
421you try to save the indirect buffer, that actually works by saving the
422base buffer. Killing the base buffer effectively kills the indirect
423buffer, but killing an indirect buffer has no effect on its base buffer.
424
425 One way to use indirect buffers is to display multiple views of an
426outline. @xref{Outline Views}.
b54346bc 427
f16874ce
EZ
428 The command @kbd{M-x make-indirect-buffer} creates an indirect buffer
429whose name is @var{indirect-name} and whose text is identical to that of
430the buffer @var{base-buffer}. It prompts for both @var{base-buffer} and
431@var{indirect-name}.
432
63ef5047 433@cindex multiple @samp{*info*} and @samp{*Help*} buffers
f16874ce
EZ
434 The command @kbd{M-x clone-indirect-buffer} creates an indirect buffer
435whose base buffer is the current buffer, and also selects the
436newly-created indirect buffer. With a numeric argument, it prompts for
437the name of the indirect buffer; otherwise it defaults to the name of
438the current buffer, modifying it by adding a @samp{<@var{n}>} prefix if
439required. @kbd{C-x 4 c} (@code{clone-indirect-buffer-other-window})
440works like @kbd{M-x clone-indirect-buffer}, but it selects the cloned
63ef5047
EZ
441buffer in another window. These commands come in handy if you want to
442create new @samp{*info*} or @samp{*Help*} buffers, for example.
f16874ce 443
b54346bc
DL
444@node Buffer Convenience
445@section Convenience Features and Customization of Buffer Handling
446
447@menu
d04efc64
DL
448* Uniquify:: Buffer names can contain directory parts.
449* BS:: Configurable buffer menu.
450@ignore
451* Iswitchb:: Switching between buffers with substrings.
452* MSB::
453@end ignore
b54346bc
DL
454@end menu
455
456@node Uniquify
457@subsection Directory Names in Buffer Names
458
459@findex toggle-uniquify-buffer-names
460@vindex uniquify-buffer-name-style
461@cindex unique buffer names
462@cindex directories in buffer names
463Emacs's standard method for making buffer names unique adds @samp{<2>},
464@samp{<3>}, etc. to the end of (all but one of) the buffers. The
465Uniquify package replaces that behavior, for buffers visiting files and
466dired buffers. It implements a uniquification that adds parts of the
467file name until the buffer names are unique. For instance, buffers
468visiting @file{/u/mernst/tmp/Makefile} and
469@file{/usr/projects/zaphod/Makefile} would be named @samp{tmp/Makefile}
470and @samp{zaphod/Makefile}, respectively (instead of @samp{Makefile}
471and @samp{Makefile<2>}). You can turn on this mode and select other
472buffer name styles by customizing the user option
473@code{uniquify-buffer-name-style}. The command @kbd{M-x
474toggle-uniquify-buffer-names} can also be used to toggle the mode.
475
476@node BS
d04efc64 477@subsection Configurable Buffer Menus
b54346bc 478
d04efc64
DL
479@findex bs-show
480@findex bs-cycle-next
481@findex bs-cycle-previous
482@cindex buffer list, customizable
483@table @kbd
484@item M-x bs-show
485Make a list of buffers similarly to @kbd{M-x list-buffers} but
486customizable.
487@item M-x bs-cycle-next
488Cycle to the next buffer in the configuration.
489@item M-x bs-cycle-previous
490Cycle to the previous buffer in the configuration.
491@end table
492
493@kbd{M-x bs-show} pops up a buffer list similar to the one normally
494displayed by @kbd{C-x C-b} but which can be customized. You might like
495to bind it to @kbd{C-x C-b}. You can customize the display, for
496instance to display a subset of buffers, in the @code{bs} Custom group.
497A special subset of all buffers is available---for instance avoiding
498ones like @samp{*Messages*}---through which you can cycle with @kbd{M-x
499bs-cycle-next} and @kbd{M-x bs-cycle-previous}. Those commands could be
500bound to convenient keys.
501
502@ignore
b54346bc 503@node Iswitchb
d04efc64 504@subsection Switching Between Buffers using Substrings
b54346bc
DL
505
506@node MSB
d04efc64
DL
507@subsection Customizable Buffer Selection with Multiple Menus
508@end ignore