12.4 Accumulating Text
Usually we copy or move text by killing it and yanking it, but there are other convenient methods for copying one block of text in many places, or for copying many scattered blocks of text into one place. Here we describe the commands to accumulate scattered pieces of text into a buffer or into a file.
M-x append-to-buffer
​
Append region to the contents of a specified buffer.
M-x prepend-to-buffer
​
Prepend region to the contents of a specified buffer.
M-x copy-to-buffer
​
Copy region into a specified buffer, deleting that buffer’s old contents.
M-x insert-buffer
​
Insert the contents of a specified buffer into current buffer at point.
M-x append-to-file
​
Append region to the contents of a specified file, at the end.
To accumulate text into a buffer, use M-x append-to-buffer
. This reads a buffer name, then inserts a copy of the region into the buffer specified. If you specify a nonexistent buffer, append-to-buffer
creates the buffer. The text is inserted wherever point is in that buffer. If you have been using the buffer for editing, the copied text goes into the middle of the text of the buffer, starting from wherever point happens to be at that moment.
Point in that buffer is left at the end of the copied text, so successive uses of append-to-buffer
accumulate the text in the specified buffer in the same order as they were copied. Strictly speaking, append-to-buffer
does not always append to the text already in the buffer—it appends only if point in that buffer is at the end. However, if append-to-buffer
is the only command you use to alter a buffer, then point is always at the end.
M-x prepend-to-buffer
is just like append-to-buffer
except that point in the other buffer is left before the copied text, so successive uses of this command add text in reverse order. M-x copy-to-buffer
is similar, except that any existing text in the other buffer is deleted, so the buffer is left containing just the text newly copied into it.
The command M-x insert-buffer
can be used to retrieve the accumulated text from another buffer. This prompts for the name of a buffer, and inserts a copy of all the text in that buffer into the current buffer at point, leaving point at the beginning of the inserted text. It also adds the position of the end of the inserted text to the mark ring, without activating the mark. See Buffers, for background information on buffers.
Instead of accumulating text in a buffer, you can append text directly into a file with M-x append-to-file
. This prompts for a filename, and adds the text of the region to the end of the specified file. The file is changed immediately on disk.
You should use append-to-file
only with files that are not being visited in Emacs. Using it on a file that you are editing in Emacs would change the file behind Emacs’s back, which can lead to losing some of your editing.
Another way to move text around is to store it in a register. See Registers.