Invoking and leaving the editor

Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

The command:

vi filename

starts up the editor with a copy of the file filename, if it exists, loaded into memory and displayed on the screen. If filename does not exist, you get a blank screen that you can use to create it.

Format of the screen in vi

Bottom line is "status line" - displays messages from vi; also used for entering global commands or search commands.

Remaining lines are your window into the file. If a particular line of the file is wider than the screen width, it wraps around onto additional lines.

If your window goes past the end of the file, the "unused" lines on the screen have a tilde character (~) in column 1 and nothing else - this distinguishes them from blank lines.

You may see a line with an at-sign (@) in the first column. This happens if a very long line must be wrapped onto more lines than are left on the screen. Rather than show only part of that line, possibly leading you to believe that it is shorter than it is, vi will simply show a group of lines containing the @ in column 1. When you move the screen window so that the entire line can now fit, then it will be displayed instead of those @ lines. vi may also put an @ character at the beginning of a line of text that has been changed, but has not yet been redrawn. This was useful in the days of slow terminal connections. You will probably never see this usage.

Vi actually works on a copy of your file in memory, called the buffer. Your original file is not changed unless you explicitly instruct vi to do so, usually when you exit the program. Even then, you have the option of discarding your changes. In general, vi gives you the chance to undo changes you have made.

Most Unix systems, including pangea, are also configured to automatically preserve the "buffer" copy of the file if your connection to the computer is accidentally broken or the computer crashes. In this case, you will generally get an email the next day (after the normal nightly system management programs are run) informing you how to recover your saved vi session. Or you can simply run the command

vi -r

immediately after logging back in from a disconnect or crash to determine if your vi buffer was saved.

If you are concerned about losing changes that you have made to the buffer copy, or if you simply want to update the disk version to make a "snapshot" to which you can recover should further editing cause problems, you can instruct vi to update the original disk copy of the file with the editing changes you have made so far by typing the command:
      :w <CR>
The alternate form

:w newfilename<CR>

saves the changes to a new disk file named newfilename and leaves the original disk copy untouched.

Choices for leaving vi

  • To update the original file with any changes you have made, and then exit the vi program, type one of these equivalent commands:
          :wq <CR>
          :x <CR>
  • To keep the changes in a new copy of the file, without changing the original, and then exit the vi program, type
          :wq new_name <CR>
  • To discard all your changes (no files updated on disk), and then exit the vi program, type
          :q! <CR>

Comments or Questions?