File I/O Commands

As we have already seen, we can read a file into the editors buffer with the e command

e  filename
ee filename

and write out our buffer to a file with the w command.

w  filename

Whenever you read a file with the edit command (e) three things actually occur:

  1. Your current buffer is purged. However, your delete buffer is kept, allowing you to delete from one file and undelete into another.
  2. The filename you specified is memorized.
  3. The contents of the file are read into your empty buffer.

In each example we have specified the filename that the command should operate upon. This is not always necessary. Should you omit the filename it will default to the filename of the last file you specified with your e command. Therefore,

e report

followed by a

w

will have the write command write to the file "report". Likewise, an

e

will simply re-read the last file edited. This is often used when you bungle something and want a fresh copy. Note that in this case you will probably have to issue an

ee

command to indicate that your unsaved buffer should be overwritten. For your protection a simple e will not destroy an unsaved buffer.

If you want to check the name of your current file, you can issue the file command:

f

which will display it in the command area. You can change it (or define it) by following the command with a filename:

f  file1

This will define your current filename as file1.

It is often useful to read another file into a non-empty buffer after some specified line. This can be accomplished with the r command; e.g. to read the file file2 into the buffer after the current line:

r  file2

The command may be prefixed by a number (or pattern search) to explicitly indicate a particular line; e.g. to read file2 after the 10th line in the text buffer:

10r  filename

Similarly, to read file2 after the line containing the next occurrence of "end" in the text buffer:

/end/r filename

The read ("r") command does not affect the current filename. If you do not specify a filename after "r" the editor will read another copy of the current file into your text buffer.

It is also possible to prefix the write ("w") command with a line number or range of lines to write.

Some examples follow.

Write all lines:

w filename

Write line 33 only:

33w filename

Write lines 1,10:

1,10w filename

Write all tagged lines:

#w filename

Finally, you can append to a file by specifying the write append command which is of the same form as the write command:

wa filename

This is useful when you wish to build a new file based upon lines or blocks of lines from your current file (or many files), for example: