Tutorial: Editing Multiple Files Using VI Text Editor

The VI editor provides an advanced feature which allows a user to invoke the editor and then edit multiple files by use of the ":e" (edit) command. This ability to access multiple files without leaving the editor permits a user to look up information in another file without exiting the editor. Additionally, because files are opened within the same editor invocation they can share the same named buffers, thereby making the transfer of text possible between the files. The example on page 40 demonstrates how two lines can be "yanked" from the file oranges, placed into a named buffer "k", and then "put" into the file apples.

When VI is invoked, a work area called a buffer is created for editing purposes. It is into this work space that a copy of a specified disk file is placed. The editor permits only one file copy in this buffer space at a time. Thus after making changes to a file (delete, add, or change), you must inform the editor what you wish done to the current buffer contents before you will be permitted to bring another file into this space. You do this by use of the ":w" (write current buffer contents to opened file), ":e!\ newfile" (toss current buffer contents, no update to opened file, and place a copy of newly called file in buffer), or ":quit!" (exit editor and toss buffer and buffer contents). The editor is smart enough to know that if all you have done is copy or read from a file, it can dispose of the unneeded buffer copy without further instructions from you when a new file is called.

When you have two files open, VI permits toggling between files by use of ":e\ #". This works because whenever VI sees the character "#" used in a command where a filename is expected, it substitutes the "#" with the name of the previous file. For example if you had been in apples then opened oranges, the command ":e #" would return you to where you were in the apples file. Repeat ":e #" and you would be back in oranges.

Another method to "cut" and "paste" between files is to use the mark command (m) in conjunction with named buffers ("). Move the cursor to the first line you wish to copy from oranges and type "ma" (m to mark the line and a to specify which mark). Next move the cursor to the last line you wish to copy and type ""zy'a". This command tells VI to open up "z (buffer named z) and y (yank) a copy of all the lines from the line marked 'a to the current line and place them in the aforementioned buffer. Return to apples with ":e #" and proceed to paste the contents of "z by moving to the desired location and p (put) the buffer contents by typing ""zp".

  1. Open the original file with "vi apples".

  2. Correct typo.

  3. Write the contents of the buffer to the file apples using the ":w" command.

  4. Issue the command ":e oranges" to open the second file.

  5. Yank a copy of two lines from the file oranges and put them into the named buffer "k" using the ""k2yy" command.

  6. Recall the original file, apples, to the buffer using the ":e apples" or ":e #" command.

  7. Contents of buffer discarded when apples is recalled to the buffer. The disk copy of oranges remains unaltered.

  8. Position the cursor and put the lines from the buffer "k" into apples using the ""kp" command.

  9. Write and quit the editor with the ":wq" command.

  10. Buffer is discarded upon leaving the editor.


source: https://engineering.purdue.edu

Post a Comment

0 Comments