Moving text from one file to another
Last revision August 2, 2004
Table of Contents:
|
You can easily insert the entire contents of one file into another using the :r command. After typing the colon (:) character, the cursor will jump down to the command/status line. There you can type r (for "read"), followed by a space, and then the name of the file that you want to read. The entire contents of that file will be copied into the file you are currently editing, starting on a new line after the line where the cursor was located.
The :r command also lets you insert the output of another program into your file. Instead of giving it the name of a file to read, type the exclamation point (!) followed by the name of a command to run. The standard output of that command will be captured and inserted into your file, starting on a new line after the line where the cursor is located. For example, the vi command
:r ! ls -l
will run the ls -l program for the current directory and insert the output listing into your file.
There are two ways to copy a section of text, rather than the entire file, from one file into another.
You can simply make a copy of the entire source file that has the text that you want to insert, edit that copy to delete all text before and after the desired section, and then use the vi :r command to insert that now-truncated copy into the second file. For example, say you want to copy only lines 20 through 39 from file B to file A. You could do this with the following combination of shell and vi commands. In this example, shell commands are shown with the % prompt, and vi commands are shown indented further with no prompt. Also, explanatory comments that are not part of the actual command are shown in square brackets.
% cp B B.part | [make copy of source file] |
% vi B.part | [edit this copy] |
40G | [vi command; move to line 40] |
dG | [vi command; delete to end of file] |
1G | [vi command; move to line 1] |
19dd | [vi command; delete lines 1-19] |
:wq | [vi command; update disk file and quit] |
% vi A | [edit destination file] |
[move cursor to desired location] | |
:r B.part | [insert contents of B.part at this location] |
A faster and more elegant way to copy a section of text from one file to another uses the named delete buffers, which persist even if you switch files within the same vi session.
First, start vi, editing the "source" file (with the lines to be copied). You yank (copy) or delete the block of text to be copied (or moved) into a named buffer. Then you use the :e command to switch from editing the "source" file to editing the "destination" file. Finally, you use a vi p (put) command to put the lines from that named buffer into the destination file at the location of the cursor.
If you have changed the source file, the plain :e command will not let you load the destination file unless you first save the changes to the source file with :w. If you do not want to save the changes to the source file, use :e! to force loading of the destination file while discarding unsaved changes to the source file.
For example, you can repeat the example above using the delete buffers rather than extra disk files to copy only lines 20 through 39 from file B to file A. In this example, shell commands are shown with the % prompt, and vi commands are shown indented further with no prompt. Also, explanatory comments that are not part of the actual command are shown in square brackets after it.
% vi B | [start by opening source file] |
20G | [move to line 20] |
"a20yy | [copy 20 lines to buffer a] |
:e! A | [switch to editing destination file] |
[move cursor to desired location] | |
"ap | [insert buffer a contents after cursor] |