Working Commands of vi

The following page is intended to give you a basic working knowledge of vi text editor. For additional help in your Unix environment type: and check the extensive manual entry for vi.

vi (visual) is a display-oriented text editor based on an underlying line editor ex. Both vi and ex were developed by The University of California, Berkeley, California, Computer Science Division, Department of Electrical Engineering and Computer Science.

To create a new file using vi (or editing an existing file) type:

A blank page is will be opened if the filename is new, otherwise the existing file will be opened for editing. When using vi, changes you make to the file are reflected in what you see on your terminal screen. The position of the cursor on the screen indicates the position within the file.

To start inserting (or adding) characters to the document press the i (or a) key. To stop inserting (or adding) characters to the document press the ESC key. There are several other ways to add the characters to the document where some are mentioned here. If you need more detail commands, check the manual entry for vi. The following information is copied from the same manual entry.

COMMAND SUMMARY
In the descriptions, CR stands for carriage return and ESC stands for the escape key.

Sample Commands

                                        arrow keys move the cursor
     h j k l	                      same as arrow keys
     i                                  insert text
     cw                              change word to new
     x                                 delete a character
     dw                              delete a word
     dd                               delete a line
     3dd                             delete 3 lines
     u                                  undo previous change
     ZZ                               exit vi, saving changes
     :q!                               quit, discarding changes
     /text                            search for text
      ESC                         end insert or incomplete cmd
      :w                               write back changes
      :q                               quit
      :q!                              quit, discard changes
      [Ctrl]+G                      show current file and line
      [Ctrl]+F                       forward screen
      [Ctrl]+B                       backward screen
      [Ctrl]+D                       scroll down half screen
      [Ctrl]+U                       scroll up half screen
      nG                               go to the beginning of the specified line
                                         (end default), where n is a line number
      (                                  beginning of sentence
      )                                  end of sentence
      {                                  beginning of paragraph
      }                                  end of paragraph

      [Ctrl]+L                       clear and redraw window
      [Ctrl]+R                      clear and redraw window if [Ctrl]+L is -> key
      [Ctrl]+E                      scroll window down 1 line
      [Ctrl]+Y                      scroll window up 1 line

      H                                top line on screen
      L                                last line on screen
      M                               middle line on screen
      +                                next line, at first non-white
      -                                previous line, at first non-white
      [Enter]                      return, same as +
      v or j                         next line, same column

      ^ or  k                       previous line, same column

      ^                                first non white-space character
      0                                beginning of line
      $                                end of line


      w                                forward a word
      b                                back a word
      e                                end of word
      )                                 to next sentence
      }                                 to next paragraph
      (                                 back a sentence
      {                                 back a paragraph

      ESC                         ends insertion, back to command mode

      a                               append after cursor
     A                                append at end of line
      i                                 insert before cursor
      I                                 insert before first non-blank
      o                               open line below
      O                              open above
      rx                              replace single char with x

   Operators
     Operators are followed by a cursor motion, and affect all text that would
     have been moved over.  For example, since w moves over a word, dw deletes
     the word that would be moved over.  Double the operator, for example, dd
     to affect whole lines.

      d                                delete
      c                                change
      y                                yank lines to buffer

     C                                change rest of line (c$)
      D                               delete rest of line (d$)
      s                                substitute chars (cl)
      S                                substitute lines (cc)
      J                                 join lines
      x                                delete characters (dl)
      X                               delete characters before cursor (dh)
      Y                                yank lines (yy)

   Yank and Put
     Put inserts the text most recently deleted or yanked; however, if a
     buffer is named (using the ASCII lower-case letters a-z), the text in
     that buffer is put instead.

      3yy                             yank 3 lines
      3yl                              yank 3 characters
      p                                 put back text after cursor
      P                                put back text before cursor

        u                               undo last change
      U                                 restore current line
      .                                  repeat last change