vi editing  «Prev  Next»
Lesson 9 Pasting text between files
ObjectiveCut, copy, and paste text between files within a vi session.

Pasting text between files

Before you can paste text between files, you must understand buffers[1]. When you delete or copy text, by default the text is stored in an unnamed buffer. For example, 5dW deletes five words and stores them in a buffer. To put the buffer contents back in the file, you must use the p or P command. If you make another edit before the "put", the unnamed buffer is overwritten by the new edit.
To avoid overwriting your buffer text, you can store it in a named buffer. vi lets you store text in up to 26 buffers, named a to z. At any time during your session, the contents of the buffer can be restored (retrieved) by using a put command, p or P.
To use a named buffer, type a double quote ("), then the buffer name, then the appropriate command to copy, delete, or put your text. For example:
CommandDescription
"a5yy Yank (copy) 5 lines into buffer a
"aP Put the contents of buffer a before the cursor
"bd) Delete from the cursor to the end of the sentence, and store this in buffer b
"bp Put the contents of buffer b after the cursor.

Named buffers guard against overwriting your edits when you are working within a single file, but they are essential when you edit multiple files. Because unnamed buffers are not preserved between files, the only way to paste text into a different file is to store the text in a named buffer.
The following SlideShow shows how to paste text between files:
  1. Assuming that you entered the command, vi recipe quartet,
  2. Delete the first line, Grilled Salmon, and store it in a buffer name a.
  3. You plan to paste the buffer contents into the next file, so you use the :n command to go there.
  4. Instead of going to the next file, vi displays the message No write since last change.
  5. Enter :w to save the recipe file.
  6. This time, the :n command takes you to the quartet file as you expect.
  7. To put the contents of buffer above the line, type a P as the command sequence.
  8. When you are ready to leave vi, enter :wq to write and quit.

vi paste between Files
In the next lesson, you will wrap up this module by reviewing key commands, terms, and concepts you have learned,
and then take a quiz.
[1]buffer: A buffer is an area of memory where data are temporarily stored.