git-for-steve tweaks.
[sxemacs] / info / sxemacs / buffers.texi
1 @node Buffers, Windows, Files, Top
2 @chapter Using Multiple Buffers
3
4 @cindex buffers
5   Text you are editing in Emacs resides in an object called a
6 @dfn{buffer}.  Each time you visit a file, Emacs creates a buffer to
7 hold the file's text.  Each time you invoke Dired, Emacs creates a buffer
8 to hold the directory listing.  If you send a message with @kbd{C-x m},
9 a buffer named @samp{*mail*} is used to hold the text of the message.
10 When you ask for a command's documentation, it appears in a buffer
11 called @samp{*Help*}.
12
13 @cindex selected buffer
14 @cindex current buffer
15   At any time, one and only one buffer is @dfn{selected}.  It is also
16 called the @dfn{current buffer}.  Saying a command operates on ``the
17 buffer'' really means that the command operates on the selected
18 buffer, as most commands do.
19
20   When Emacs creates multiple windows, each window has a chosen buffer which
21 is displayed there, but at any time only one of the windows is selected and
22 its chosen buffer is the selected buffer.  Each window's mode line displays
23 the name of the buffer the window is displaying (@pxref{Windows}).
24
25   Each buffer has a name which can be of any length but is
26 case-sensitive.  You can select a buffer using its name.  Most
27 buffers are created when you visit files; their names are derived from
28 the files' names.  You can also create an empty buffer with any name you
29 want.  A newly started Emacs has a buffer named @samp{*scratch*} which
30 you can use for evaluating Lisp expressions in Emacs.
31
32   Each buffer records what file it is visiting, whether it is
33 modified, and what major mode and minor modes are in effect in it
34 (@pxref{Major Modes}).  Any Emacs variable can be made @dfn{local to} a
35 particular buffer, meaning its value in that buffer can be different from
36 the value in other buffers.  @xref{Locals}.
37
38 @menu
39 * Select Buffer::   Creating a new buffer or reselecting an old one.
40 * List Buffers::    Getting a list of buffers that exist.
41 * Misc Buffer::     Renaming; changing read-onliness; copying text.
42 * Kill Buffer::     Killing buffers you no longer need.
43 * Several Buffers:: How to go through the list of all buffers
44                      and operate variously on several of them.
45 @end menu
46
47 @node Select Buffer, List Buffers, Buffers, Buffers
48 @section Creating and Selecting Buffers
49 @cindex changing buffers
50 @cindex switching buffers
51
52 @table @kbd
53 @item C-x b @var{buffer} @key{RET}
54 Select or create a buffer named @var{buffer} (@code{switch-to-buffer}).
55 @item C-x 4 b @var{buffer} @key{RET}
56 Similar, but select a buffer named @var{buffer} in another window
57 (@code{switch-to-buffer-other-window}).
58 @item M-x switch-to-other-buffer @var{n}
59 Switch to the previous buffer.
60 @end table
61
62 @kindex C-x 4 b
63 @kindex C-x 5 b
64 @findex switch-to-buffer-other-window
65 @kindex C-x b
66 @findex switch-to-buffer
67 @findex switch-to-buffer-other-frame
68   To select a buffer named @var{bufname}, type @kbd{C-x b @var{bufname}
69 @key{RET}}.  This is the command @code{switch-to-buffer} with argument
70 @var{bufname}.  You can use completion on an abbreviation for the buffer
71 name you want (@pxref{Completion}).  An empty argument to @kbd{C-x b}
72 specifies the most recently selected buffer that is not displayed in any
73 window.@refill
74
75   Most buffers are created when you visit files, or use Emacs commands
76 that display text.  You can also create a buffer explicitly by typing
77 @kbd{C-x b @var{bufname} @key{RET}}, which creates a new, empty buffer
78 that is not visiting any file, and selects it for editing.  The new
79 buffer's major mode is determined by the value of
80 @code{default-major-mode} (@pxref{Major Modes}).  Buffers not visiting
81 files are usually used for making notes to yourself.  If you try to save
82 one, you are asked for the file name to use.
83
84  The function @code{switch-to-buffer-other-frame} is similar to
85 @code{switch-to-buffer} except that it creates a new frame in which to
86 display the selected buffer.
87
88 @findex switch-to-other-buffer
89 Use @kbd{M-x switch-to-other-buffer} to visit the previous buffer. If
90 you supply a positive integer @var{n}, the @var{n}th most recent buffer
91 is displayed. If you supply an argument of 0, the current buffer is
92 moved to the bottom of the buffer stack.
93
94   Note that you can also use @kbd{C-x C-f} and any other command for
95 visiting a file to switch buffers.  @xref{Visiting}.
96
97 @node List Buffers, Misc Buffer, Select Buffer, Buffers
98 @section Listing Existing Buffers
99
100 @table @kbd
101 @item C-x C-b
102 List the existing buffers (@code{list-buffers}).
103 @end table
104
105 @kindex C-x C-b
106 @findex list-buffers
107   To print a list of all existing buffers, type @kbd{C-x C-b}.  Each
108 line in the list shows one buffer's name, major mode, and visited file.
109 A @samp{*} at the beginning of a line indicates the buffer has been
110 ``modified''.  If several buffers are modified, it may be time to save
111 some with @kbd{C-x s} (@pxref{Saving}).  A @samp{%} indicates a read-only
112 buffer.  A @samp{.} marks the selected buffer.  Here is an example of a
113 buffer list:@refill
114
115 @smallexample
116  MR Buffer         Size  Mode           File
117  -- ------         ----  ----           ----
118 .*  emacs.tex      383402 Texinfo       /u2/emacs/man/emacs.tex
119     *Help*         1287  Fundamental
120     files.el       23076 Emacs-Lisp     /u2/emacs/lisp/files.el
121   % RMAIL          64042 RMAIL          /u/rms/RMAIL
122  *% man            747   Dired          /u2/emacs/man/
123     net.emacs      343885 Fundamental   /u/rms/net.emacs
124     fileio.c       27691 C              /u2/emacs/src/fileio.c
125     NEWS           67340 Text           /u2/emacs/etc/NEWS
126     *scratch*      0     Lisp Interaction
127 @end smallexample
128
129 @noindent
130 Note that the buffer @samp{*Help*} was made by a help request; it is not
131 visiting any file.  The buffer @code{man} was made by Dired on the
132 directory @file{/u2/emacs/man/}.
133
134 As you move the mouse over the @samp{*Buffer List*} buffer, the lines
135 are highlighted.  This visual cue indicates that clicking the right
136 mouse button (@code{button3}) will pop up a menu of commands on the
137 buffer represented by this line.  This menu duplicates most of those
138 commands which are bound to keys in the @samp{*Buffer List*} buffer.
139
140 @node Misc Buffer, Kill Buffer, List Buffers, Buffers
141 @section Miscellaneous Buffer Operations
142
143 @table @kbd
144 @item C-x C-q
145 Toggle read-only status of buffer (@code{toggle-read-only}).
146 @item M-x rename-buffer
147 Change the name of the current buffer.
148 @item M-x view-buffer
149 Scroll through a buffer.
150 @end table
151
152 @cindex read-only buffer
153 @kindex C-x C-q
154 @findex toggle-read-only
155 @vindex buffer-read-only
156   A buffer can be @dfn{read-only}, which means that commands to change
157 its text are not allowed.  Normally, read-only buffers are created by
158 subsystems such as Dired and Rmail that have special commands to operate
159 on the text.  Emacs also creates a read-only buffer if you
160 visit a file that is protected.  To make changes in a read-only buffer,
161 use the command @kbd{C-x C-q} (@code{toggle-read-only}).  It makes a
162 read-only buffer writable, and makes a writable buffer read-only.  This
163 works by setting the variable @code{buffer-read-only}, which has a local
164 value in each buffer and makes a buffer read-only if its value is
165 non-@code{nil}.
166
167 @findex rename-buffer
168   @kbd{M-x rename-buffer} changes the name of the current buffer,
169 prompting for the new name in the minibuffer.  There is no default.  If you
170 specify a name that is used by a different buffer, an error is signalled and
171 renaming is not done.
172
173 @findex view-buffer
174   @kbd{M-x view-buffer} is similar to @kbd{M-x view-file} (@pxref{Misc
175 File Ops}), but it examines an already existing Emacs buffer.  View mode
176 provides convenient commands for scrolling through the buffer but not
177 for changing it.  When you exit View mode, the resulting value of point
178 remains in effect.
179
180 To copy text from one buffer to another, use the commands @kbd{M-x
181 append-to-buffer} and @kbd{M-x insert-buffer}.  @xref{Accumulating
182 Text}.@refill
183
184 @node Kill Buffer, Several Buffers, Misc Buffer, Buffers
185 @section Killing Buffers
186
187   After using Emacs for a while, you may accumulate a large number of
188 buffers and may want to eliminate the ones you no
189 longer need.  There are several commands for doing this.
190
191 @c WideCommands
192 @table @kbd
193 @item C-x k
194 Kill a buffer, specified by name (@code{kill-buffer}).
195 @item M-x kill-some-buffers
196 Offer to kill each buffer, one by one.
197 @end table
198
199 @findex kill-buffer
200 @findex kill-some-buffers
201 @kindex C-x k
202
203   @kbd{C-x k} (@code{kill-buffer}) kills one buffer, whose name you
204 specify in the minibuffer.  If you type just @key{RET} in the
205 minibuffer, the default, killing the current buffer, is used.  If the
206 current buffer is killed, the buffer that has been selected recently but
207 does not appear in any window now is selected.  If the buffer being
208 killed contains unsaved changes, you are asked to confirm with @kbd{yes}
209 before the buffer is killed.
210
211   The command @kbd{M-x kill-some-buffers} asks about each buffer, one by
212 one.  An answer of @kbd{y} means to kill the buffer.  Killing the current
213 buffer or a buffer containing unsaved changes selects a new buffer or asks
214 for confirmation just like @code{kill-buffer}.
215
216 @node Several Buffers,, Kill Buffer, Buffers
217 @section Operating on Several Buffers
218 @cindex buffer menu
219
220   The @dfn{buffer-menu} facility is like a ``Dired for buffers''; it allows
221 you to request operations on various Emacs buffers by editing a
222 buffer containing a list of them.  You can save buffers, kill them
223 (here called @dfn{deleting} them, for consistency with Dired), or display
224 them.
225
226 @table @kbd
227 @item M-x buffer-menu
228 Begin editing a buffer listing all Emacs buffers.
229 @end table
230
231 @findex buffer-menu
232   The command @code{buffer-menu} writes a list of all Emacs buffers into
233 the buffer @samp{*Buffer List*}, and selects that buffer in Buffer Menu
234 mode.  The buffer is read-only.  You can only change it using the special
235 commands described in this section.  Most of the commands are graphic
236 characters.  You can use  Emacs cursor motion commands in the
237 @samp{*Buffer List*} buffer.  If the cursor is on a line describing a
238 buffer, the following  special commands apply to that buffer:
239
240 @table @kbd
241 @item d
242 Request to delete (kill) the buffer, then move down.  A @samp{D} before
243 the buffer name on a line indicates a deletion request.  Requested
244 deletions actually take place when you use the @kbd{x} command.
245 @item k
246 Synonym for @kbd{d}.
247 @item C-d
248 Like @kbd{d} but move up afterwards instead of down.
249 @item s
250 Request to save the buffer.  An @samp{S} before the buffer name on a line
251 indicates the request.  Requested saves actually take place when you use
252 the @kbd{x} command.  You can request both saving and deletion for the
253 same buffer.
254 @item ~
255 Mark buffer ``unmodified''.  The command @kbd{~} does this
256 immediately when typed.
257 @item x
258 Perform previously requested deletions and saves.
259 @item u
260 Remove any request made for the current line, and move down.
261 @item @key{DEL}
262 Move to previous line and remove any request made for that line.
263 @end table
264
265   All commands that add or remove flags to request later operations
266 also move down a line.  They accept a numeric argument as a repeat count,
267 unless otherwise specified.
268
269   There are also special commands to use the buffer list to select another
270 buffer, and to specify one or more other buffers for display in additional
271 windows.
272
273 @table @kbd
274 @item 1
275 Select the buffer in a full-frame window.  This command takes effect
276 immediately.
277 @item 2
278 Immediately set up two windows, with this buffer in one and the
279 buffer selected before @samp{*Buffer List*} in the other.
280 @item f
281 Immediately select the buffer in place of the @samp{*Buffer List*} buffer.
282 @item o
283 Immediately select the buffer in another window as if by @kbd{C-x 4 b},
284 leaving @samp{*Buffer List*} visible.
285 @item q
286 Immediately select this buffer, and display any buffers previously
287 flagged with the @kbd{m} command in other windows.  If there are no
288 buffers flagged with @kbd{m}, this command is equivalent to @kbd{1}.
289 @item m
290 Flag this buffer to be displayed in another window if the @kbd{q}
291 command is used.  The request shows as a @samp{>} at the beginning of
292 the line.  The same buffer may not have both a delete request and a
293 display request.
294 @end table
295
296  Going back between a @code{buffer-menu} buffer and other Emacs buffers is
297 easy.  You can, for example, switch from the @samp{*Buffer List*}
298 buffer to another Emacs buffer, and edit there.  You can then reselect the
299 @code{buffer-menu} buffer and perform operations already
300 requested, or you can kill that buffer or pay no further attention to it.
301  All that @code{buffer-menu} does directly is create and select a
302 suitable buffer, and turn on Buffer Menu mode.  All the other
303 capabilities of the buffer menu are implemented by special commands
304 provided in Buffer Menu mode.
305
306   The only difference between @code{buffer-menu} and @code{list-buffers} is
307 that @code{buffer-menu} selects the @samp{*Buffer List*} buffer and
308 @code{list-buffers} does not.  If you run @code{list-buffers} (that is,
309 type @kbd{C-x C-b}) and select the buffer list manually, you can use all
310 the commands described here.