Basic Linux Tutorial
Computer Tutorial

Importing and Exporting Text



Importing and Exporting Text

The vi editor lets you deal with files in many ways. We can open multiple files, copy text from an opened file to save it to another file on disk (export), or insert a disk file into the editor at a specific location (import).
Importing a Disk File into Editor
To insert a disk file into a location in the opened file, we use the :r filename command. The filename is the name of a file, with full path. If the path is not specified, vi searches for the file in the current directory. For example, if you have opened a file file1 in the editor and want to insert another file file2 after line number 8, you first move the cursor to line number 8. Then you issue the :r file2 command. All contents of file file2 are inserted into the editor after the current line. This is a handy way to combine multiple files.

Exporting Text from the Current File

You can export any number of lines so that they are saved as a new file on the disk. We use the w command for this purpose. As an example, if you have opened file1 in the editor and want to save lines 3 to 47 as file3, you can use the command :3,47w file3. If you want to save the opened file as a new file (to make a backup), the same command can be used without line numbers. To save the current file as file4, you use :w file4.