Intermediate text editing with vi

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

Vi editor commands can be classified into four different types. vi consists of a full-screen interface grafted onto an earlier command-line editor named ex.

The full-screen editing interface allows you to move the cursor to a particular part of the file that you can see, and then press a key that executes a command to modify a specified amount of text starting at the cursor position or switch into insert mode to add more text.

The older command line interface in ex had no way to locate the cursor and thus automatically judge where in the file you wanted to edit. Instead, you had to type in a line range or a text pattern that defined a portion of the file, followed by the command that affected it. In this interface, you could not have single key commands like vi. Rather, you needed to be able to type a command of arbitrary length, ending it with the RETURN key.

Most f the old ex commands are still accessible in vi and are used for "global" editing functions that are meant to affect more than one line or pattern, or for setting editor parameters and customizations.

The four main types of vi editor commands are:

  1. Single or double character vi commands that take effect immediately without needing to press the RETURN key. These are not displayed anywhere on the screen as you type them. Examples include:
    • Commands to move about in the file:
      h, j, k, l, CTRL-F, CTRL-B, H, M, L, G, w, b, 0, $, etc.
    • Commands to enter insert/append/replace modes:
      i, I, a, A, r, R, s, o, O
    • Commands to delete text:
      x, dd
    • Undo and repeat:
      u, .
  2. ex-style commands that begin with a colon character (:), are shown as you type them on the status line, and must be ended by pressing the RETURN key before they take effect. Examples:
    :w Update file on disk with changes made to memory copy.
    :q Quit the editor.
    :e name Load a new file name into the editor buffer, discarding the current one (need :e! name if you have changed the current one but do not want to keep the changes.)
    :set nu Show line numbers on screen. There are many other "set" commands that control how the file is displayed and how the editor works. These set commands provide some limited customization.
    :n where "n" is an integer value. This command moves the cursor (and window if necessary) to line "n" in the file.
    :r name Read the contents of file name into the current buffer at the location of the cursor. Use to insert contents of one file into another.
    :s This is the string substitution command. It is a very general and powerful string (text) substitution capability.
  3. Ex-style pattern matching commands to search the file for a string. These begin with a slash (/) or question mark (?) character, which is shown on the status line. The string (regular expression) to search for is also shown as you type it. The command does not go into effect until you press the RETURN key.

    Vi commands n and N are then used to continue the search for the next occurrence.

  4. Last class are the operator-object commands. These are the most general and show the underlying structure of the vi command set. In fact, most of the single or double character vi commands discussed already are just special cases of the operator-object syntax or aliases for common combinations.

Comments or Questions?