Skip to Content
Learning the vi and Vim Editors, 7th Edition
book

Learning the vi and Vim Editors, 7th Edition

by Arnold Robbins, Elbert Hannah, Linda Lamb
July 2008
Beginner
492 pages
16h
English
O'Reilly Media, Inc.
Content preview from Learning the vi and Vim Editors, 7th Edition

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:

:readfilename

or its abbreviation:

:rfilename

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 /home/tim. Position the cursor one line above the line where you want the new data inserted, and enter:

:r /home/tim/data

The entire contents of /home/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 /home/tim/data

Here are other ways to read in a file:

:$r /home/tim/data

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

:0r /home/tim/data

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

:/ pattern /r /home/tim/data

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning the vi and Vim Editors, 8th Edition

Learning the vi and Vim Editors, 8th Edition

Arnold Robbins, Elbert Hannah
Vim Masterclass

Vim Masterclass

Jason Cannon
Mastering Vim

Mastering Vim

Ruslan Osipov

Publisher Resources

ISBN: 9780596529833Errata Page