Vi Quick Reference. (aka cheat-sheet)


This is a quick guide to the vi editor. It is not intended to be complete (a hopeless task) nor definitive. In fact it is definitively not definitive. None the less you should find it useful.


  1. Critical basic fact about vi. Vi has two modes. The default mode is a full-screen editor, which works more or less like other editors, and command line mode which works like an old fashioned line editor (and is, in fact, the "ed" or "ex" editor). In order to effectively use vi you need to know how to use both modes because some things are much easier in one mode than another. You change from the full-screen mode to the command mode by entering the ':' (colon - the punctuation mark not the organ). When it has executed a command it goes back to the full-screen mode (if you don't want to do anything, just hit carriage rerturn for a "null" command).

  2. Moving around in visual mode. The arrow keys and page up/page down usually work. However, real UNIX geeks use the keyboard commands.

    h

    j

    k

    l

    right

    down

    up

    left

    You may have noticed that keyboards, in general have a little dimple on the 'j' and 'f' keys. This is useful in keeping your hands in the right place. Also if you are a touch typist (the single most useful high school course i ever took ;-} ) then you don't have to remove your hands from the keyboard and can type much faster. Most implementations of vi do not pay attention to the mouse, so if you move the mouse over the text it won't help you.

  3. Repeating commands. The lazy way to do this is just repeat the command. However if you enter a number before the command then the command is repeated that many times. For example "80l" moves left 80 times.

  4. Inserting/editing text. Once you can move around a file you need to be able to change it. Here is a short list of commands.

    i

    Insert text right at the current position. Exit with the escape key

    a

    Append text right after the current position. Exit with the escape key

    O

    Open a new line above the current one and put it in insert mode

    o

    Open a new line below the current one and put it in insert mode.

    These commands let you create new stuff. They can be repeated by pre-pending a number to the command. "80i\" will produce a row of 80 '\' characters. This is sort of useful for producing fancy dividers in programs.

  5. Deleting, cutting and pasting text.

    dd

    delete a line

    dw

    delete a word

    dl

    delete the character at the cursor

    x

    delete a character.

    yy

    yank a line

    P

    Paste what has been deleted or yanked above the current line

    p

    Paste what has been deleted or yanked below the current line

    Again the repeat command works with all of these, so "10dd 4P" deletes ten lines and pastes them back 4 times above the current position. The 'x' command does not put the deleted text into the paste buffer, in case you were wondering why there is a dl and an x.

  6. Finding things.

    ?<string>

    find the string above the current place

    /<string>

    find the string below the current place

    n

    find the next copy of the string

    ?

    if you are going down, go up

    /

    if you are going up, go down

  7. Replacing text. The command 'r' replaces the current letter with the next one you type. (i.e. 'ra' replaces the current letter with 'a'). the command R puts you in a "replace mode" where every character gets replaced.

  8. Undoing things. Try 'u'. Different versions of vi will let you undo different amounts.

    At a minimum vi lets you undo one command.

  9. Repeating the same command. The command '.' repeats the last command. It doesn't repeat cursor movements so you can move from line to line or character to character and do the same thing over and over and over and over.

  10. Command line options.

    :<number>

    go to line "number" (:10 == go to line 10)

    :<range>s/<string_one>/<another_string>

    replace the first occurance in each line of one string with another. If you append '/g' to this it replaces all of the occurances.

    <range> commands


    %

    all

    $

    The current line

    number,number

    a specific range (1,2 is the first two lines)

    $,number or number,$

    you can use $ as a number too.

    (nothing)

    No range means the current line

    :u

    undo works here too

    :<range>d

    delete

    :!<command>

    execute a command

    :help

    (sometimes) help

    :v

    go back to visual

    Strings can be specified with wild cards. Not surprisingly the wild cards are similar to the command line in UNIX. These include * for any string, . for any character (NOT ?) , and [...] for a range of characters. You can also use the '\' character to "escape" a character (as in \* for '*' or \/ for '/' (what do you think :%s/\/\\/g does?)).

  11. Exiting and saving a file (or not saving a file).

ZZ

write the current file and exit (not recommended because you will one day overwrite something you don't want to).

:w

Write the current file

:w!

Force it to try to write the current file

q

quit (if you can)

:q!

Force it to quit without saving

:wq

Write and quit

:w!q!

Force it to write and to quit


This has just begun to scratch the surface of the things you can do in vi. I haven't covered (and rarely use) things like marking text or command macros. There is also a whole swarm of status variables you can set. These do amusing things, like making the editor be aware of the language you are using, but are not exactly portable from version to version.