Using the VI Editor

This document is intended to be an introduction to the use of the VI editor. It is by no means complete.

The VI editor is available on the Unix computers at Niagara at the current writing. In the discussion below, we use the "$" as a generic representative of the operating system's prompt message. Some items will be underlined in order to stress what the user must type.

  1. ENTERING VI is done via a command of the form
    	$ vi filename
    
    where "filename" is the name of the file you wish to edit. VI may be entered in this way whether the file is old or new. Regardless, you enter VI in "command mode" (see the item on MODES, below).

  2. CASE: note that VI commands are case-sensitive -- for example, the command R is different from the command r.

  3. MODES AND MODE CHANGES. The VI editor has two "modes" of operation: text entry mode and command mode. Text entry mode is used for the insertion of new text into the file being edited. Command mode is used for all other editing operations.

    NOTE: VI is an editor, not a word processor. Although editors and word processors are functionally similar, one major difference is that editors require you to strike the RETURN/ENTER key at the end of a line when you enter text in order to create a new line in the computer's memory. This is because editors are generally used to write programs, a situation in which we insist on new lines more frequently than when we write letters to Mom and Dad.


  4. CURSOR MOVEMENT COMMANDS: Note that most of these may be "multiplied" to obtain a repeating effect. For example, the command
              j
    moves the cursor down by one line; the command
              15j
    moves the cursor down by 15 lines. Except as noted, only the unmultiplied are given in the table below:
    Command   Explanation
     j	  move down 1 line
     k	  move up 1 line
    return	  move to start of next line
     h        move left 1 character
    backsp      "    "  1     "
     l        move right 1 character
    space       "    "   1     "
     w        (word) move right 1 word
     b        (back) move left 1 word
     e        (end) move to end of current or next word
     $        move to right end of current line
     0 (zero) move to left end of current line
     H	  move to top of current screen
     L        move to bottom of current screen
    ctrl-u    (up) move up half a screen (12 lines)
    ctrl-d    (down) move down half a screen
    ctrl-f    (forward) move forward 1 screen
    ctrl-b    (back) move back 1 screen
     G        go to end (last line) of file (unmultipliable --
           	     see next command)
    nG        go to line n, e.g.,  250G  moves cursor to line 250
    /string   go (forward) to next occurrence of string.  The
    	     search wraps circularly past the end of the
    	     file, if necessary, to search the earlier part
    	     of the file for the string.
    
    	  Example:  /Niagara
    		    moves the cursor forward to "Niagara"
    
    ?string   go (backward) to previous occurrence of string.
    	     The search wraps circularly past the start of
    	     the file, if necessary, to search the later part
    	     of the file for the string.
    
    	  Example:  ?University
    		    moves the cursor backward to "University"
    
     n	  (next) go the next (forward or backward) occurrence
    	     of the string searched for by the last  /  or  ?
    	     command, respectively
    
  5. DELETE COMMANDS -- as with the cursor movement commands, most of these may be multiplied to obtain a repeating effect.
    Command  Explanation
     x       delete current character
     dw	 (delete word) delete from current character
    	    through end of current "word" including
    	    trailing blank(s)
     dd      delete current line
     dG      delete the part of the file starting with
    	    the current line
     r       replace current character with next character
    	    typed.  For example, if the cursor is at the
                "h" of the when the user types ri, "h" is
                replaced by "i" so that the word becomes tie.
    
  6. COPYING TEXT (copy-and-paste):
  7. MOVING TEXT (cut-and-paste): Substitute dd for Y in the description of copying lines, above.

    NOTE: the P command may be used to insert whatever was last deleted or copied to the temporary buffer. Above, we illustrated copying and moving lines; however, e.g., if 5dw is followed by a cursor movement and then P, the user has moved 5 words.

  8. FILE OPERATIONS:
         Command      Explanation
         :w	          (write) save to default filename without exiting VI
         :w filename  save to file named without exiting VI
         :r filename  (read) import named file, inserting it beginning at
    		     line below current cursor location
         ZZ	          save to default filename and exit VI to the
                         operating system
         :q	          quit VI without saving (use  :q!  if changes you
                         don't wish to save have been made in the file)
    
  9. MISCELLANEOUS COMMANDS:
          Command		Explanation
            ctrl-g    	get line number of current line in file
    	J		join next line to current line,
                               combining 2 lines into 1
    	u               undo previous operation
    

    Additional commands are discussed in this document.


Back to Boxer's Home Page