Reading and Writing Files


Exit (ZZ)

The vim editor reads a disk file into the Work buffer when you specify a filename on the command line you use to call vim. The ZZ command that terminates the editing session writes the contents of the Work buffer back to the disk file. This section discusses other ways of reading text into the Work buffer and writing it to a file.

Reading Files

Read (:r)

The Read command reads a file into the Work buffer. The new file does not overwrite any text in the Work buffer but rather is positioned following the single address you specify (or the current line if you do not specify an address). You can use an address of 0 to read the file into the beginning of the Work buffer. The Read command has the following syntax:

:[address]r [filename]


As with other commands that begin with a colon, when you enter the colon it appears on the status line. The filename is the pathname of the file that you want to read and must be terminated by RETURN. If you omit the filename, vim reads the file you are editing from the disk.

Writing Files

Write (:w)

The Write command writes part or all of the Work buffer to a file. You can use an address to write out part of the Work buffer and a filename to specify a file to receive the text. If you do not use an address or filename, vim writes the entire contents of the Work buffer to the file you are editing, updating the file on the disk.

During a long editing session, it is a good idea to use the Write command occasionally. If a problem later develops, a recent copy of the Work buffer is then safe on the disk. If you use a :q! command to exit from vim, the disk file reflects the version of the Work buffer at the time you last used the Write command. The Write command has two possible formats:

:[address]w[!] [filename] :[address]w>> filename


The second format appends text to an existing file. The address specifies the portion of the Work buffer vim will write to the file. The address follows the form of the address that the Substitute command uses (page 172). If you do not specify an address, vim writes the entire contents of the Work buffer. The optional filename is the pathname of the file you are writing to. If you do not specify a filename, vim writes to the file you are editing.

w!

Because the Write command can quickly destroy a large amount of work, vim demands that you enter an exclamation point (!) following the w as a safeguard against accidentally overwriting a file. The only times you do not need an exclamation point are when you are writing out the entire contents of the Work buffer to the file being edited (using no address and no filename) and when you are writing part or all of the Work buffer to a new file. When you are writing part of the file to the file being edited or when you are overwriting another file, you must use an exclamation point.

Identifying the Current File

The File command (:f) provides the same information as the Status command (CONTROL-G, page 175). The filename the File command displays is the one the Write command uses if you give a :w command without a filename.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net