vi Editor Cheat-Sheet


The vi editor: This is an old-fashioned text-file editor in the Unix world. It uses the full screen (or full window), but it does not use a mouse. It is useful because it should always be available and should always work. vi is enormously complicated -- whole books cover just this editor. (Your second text, A Practical Guide to Linux, by Sobell has his Chapter 8 on Linux: 44 pages.) It is notoriously confusing for beginners, but you can get by very well with just the 18 commands given in the next section. These 18 commands are all that you will be responsible for knowing on exams. For common editing tasks, a mouse-based editor is often more appropriate. Very intricate editing might better be done with a shell script or with a language like Perl. Unix commands below are in bold green.


The basics that you must know:

Basics of vi that you must know
To enter the vi editor, type vi on the command line followed by a file name to create or open:

    % vi file.c

Produces a clear screen if it's a new file, with ~ running down the left side.

You can get by with a very short list of commands (18 of them):

    i, I: -insert or insert at start of line, and enter input mode
    a, A: -append or append at end of line, and enter input mode
    Esc: -leave input mode, enter command mode
    x, dd: -delete a char, delete a line (in command mode only)
    h, l, j, k: -move left, right, up, down (in command mode only)
    ZZ: -save changes, exit vi (in command mode only)
    :w, :w filename: -write, write to "filename", no exit (in command mode)
    :q, :q!: -quit and don't save, quit and don't save even if changes (in command mode)
    /sometext: -search for "sometext" (in command mode)
    :23: -go to line 23 (in command mode)

If in doubt, press "Esc". Then type ":q!" (to exit vi without saving changes), and start over.

Remember, in input mode (after an i, I, a, or A command), all key strokes except Esc become a character in the file.


Carrying on: Below are useful commands to make editing more convenient. Try to relate these to the word processing techniques you use in the gui world with a mouse.

"Command mode"

"Input Mode" - editing your code...


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.