Minor change.
[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
564ee37b
RS
137Note that the buffer @samp{*Help*} was made by a help request; it is
138not visiting any file. The buffer @code{man} was made by Dired on the
139directory @file{/u2/emacs/man/}. You can list only buffers that are
140visiting files by giving the command a prefix; for instance, by typing
141@kbd{C-u C-x C-b}.
6bf7aab6
DL
142
143@need 2000
144@node Misc Buffer
145@section Miscellaneous Buffer Operations
146
147@table @kbd
148@item C-x C-q
149Toggle read-only status of buffer (@code{vc-toggle-read-only}).
150@item M-x rename-buffer @key{RET} @var{name} @key{RET}
151Change the name of the current buffer.
152@item M-x rename-uniquely
153Rename the current buffer by adding @samp{<@var{number}>} to the end.
154@item M-x view-buffer @key{RET} @var{buffer} @key{RET}
155Scroll through buffer @var{buffer}.
156@end table
157
158@kindex C-x C-q
4946337d
EZ
159@c Don't index vc-toggle-read-only here, it is indexed in files.texi,
160@c in the node "Basic VC Editing".
161@c @findex vc-toggle-read-only
6bf7aab6
DL
162@vindex buffer-read-only
163@cindex read-only buffer
164 A buffer can be @dfn{read-only}, which means that commands to change
564ee37b
RS
165its contents are not allowed. The mode line indicates read-only
166buffers with @samp{%%} or @samp{%*} near the left margin. Read-only
167buffers are usually made by subsystems such as Dired and Rmail that
168have special commands to operate on the text; also by visiting a file
169whose access control says you cannot write it.
6bf7aab6
DL
170
171 If you wish to make changes in a read-only buffer, use the command
172@kbd{C-x C-q} (@code{vc-toggle-read-only}). It makes a read-only buffer
173writable, and makes a writable buffer read-only. In most cases, this
174works by setting the variable @code{buffer-read-only}, which has a local
175value in each buffer and makes the buffer read-only if its value is
176non-@code{nil}. If the file is maintained with version control,
177@kbd{C-x C-q} works through the version control system to change the
178read-only status of the file as well as the buffer. @xref{Version
179Control}.
180
181@findex rename-buffer
182 @kbd{M-x rename-buffer} changes the name of the current buffer. Specify
183the new name as a minibuffer argument. There is no default. If you
184specify a name that is in use for some other buffer, an error happens and
185no renaming is done.
186
187 @kbd{M-x rename-uniquely} renames the current buffer to a similar name
188with a numeric suffix added to make it both different and unique. This
189command does not need an argument. It is useful for creating multiple
190shell buffers: if you rename the @samp{*Shell*} buffer, then do @kbd{M-x
191shell} again, it makes a new shell buffer named @samp{*Shell*};
192meanwhile, the old shell buffer continues to exist under its new name.
193This method is also good for mail buffers, compilation buffers, and most
194Emacs features that create special buffers with particular names.
195
196@findex view-buffer
197 @kbd{M-x view-buffer} is much like @kbd{M-x view-file} (@pxref{Misc
198File Ops}) except that it examines an already existing Emacs buffer.
199View mode provides commands for scrolling through the buffer
200conveniently but not for changing it. When you exit View mode with
201@kbd{q}, that switches back to the buffer (and the position) which was
202previously displayed in the window. Alternatively, if you exit View
203mode with @kbd{e}, the buffer and the value of point that resulted from
204your perusal remain in effect.
205
206 The commands @kbd{M-x append-to-buffer} and @kbd{M-x insert-buffer}
207can be used to copy text from one buffer to another. @xref{Accumulating
208Text}.@refill
209
210@node Kill Buffer
211@section Killing Buffers
212
213@cindex killing buffers
214 If you continue an Emacs session for a while, you may accumulate a
215large number of buffers. You may then find it convenient to @dfn{kill}
216the buffers you no longer need. On most operating systems, killing a
217buffer releases its space back to the operating system so that other
218programs can use it. Here are some commands for killing buffers:
219
220@c WideCommands
221@table @kbd
222@item C-x k @var{bufname} @key{RET}
223Kill buffer @var{bufname} (@code{kill-buffer}).
224@item M-x kill-some-buffers
225Offer to kill each buffer, one by one.
226@end table
227
228@findex kill-buffer
229@findex kill-some-buffers
230@kindex C-x k
231
232 @kbd{C-x k} (@code{kill-buffer}) kills one buffer, whose name you
233specify in the minibuffer. The default, used if you type just @key{RET}
234in the minibuffer, is to kill the current buffer. If you kill the
235current buffer, another buffer is selected; one that has been selected
236recently but does not appear in any window now. If you ask to kill a
237file-visiting buffer that is modified (has unsaved editing), then you
238must confirm with @kbd{yes} before the buffer is killed.
239
240 The command @kbd{M-x kill-some-buffers} asks about each buffer, one by
241one. An answer of @kbd{y} means to kill the buffer. Killing the current
242buffer or a buffer containing unsaved changes selects a new buffer or asks
243for confirmation just like @code{kill-buffer}.
244
245 The buffer menu feature (@pxref{Several Buffers}) is also convenient
246for killing various buffers.
247
248@vindex kill-buffer-hook
249 If you want to do something special every time a buffer is killed, you
250can add hook functions to the hook @code{kill-buffer-hook} (@pxref{Hooks}).
251
252@findex clean-buffer-list
253 If you run one Emacs session for a period of days, as many people do,
254it can fill up with buffers that you used several days ago. The command
255@kbd{M-x clean-buffer-list} is a convenient way to purge them; it kills
256all the unmodified buffers that you have not used for a long time. An
257ordinary buffer is killed if it has not been displayed for three days;
258however, you can specify certain buffers that should never be killed
259automatically, and others that should be killed if they have been unused
260for a mere hour.
261
262@cindex Midnight mode
263@vindex midnight-mode
264@vindex midnight-hook
265 You can also have this buffer purging done for you, every day at
266midnight, by enabling Midnight mode. Midnight mode operates each day at
267midnight; at that time, it runs @code{clean-buffer-list}, or whichever
268functions you have placed in the normal hook @code{midnight-hook}
269(@pxref{Hooks}).
270
271 To enable Midnight mode, use the Customization buffer to set the
272variable @code{midnight-mode} to @code{t}. @xref{Easy Customization}.
273
274@node Several Buffers
275@section Operating on Several Buffers
276@cindex buffer menu
277
278 The @dfn{buffer-menu} facility is like a ``Dired for buffers''; it allows
279you to request operations on various Emacs buffers by editing an Emacs
280buffer containing a list of them. You can save buffers, kill them
281(here called @dfn{deleting} them, for consistency with Dired), or display
282them.
283
284@table @kbd
285@item M-x buffer-menu
286Begin editing a buffer listing all Emacs buffers.
287@end table
288
289@findex buffer-menu
290 The command @code{buffer-menu} writes a list of all Emacs buffers into
291the buffer @samp{*Buffer List*}, and selects that buffer in Buffer Menu
292mode. The buffer is read-only, and can be changed only through the
293special commands described in this section. The usual Emacs cursor
294motion commands can be used in the @samp{*Buffer List*} buffer. The
295following commands apply to the buffer described on the current line.
296
297@table @kbd
298@item d
299Request to delete (kill) the buffer, then move down. The request
300shows as a @samp{D} on the line, before the buffer name. Requested
301deletions take place when you type the @kbd{x} command.
302@item C-d
303Like @kbd{d} but move up afterwards instead of down.
304@item s
305Request to save the buffer. The request shows as an @samp{S} on the
306line. Requested saves take place when you type the @kbd{x} command.
307You may request both saving and deletion for the same buffer.
308@item x
309Perform previously requested deletions and saves.
310@item u
311Remove any request made for the current line, and move down.
312@item @key{DEL}
313Move to previous line and remove any request made for that line.
314@end table
315
316 The @kbd{d}, @kbd{C-d}, @kbd{s} and @kbd{u} commands to add or remove
317flags also move down (or up) one line. They accept a numeric argument
318as a repeat count.
319
320 These commands operate immediately on the buffer listed on the current
321line:
322
323@table @kbd
324@item ~
325Mark the buffer ``unmodified.'' The command @kbd{~} does this
326immediately when you type it.
327@item %
328Toggle the buffer's read-only flag. The command @kbd{%} does
329this immediately when you type it.
330@item t
331Visit the buffer as a tags table. @xref{Select Tags Table}.
332@end table
333
334 There are also commands to select another buffer or buffers:
335
336@table @kbd
337@item q
338Quit the buffer menu---immediately display the most recent formerly
339visible buffer in its place.
340@item @key{RET}
341@itemx f
342Immediately select this line's buffer in place of the @samp{*Buffer
343List*} buffer.
344@item o
345Immediately select this line's buffer in another window as if by
346@kbd{C-x 4 b}, leaving @samp{*Buffer List*} visible.
347@item C-o
348Immediately display this line's buffer in another window, but don't
349select the window.
350@item 1
351Immediately select this line's buffer in a full-screen window.
352@item 2
353Immediately set up two windows, with this line's buffer in one, and the
354previously selected buffer (aside from the buffer @samp{*Buffer List*})
355in the other.
356@item b
357Bury the buffer listed on this line.
358@item m
359Mark this line's buffer to be displayed in another window if you exit
360with the @kbd{v} command. The request shows as a @samp{>} at the
361beginning of the line. (A single buffer may not have both a delete
362request and a display request.)
363@item v
364Immediately select this line's buffer, and also display in other windows
365any buffers previously marked with the @kbd{m} command. If you have not
366marked any buffers, this command is equivalent to @kbd{1}.
367@end table
368
369 All that @code{buffer-menu} does directly is create and switch to a
370suitable buffer, and turn on Buffer Menu mode. Everything else
371described above is implemented by the special commands provided in
372Buffer Menu mode. One consequence of this is that you can switch from
373the @samp{*Buffer List*} buffer to another Emacs buffer, and edit there.
374You can reselect the @samp{*Buffer List*} buffer later, to perform the
375operations already requested, or you can kill it, or pay no further
376attention to it.
377
378 The only difference between @code{buffer-menu} and @code{list-buffers}
379is that @code{buffer-menu} switches to the @samp{*Buffer List*} buffer
380in the selected window; @code{list-buffers} displays it in another
381window. If you run @code{list-buffers} (that is, type @kbd{C-x C-b})
382and select the buffer list manually, you can use all of the commands
383described here.
384
385 The buffer @samp{*Buffer List*} is not updated automatically when
386buffers are created and killed; its contents are just text. If you have
387created, deleted or renamed buffers, the way to update @samp{*Buffer
388List*} to show what you have done is to type @kbd{g}
389(@code{revert-buffer}) or repeat the @code{buffer-menu} command.
390
391@node Indirect Buffers
392@section Indirect Buffers
393@cindex indirect buffer
394@cindex base buffer
395
396 An @dfn{indirect buffer} shares the text of some other buffer, which
397is called the @dfn{base buffer} of the indirect buffer. In some ways it
398is the analogue, for buffers, of a symbolic link between files.
399
400@table @kbd
401@findex make-indirect-buffer
52ec6cdc 402@item M-x make-indirect-buffer @key{RET} @var{base-buffer} @key{RET} @var{indirect-name} @key{RET}
6bf7aab6
DL
403Create an indirect buffer named @var{indirect-name} whose base buffer
404is @var{base-buffer}.
f16874ce
EZ
405@findex clone-indirect-buffer
406@item M-x clone-indirect-buffer @key{RET}
407Create an indirect buffer that is a twin copy of the current buffer.
112c140f 408@item C-x 4 c
f16874ce
EZ
409@kindex C-x 4 c
410@findex clone-indirect-buffer-other-window
411Create an indirect buffer that is a twin copy of the current buffer, and
412select it in another window (@code{clone-indirect-buffer-other-window}).
6bf7aab6
DL
413@end table
414
415 The text of the indirect buffer is always identical to the text of its
416base buffer; changes made by editing either one are visible immediately
417in the other. But in all other respects, the indirect buffer and its
418base buffer are completely separate. They have different names,
419different values of point, different narrowing, different markers,
420different major modes, and different local variables.
421
422 An indirect buffer cannot visit a file, but its base buffer can. If
423you try to save the indirect buffer, that actually works by saving the
424base buffer. Killing the base buffer effectively kills the indirect
425buffer, but killing an indirect buffer has no effect on its base buffer.
426
427 One way to use indirect buffers is to display multiple views of an
428outline. @xref{Outline Views}.
b54346bc 429
63ef5047 430@cindex multiple @samp{*info*} and @samp{*Help*} buffers
564ee37b
RS
431 A quick and handy way to make an indirect buffer is with the command
432@kbd{M-x clone-indirect-buffer}. It creates and selects an indirect
433buffer whose base buffer is the current buffer. With a numeric
434argument, it prompts for the name of the indirect buffer; otherwise it
435defaults to the name of the current buffer, modifying it by adding a
436@samp{<@var{n}>} prefix if required. @kbd{C-x 4 c}
437(@code{clone-indirect-buffer-other-window}) works like @kbd{M-x
438clone-indirect-buffer}, but it selects the cloned buffer in another
439window. These commands come in handy if you want to create new
440@samp{*info*} or @samp{*Help*} buffers, for example.
441
442 The more general way is with the command @kbd{M-x
443make-indirect-buffer}. It creates an indirect buffer from buffer
444@var{base-buffer}, under the name @var{indirect-name}. It prompts for
445both @var{base-buffer} and @var{indirect-name} using the minibuffer.
f16874ce 446
b54346bc
DL
447@node Buffer Convenience
448@section Convenience Features and Customization of Buffer Handling
449
450@menu
d04efc64 451* Uniquify:: Buffer names can contain directory parts.
d04efc64 452* Iswitchb:: Switching between buffers with substrings.
564ee37b 453* Buffer Menus:: Configurable buffer menu.
b54346bc
DL
454@end menu
455
456@node Uniquify
564ee37b 457@subsection Making Buffer Names Unique
b54346bc 458
b54346bc
DL
459@cindex unique buffer names
460@cindex directories in buffer names
564ee37b
RS
461 When several buffers visit identically-named files, Emacs must give
462the buffers distinct names. The usual method for making buffer names
463unique adds @samp{<2>}, @samp{<3>}, etc. to the end of the buffer
464names (all but one of them).
465
466@vindex uniquify-buffer-name-style
467 Other methods work by adding parts of each file's directory to the
468buffer name. To select one, customize the variable
469@code{uniquify-buffer-name-style} (@pxref{Easy Customization}).
470
471 For instance, the @code{forward} naming method puts part of the
472directory name at the beginning of the buffer name; using this method,
473buffers visiting @file{/u/mernst/tmp/Makefile} and
474@file{/usr/projects/zaphod/Makefile} would be named
475@samp{tmp/Makefile} and @samp{zaphod/Makefile}, respectively (instead
476of @samp{Makefile} and @samp{Makefile<2>}).
477
478 By contrast, the @code{post-forward} naming method would call the
479buffers @samp{Makefile|tmp} and @samp{Makefile|zaphod}, and the
480@code{reverse} naming method would call them @samp{Makefile\tmp} and
481@samp{Makefile\zaphod}. The nontrivial difference between
482@code{post-forward} and @code{reverse} occurs when just one directory
483name is not enough to distinguish two files; then @code{reverse} puts
484the directory names in reverse order, so that @file{/top/middle/file}
485becomes @samp{file\middle\top}, while @code{post-forward} puts them in
486forward order after the file name, as in @samp{file|top/middle}.
487
488 Which rule to follow for putting the directory names in the buffer
489name is not very important if you are going to @emph{look} at the
490buffer names before you type one. But as an experienced user, if you
491know the rule, you won't have to look. And then you may find that one
492rule or another is easier for you to remember and utilize fast.
b54346bc 493
1ea14188
DL
494@node Iswitchb
495@subsection Switching Between Buffers using Substrings
496
497@findex iswitchb-mode
498@cindex Iswitchb mode
499@cindex mode, Iswitchb
500@kindex C-x b @r{(Iswitchb mode)}
501@kindex C-x 4 b @r{(Iswitchb mode)}
502@kindex C-x 5 b @r{(Iswitchb mode)}
503@kindex C-x 4 C-o @r{(Iswitchb mode)}
504
564ee37b
RS
505 Iswitchb global minor mode provides convenient switching between
506buffers using substrings of their names. It replaces the normal
507definitions of @kbd{C-x b}, @kbd{C-x 4 b}, @kbd{C-x 5 b}, and @kbd{C-x
5084 C-o} with alternative commands that are somewhat ``smarter.''
1ea14188 509
564ee37b
RS
510 When one of these commands prompts you for a buffer name, you can
511type in just a substring of the name you want to choose. As you enter
512the substring, Iswitchb mode continuously displays a list of buffers
513that match the substring you have typed.
1ea14188 514
564ee37b
RS
515 At any time, you can type @key{RET} to select the first buffer in
516the list. So the way to select a particular buffer is to make it the
517first in the list. There are two ways to do this. You can type more
518of the buffer name and thus narrow down the list, excluding unwanted
519buffers above the desired one. Alternatively, you can use @kbd{C-s}
520and @kbd{C-r} to rotate the list until the desired buffer is first.
521
522 @key{TAB} while entering the buffer name performs completion on the
523string you have entered, based on the displayed list of buffers.
524
525@node Buffer Menus
526@subsection Customizing Buffer Menus
b54346bc 527
d04efc64 528@findex bs-show
d04efc64
DL
529@cindex buffer list, customizable
530@table @kbd
531@item M-x bs-show
532Make a list of buffers similarly to @kbd{M-x list-buffers} but
533customizable.
d04efc64
DL
534@end table
535
564ee37b
RS
536 @kbd{M-x bs-show} pops up a buffer list similar to the one normally
537displayed by @kbd{C-x C-b} but which you can customize. If you prefer
538this to the usual buffer list, you can bind this command to @kbd{C-x
539C-b}. To customize this buffer list, use the @code{bs} Custom group
540(@pxref{Easy Customization}).
1ea14188
DL
541
542@findex msb-mode
543@cindex mode, MSB
544@cindex MSB mode
545@cindex buffer menu
546@findex mouse-buffer-menu
564ee37b
RS
547@kindex C-Down-Mouse-1
548 MSB global minor mode (``MSB'' stands for ``mouse select buffer'')
549provides a different and customizable mouse buffer menu which you may
550prefer. It replaces the bindings of @code{mouse-buffer-menu},
551normally on @kbd{C-Down-Mouse-1}, and the menu bar buffer menu. You
552can customize the menu in the @code{msb} Custom group.