The vi Editor

  1. vi is universal.
    vi is the lingua franca of UNIX editing.

    vi is a screen oriented line editor that exists in nearly identical form on every UNIX system.

  2. The two modes of vi
    vi uses the keyboard for both commands and text input. vi was written to be used on a variety of terminals, so it doesn't assume any special key capability.

    Because vi uses the same keyboard for command and insert, what a key does depends upon the mode you are in. The vi editor has two modes: command and insert. The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes. The insert mode puts anything typed on the keyboard into the current file.

    vi starts out in command mode. There are several commands that put the vi editor into insert mode. The most commonly used commands to get into insert mode are a and i. Once you are in insert mode, you get out of it by hitting the escape key.

  3. vi commands

    vi commands are case sensitive, are not echoed on the screen, do not require a return after them and are usually one letter. They may contain a count prefix such as 5j.

    A special group of commands (ex) are available after /,? or : . / and ? are search commands, : initiates an ex command.

  4. Starting vi
    To start vi enter the command.
    vi [filename]

  5. Exiting vi
    1. To exit vi without saving.
      :q
    2. To exit and save
      :wq
      or
      ZZ


  6. Some Simple vi commands:
    Here is a simple set of commands to get a beginning vi user started.


    a
    enter insert mode, the characters typed in will be inserted after the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.

    h
    move the cursor to the left one character position.

    i
    enter insert mode, the characters typed in will be inserted before the current cursor position. If you specify a count, all the text that had been inserted will be repeated that many times.

    j
    move the cursor down one line.

    k
    move the cursor up one line.

    l
    move the cursor to the right one character position.

    r
    replace one character under the cursor. Specify count to replace a number of characters

    u
    undo the last change to the file. Typing u again will re-do the change.

    x
    delete character under the cursor. Count specifies how many characters to delete. The characters will be deleted after the cursor.


  7. Text Buffers in vi
    The vi editor has 36 buffers for storing pieces of text, and also a general purpose buffer. Any time a block of text is deleted or yanked from the file, it gets placed into the general purpose buffer. The block of text is also stored in another buffer as well, if it is specified. The buffer is specified using the " command. After typing ", a letter or digit specifying the buffer must be entered. For example, the command: "mdd uses the buffer m, and the last two characters stand for delete current line. Similarly, text can be pasted in with the p or P command. "mp pastes the contents of buffer m after the current cursor position. For any of the commands used in the next two sections, these buffers can be specified for temporary storage of words or paragraphs.

  8. Cutting and Yanking
    The command commonly used command for cutting is d. This command deletes text from the file. The command is preceded by an optional count and followed by a movement specification. If you double the command by typing dd, it deletes the current line. Here are some combinations of these:
    d^
    deletes from current cursor position to the beginning of the line.
    d$
    deletes from current cursor position to the end of the line.
    dw
    deletes from current cursor position to the end of the word.
    3dd
    deletes three lines from current cursor position downwards.

    There is also the y command which operates similarly to the d command which take text from the file without deleting the text.

  9. Pasting
    The commands to paste are p and P. The only differ in the position relative to the cursor where they paste. p pastes the specified or general buffer after the cursor position, while P pastes the specified or general buffer before the cursor position. Specifying count before the paste command pastes text the specified number of times.

  10. Searching
    For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the bottom line, where you type the particular string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the ? command searches backwards (upwards) in the file. Some characters have special meanings to vi, so they must be preceded by a backslash (\) to be included as part of the search expression.

    Special characters:

    ^
    Beginning of the line. (At the beginning of a search expression.)
    .
    Matches a single character.
    *
    Matches zero or more of the previous character.
    $
    End of the line (At the end of the search expression.)
    [
    Starts a set of matching, or non-matching expressions... For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad
    <
    Put in an expression escaped with the backslash to find the ending or beginning of a word. For example: /\<the\> should find only word the, but not words like these: there and other.
    >
    See the '<' character description above.



  11. Settings for vi (and ex)
    You can customize the way VI behaves upon start up. There are several edit options which are available using the :set command, these are the vi and ex editor options available (You can get this list by typing :set all and then return in command mode)

    noautoindent        magic                               noshowmatch
    autoprint           mesg                                noshowmode
    noautowrite         nomodelines                         noslowopen
    nobeautify          nonumber                            tabstop=8
    directory=/tmp      nonovice                            taglength=0
    nodoubleescape      nooptimize                          tags=tags /usr/lib/tags
    noedcompatible      paragraphs=IPLPPPQPP LIpplpipnpbp   term=xterm
    noerrorbells        prompt                              noterse
    noexrc              noreadonly                          timeout
    flash               redraw                              timeoutlen=500
    hardtabs=8          remap                               ttytype=xterm
    noignorecase        report=5                            warn
    keyboardedit        scroll=11                           window=23
    keyboardedit!       sections=NHSHH HUuhsh+c             wrapscan
    nolisp              shell=/bin/csh                      wrapmargin=0
    nolist              shiftwidth=8                        nowriteany
    

    Some of these options have values set with the equals sign '=' in it, while others are either set or not set. (These on or off type of options are called Boolean, and have "no" in front of them to indicate that they are not set.) The options shown here are the options that are set without any customization. Descriptions of some of these are given below, with an abbreviation. For example, the command set autoindent, you can type :set autoindent or :set ai. To unset it, you can type :set noautoindent or :set noai.

    autoindent (ai)
    This option sets the editor so that lines following an indented line will have the same indentation as the previous line. If you want to back over this indentation, you can type ^D at the very first character position. This ^D works in the insert mode, and not in command mode. Also, the width of the indentations can be set with shiftwidth, explained below.
    exrc
    The .exrc file in the current directory is read during startup. This has to be set either in the environment variable EXINIT or in the .exrc file in your home directory.
    mesg
    Turn off messages if this option is unset using :set nomesg, so that nobody can bother you while using the editor.
    number (nu)
    Displays lines with line numbers on the left side.
    shiftwidth (sw)
    This option takes a value, and determines the width of a software tabstop. (The software tabstop is used for the << and >> commands.) For example, you would set a shift width of 4 with this command: :set sw=4.
    showmode (smd)
    This option is used to show the actual mode of the editor that you are in. If you are in insert mode, the bottom line of the screen will say INPUT MODE.
    warn
    This option warns you if you have modified the file, but haven't saved it yet.
    window (wi)
    This option sets up the number of lines on the window that VI uses. For example, to set the VI editor to use only 12 lines of your screen (because your modem is slow) you would use this: :set wi=12.
    wrapscan (ws)
    This option affects the behavior of the word search. If wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning.
    wrapmargin (wm)
    If this option has a value greater than zero, the editor will automatically "word wrap". That is, if you get to within that many spaces of the left margin, the word will wrap to the next line, without having to type return. For example, to set the wrap margin to two characters, you would type this: :set wm=2.


  12. The .exrc file
    If you create a file called .exrc in your home directory, all the commands in there will be read when vi starts up.

  13. Recovering Your Work When Something Goes Wrong
    The vi editor edits a temporary copy of your file, and after the editing is complete, or when you tell it to save, it puts the contents of the temporary copy into the original file. If something goes wrong while you are editing your file, the vi editor will attempt to save whatever work you had in progress, and store it for later recovery. (Note: If vi dies while you were working on any file, it sends you an email message on how to recover it). The -r option stands for recovery. If you were editing the file vitalinfo, and you accidentally got logged out, then the -r option of the 'vi' editor should help. The command would look somewhat like this: vi -r vitalinfo After using the -r option once, though, you MUST save what you have recovered to the actual file... The -r option only works once per failed VI session.

© Allan Kochis Last revision 1/7/2000