The Incredibly Terse VI Manual

by Charles Roth. Last revised 11 June 1996.

  1. Introduction

    VI is a very powerful text editor that dates from the early days of Unix. It is also quite obscure in its workings. This "cheat sheet" summarizes the most common things you'll want to do in VI, and points the way to some of its more powerful capabilities.

  2. Usage

    To edit a file called "xyz", type: vi xyz

    VI is a "two-mode" editor. When you type a character, it is either:

    VI always starts in the first mode, called "command mode". Section 4 tells you how to switch to the other mode, called "insert" mode.. When in doubt, press the escape key (usually labeled "Esc" or "ESC"). This will put VI in the first, or "command" mode. If you are already in command mode, it will just beep at you. Ignore it.

  3. Moving (the cursor) around in a file

    Short Distances Long Distances
    h move left one character
    l move right one character
    j move down one line ctrl-F move down ("forward") one screen
    k move up one line ctrl-B move up ("back") one screen
    0 (zero) move to start of line 1G move to the first line of the file
    $ move to end of line G move to the last line of the file
    Moving to Special Places Text Searching
    ma mark this location as point "a" /xyz search for and move to next "xyz"
    'a move to previously marked "a" n move to next instance of search
    % move from "(" to matching ")" N move to previous instance of search

  4. Inserting Text

    All of these commands switch VI from "command" mode to "insert" mode. Everything you type after this command gets inserted into the file. Press ESC to go back to command mode.

  5. Editing

    Editing text Editing the file
    x delete this character :w save your work
    dd delete this entire line :q! exit VI, don't save your work
    D delete from here to end of line ZZ save your work, and exit VI
    u undo last operation :r abc read in file abc after this line
    . do last operation again

  6. EX operations

    You may perform more complicated operations on sets of lines in a file by using "ex" commands. All "ex" commands begin with a colon (":") and have the general form:

         :line1,line2,operation
    
    Line numbers may be of the form:

    Operations include:

    Examples:

    Certain characters, when used in between the slashes in the substitute ("s") command, have a special meaning. These characters are called "metacharacters". They are:

    Examples:

    :1,$s/^/Hi! /
    Puts "Hi! " at beginning of every line

    :.s/a.b/ABC/
    On current line, replace "a", any character, "b", with "ABC"

    :1,$s/^.*$/& &/
    Double contents of every line. That is, take each line, and append it to itself with a space in between. The ".*" matches any number of any character, and it's anchored on both ends so you get the entire line. Each "&" gets replaced with the entire match, so you end up "doubling" the entire line.