AIX Tip of the Week

AIX Tip of the Week: Cutting/Pasting Text Between Files in vi

Audience: AIX Users

Date: May 28, 1999

A common editing task is to cut and paste text from one file into another. There are several ways to do this in vi. One way is to copy the source file to a temp file and edit out the unwanted lines. Next, edit the target file, and read in the desired text from the temp file using the vi command ":r temp".

A more efficient method is to copy the part of the source file you want into a vi buffer, switch to the target file, and paste the lines from the buffer. Sounds complicated, but is easy to do in practice. Here's an example of how.

  1. vi source_file target_file
  2. "a20,40yy
  3. :n
  4. "ap

Line 1 starts the vi editing session with two files. The first file to be edited will be the source_file.

Line 2 copies (aka "yanks") lines 20 through 40 into a buffer labeled "a". You can use any range of lines, and up to 26 different buffers (a-z).

Line 3 switches the editing session from the source_file to the target_file. You would then position the cursor to where you want to paste the copied text.

Line 4 copies the contents from buffer "a" to the line after the cursor.

Using vi's buffers can also be used to copy (or move) lines within a file.