vi Commands

Nalini Hasija
University of Georgia
20 February 2004

This document tells you how to build Web pages on It covers the basic knowledge you
will need in the following areas:

Updated: 16 September, 2015

Discover Life | Education | Training Guides | Building Web Pages | vi Commands

vi is a text editor that you can use on the Web server. It is very powerful and somewhat difficult to learn, but don't be intimidated. Sometimes it is easier to make a quick change to a file that you've already transferred instead of changing the file on your local machine and transferring it again. Use vi under these cirumstances.

Getting Started

Three Basic vi Steps:

  • Open:
    • To back up a text document before you make any changes, type the command:
      bak filename
      where filename is the name of the document that you want to open and edit.

      This sends a back up copy of the text document to the OLD directory under UNIX.

    • To open a text document in order to edit, type the command:
      vi filename
      where filename is the name of the document that you want to edit.
    • Important Note: vi must be in lower case
      The case of the filename is very important.
      In UNIX, filename is not the same as FILENAME.

  • Edit:
    • Use the vi commands listed below to edit the vi document.
    • To read a text file called filename into a vi file, use the command:
      :r filename (colon r filename) for read to filename.
    • To write a vi file to a text file called junk, use the command:
      :w junk (colon w junk) for read to junk.
  • Save and Quit:
    • Note: : (colon) puts you into command mode.

    • To save changes at any time use the command:
      :w (colon w) for write.
    • To exit vi at any time use the command:
      :q (colon q) for quit.
    • To save and exit vi, use the command:
      :wq (colon w q) for write and then quit vi.
    • To abandon a file before you save it, use the command:
      :q! (colon q exclamation) for I really want to quit!!!
    • To reload the vi file at any time use the command:
      :e! (colon e exclamation).

    Moving cursor

    When you are in vi you cannot use your mouse to move around. You must use your arrow keys to move the cursor (or use the h, j, k, l keys if your keyboard lacks arrows).

    • $ (dollar sign) moves the cursor to the end of the current line.
    • 0 (zero) moves the cursor to the beginning of the current line.
    • 1G (one CAPITAL G) goes to the first line of the document.
    • G (CAPITAL G) goes to the last line of the document.
    Searching text

    • /findtext (slash) moves the cursor down to the next occurrence of findtext.
    • ?Findtext (question mark) moves cursor to the last occurrence of Findtext.
    • n (lower case n) repeats the last search command.
    • ma (lower case m a) sets the marker a on a line.
    • 'b (apostrophe lower case b) goes to the marker b, assuming you set it earlier.
    Warning: Findtext is different from findtext. UNIX is case sensitive!!!

    Adding text

    Note: Use <esc> key to stop adding text. <esc> designates the single key marked Esc. Do not put < > on either side of it.

    • i sometext <esc> (lower case i) inserts sometext before the cursor.
    • a sometext <esc> (lower case a) adds sometext after the cursor.
    • I sometext <esc> (CAPITAL I) inserts sometext at beginning of current line.
    • A sometext <esc> (CAPITAL A) adds sometext at end of the current line.
    • o sometext <esc> (lower case o) starts a new line, adding sometext below current line.
    • O sometext <esc> (CAPITAL O) starts new line, adding sometext above current line.

    Deleting text

    • x (lower case x) deletes the character below the cursor.
    • 4x (four lower case x) deletes 4 characters .
    • dd (lower case d d) deletes the current line.
    • 3dd (three lower case d d) deletes 3 lines .
    • dw (lower case d w) deletes one word.
    • d$ (lower case d dollar sign) deletes to the end of the line.
    • dG (lower case d CAPITAL G) deletes to the end of the document.

    Changing text

    • r (lower case r) replaces the character under the cursor.
    • cw newword <esc> (lower case c w) changes an oldword to newword.
      Use <esc> to finish newword.
    • c$ newtext <esc> (lower case c dollar sign) changes oldtext until the end of line to newtext.
      End with <esc>

    Copying and placing lines

    • yy (lower case y y) copies the cursor's line into a buffer
    • 9yy (nine lower case y y) copies 9 lines into the buffer
    • p (lower case p) pastes the copied lines below the cursor.
    • P (CAPITAL P) pastes the copied lines above the cursor.

    Moving lines

    • Similar to copying lines, but instead of using yy to copy them, use dd to delete them.
      Move the cursor directly to where you want to copy the lines to.
      Use p or P to put the the lines below or above the cursor.

    Joining lines

    • J (CAPITAL J) joins cursor line to the next one.

    Other useful vi commands

    • . (period) repeats the last command.
    • u (lower case u) undoes the last command.
    • U (CAPITAL U) undoes all changes to a line.
    • :1,$s/oldword/newword/g
      (colon one comma dollar sign lower case s slash oldword slash newword slash lower case g)
      changes every occurence of oldword for newword.
    • :%s/oldword//
      (colon percent sign lower case s slash oldword slash slash)
      deletes every occurence of oldword.

      Warning: If you mash the arrow keys before you use the <esc> key to stop adding text, then you will add a bunch of garbage lines.
      Solution: Press the <esc> key and delete the garbage.

    Discover Life | Education | Training Guides | Building Web Pages | vi Commands