home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 5.3 Saving and Exiting Files Chapter 5
Introducing the ex Editor
Next: 5.5 Editing Multiple Files
 

5.4 Copying a File into Another File

Sometimes you want to copy text or data already entered on the system into the file you are editing. In vi you can read in the contents of another file with the ex command:

:
read
 

filename

or its abbreviation:

:
r
 
filename

This command inserts the contents of filename starting on the line after the cursor position in the file. If you want to specify a line other than the one the cursor's on, simply type the line number (or other line address) you want before the read or r command.

Let's suppose you are editing the file practice and want to read in a file called data from another directory called /usr/tim . Position the cursor one line above the line where you want the new data inserted, and enter:

:
r /usr/tim/data

The entire contents of /usr/tim/data are read into practice , beginning below the line with the cursor.

To read in the same file and place it after line 185, you would enter:

:
185r /usr/tim/data

Here are other ways to read in a file:

:$r /usr/tim/data

Place the read-in file at the end of the current file.

:0r /usr/tim/data

Place the read-in file at the very beginning of the current file.

:/ pattern /r /usr/tim/data

Place the read-in file in the current file, after the line containing pattern .


Previous: 5.3 Saving and Exiting Files Learning the vi Editor Next: 5.5 Editing Multiple Files
5.3 Saving and Exiting Files Book Index 5.5 Editing Multiple Files

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System