Vi Tutorial


  1. Modes

    Vi uses a modal system of editing, the two modes are command mode and insert mode. The editor initially comes up on command mode.

    As a general rule it is a good habit to get into to remain in command mode except when you are actually typing in text. Learn to automatically hit <Escape> each time you finish editing.

    Note: You can configure vi to display whether you are in insert mode or command mode. From command mode (the default when vi starts) type in:

    :set showmode<Enter>

  2. Starting vi

    vi begin editing unnamed file
    vi file1 begin editing file1
    vi file1 file2 file3 begin editing 3 files, file1 will appear initially
    vi *.txt begin editing all files ending in '.txt'
    vi -r important.file view file 'important.file' in read-only mode
    view important.file same as vi -r

    Related Commands

    Note that the following commands are to be executed from command mode

  3. Leaving vi

    Note: the following commands are all executable only from command mode

    :qquit (assumes no changes made)
    :q!quit, discard any changes that were made
    :wqwrite the file, then quit
    ZZwrite the file, then quit
    :w newnamewrite the file to 'newname'

    One associated command is :w which writes the current file to disk, but does not exit vi. A good habit to develop is to periodically type :w during your editing session, as this prevents loss of data if the system crashes.

  4. Insert Mode, the most useful commands

  5. Command Mode, navigating the current file

    Although the <PageUp> and <PageDown> and arrow keys work on some systems, I would strongly discourage their use. The following commands are guaranteed to work properly on all systems.

    Actionvi equivalent
    Page Up^F
    Page Down^B
    Left Arrowh
    Right Arrowl
    Up Arrowk
    Down Arrowj
    Home0
    End$

  6. Command Mode, deleting text

    Actionvi equivalent
    delete current linedd
    delete from cursor to end of lineD
    delete character under cursorx
    delete character before cursor (backspace)X

    Note that many of the above commands can be preceeded by a number, for example:

  7. Command Mode, line numbers

    Action vi equivalent
    show line numbers:set nu
    turn off line numbers:set nonu
    display current line number/file name^g
    go to line number (1 for example):1
    go to last line in fileG

  8. Command Mode, cutting, copying and pasting

    Action vi equivalent
    copy current line to "clipboard"yy
    paste contents of "clipboard" below current line p
    paste contents of "clipboard" above current line P
    copy current line, and next 4 lines to "clipboard" 5yy
    write lines 200 -> 205 to temp.file :200,205w temp.file
    write lines 100 -> end of file to temp.file :100,$w temp.file
    write current line -> line 300 to temp.file :.,300w temp.file
    write current line and the next 3 lines to temp.file :.,.+3w temp.file
    write lines 200 -> next to last line of the file to temp.file :200, $-1w temp.file
    read contents of temp.file into current file :r temp.file

  9. Command Mode, searching

    Action vi equivalent
    set case insensitive search:set ic
    set case sensitive search:set noic
    search forward (down) for "hello"/hello
    search backward (up) for "hello"?hello
    search again, (same direction as original)n
    search again, (opposite direction as original)N
    search for "hello" at start of a line/^hello
    search for "hello" at end of a line/hello$
    search for "hello" or "Hello"/[hH]ello
    search for "int" as a word (i.e. not print or sprint) /\<int\>
    search for "eat" but not "beat" or "neat" /\[^bn]eat

  10. Command Mode, replacing

    Action vi equivalent
    replace "dog" with "cat" (first occurrence of dog) on the current line :s/dog/cat
    replace "dog" with "cat" on lines 1 -> 3 (first occurrence of dog on each line) of the file :1,3s/dog/cat
    replace "dog" with "cat" on lines 1 -> 3 of the file, every occurrence :1,3s/dog/cat/g
    replace "dog" with "cat" (every occurrence) for the entire file :1,$s/dog/cat/g
    replace "dog" with "cat" (every occurrence) for the entire file (alternative method) :%s/dog/cat/g
    replace "dog" with "cat" (every occurrence) for the entire file but confirm each replacement :%s/dog/cat/gc

  11. Command Mode, swapping commands

    Action vi equivalent
    swap two letters (i.e. cta -> cat) note that the cursor must be on the first of the two letters xp
    swap two lines (swaps current line with the line below it) ddp

  12. Command Mode, other useful commands

  13. Table of unused letters/symbols

    Letters:g, K, q, V, v
    Control keys:^a, ^k, ^o, ^t, ^w, ^x
    Symbols:_, *, \, =

  14. The .exrc file

    Located in your home directory ($HOME). This file allows you to customize your vi environment, and preserve your settings between sessions.

    Note that although the set and map commands are preceeded by a : when we enter them directly in vi, they have not starting : in the .exrc file. Here is an example .exrc file

          """""""""""""""""""""""""""""""""""""""""""""""""""""""""
          " Note that a " at the start of a line is a comment in
          " the .exrc file
          """""""""""""""""""""""""""""""""""""""""""""""""""""""""
    
          " Map [>] to [Indent current line by 3 spaces]
          map > :s/^/   /^M
    
          " Map [<] to [Delete all spaces / tabs from the beginning 
          " of the current line]
          map < :s/^[     ]*//^M
    
          " enable mode (input/command) display
          set showmode
    
          " default to ignore case for all searching
          set ic