There are three modes in vi.

 

        1. Command Mode -> normal and initial state, other modes return to this state.

        2. Input Mode -> once in this mode, you can type freely.

        3. Line Mode -> waiting for input after certain commands.

 

When you open a file using vi (i.e. "vi file.c"), you will start out in command mode. To start typing, you have to type any one of the following keys:

 

        i -> insert -- allows you to start typing where the cursor is currently. When you first open the file, the cursor will be in the upper left most corner.

        a -> append -- instead of typing where the cursor is currently, it moves over one character then allows you to start typing.  This is _very_ useful when you want to add more characters to the end of a line.

        o -> open -- insert a new line, then go into insert mode.

 

After typing the things you need to type, you'll want to save the file (or not save the file).  In order to do that, you need to be in command mode.  To get out of input mode and in to command mode, all you have to do is hit the escape (ESC) key.

 

The following key combinations will help you get out of vi (and do some other stuff).  When you see a ":", that actually means type a colon.  Typing a ":" puts you into line mode.  When you press the "enter" key and after whatever you told it to do, it will revert back to command mode (unless you quit the application).

 

        :w   -> write -- saves the file.

        :wq  -> write then quit -- saves the file the quits vi.

        :q   -> quit -- quit vi without saving the file.

        :w!  -> force a write -- if you're the owner a file, but only have read permission on the file, this will bypass that and actually save the file.

        :wq! -> force a write then quit -- same as above, then quits vi.

        :q!  -> force a quit -- quit without saving the file.  If you just type ":q", and you've made changes, you'll get an error saying that you've changed the file and you must either write it or use ":q!".

        :e!  -> re-edit the file -- dump any changes since the last save and re-edit the current file.

 

Next, moving the cursor around in a file.  All of these commands are to be entered in command mode. Vi newbies beware - you can use the h, j, k, and l keys to move around, but it takes a little getting used to!

 

        (arrow keys) -> move you around in the direction that they point.

        h -> left -- move the cursor left (like left arrow).

        j -> down -- move the cursor down (like down arrow).

        k -> up -- move the cursor up (like up arrow).

        l -> right -- move the cursor right (like right arrow).

        (number)G -> goto -- that's right, a number followed directly by the letter "G".  That takes you to the specified line number.  If you just type "G", you'll go to the end of the file.  To go to the beginning of the file, type "1G".

        0 -> beginning of line -- takes you to the beginning of the current line.

        $ -> end of line -- takes you to the end of the current line.

 

Cut, Copy, and Paste.  These commands are really powerful!  When in command mode, you type any of the following key combinations.

 

        yy -> copy (yank) -- copies the current line.

        y(number)y -> Copy (yank) -- copies the (number) lines.

        dd -> cut (delete) -- cuts the current line.

        d(number)d -> Cut (delete) -- cuts the (number) lines.

        dw -> delete word -- cuts the word pointed to by the cursor.

        x -> delete character -- deletes the character pointed to by the cursor.

        D -> delete to the end of line -- cuts everything from the cursor to the end of the line.

        p -> paste (or put) -- pastes what is in the "clipboard".  When used after a yy or a dd, it inserts the copied lines after the line where the cursor is.  When used with "dw" or "D", it pastes the characters starting where the cursor is pointing.

 

Here is a list of other commands that should be executed in command mode.

 

        . -> repeat last command -- it repeats whatever you just did.  If you typed "dd" to delete 1 line, it will execute another "dd".

        u -> undo -- does just that, undoes your _last_ command.  I'm almost positive that this is a bug in the original vi, but it is fixed in most vi clones (i.e. vim).  In vim, you can undo multiple commands.

 

 

PC Drew (drewpc@colorado.edu)

More Fundamentals

 

The next few commands are commands to change/edit portions of a file.

        cw -> change word -- this basically executes a dw (delete word), then an i (insert).

        r(char)  -> replace -- this replaces the character under the cursor with the specified character.

 

Movement commands.

        0 -> beginning of the current line.

        $ -> end of the current line.

        b -> beginning -- moves to the beginning of the current word.  If the cursor is already at the beginning of the current word, the cursor then moves to the beginning of the previous word.

        w -> ? -- moves to the beginning of the next word.  Like the previous command, but moves forward instead of backward.

        e -> end -- moves to the end of the current word.  If the cursor is already at the end of the current word, the cursor then moves to the end of the next word.

 

Search commands.

        /(characters) -> this searches the file, from the current line, for the characters specified.  "/hi" would search for "hi" within the file.  If there are more than one instances of the search text found, you can use the "n" character to go from one instance to the next.

        ?(characters) -> same as above, only searches above the cursor.  For example, if the cursor is in the middle of the file, this will search from the middle to the top in a backward manner.

 

Display commands.

        ^f -> page forward (it's actually ctrl-f).

        ^b -> page backward (it's actually ctrl-b).

 

Other commands.

        ~ -> changes the case of the character under the cursor.  If the cursor is on a "p", then it get's changed to a "P".  And if it's on a "P", it get's changed to a "p".  Characters other than a-z and A-Z are not affected.

        ^v(char) -> this interprets the following character as a literal character instead of as its vi special meaning.  This is similar to using the "\" in UNIX.  For instance, to put the escape key in to a file, you'd type "^v" and you'd get this:

 

Advanced Features

 

Some of the most powerful commands are in line mode (the mode after typing ":"). The first is the substitute function.  An example of this is:

 

        :.,$s/hi/lo/g

 

it's really quite logical when you dissect it:

 

        ".,$" -> the line numbers to apply the following command.  The "." refers to the current line (i.e. "cd ." goes to the current dir).  The "$" refers to the end of the file.  Therefore, ".,$" means "from the current line to the end of the file...".  You can also specify line numbers like "1,5" or any combination of variables and numbers like "1,$".

        "s"   -> substitute (search and replace)

        "/hi" -> it searches for the text after the first "/" (in this case, "hi").  If another "/" is not present, then it just removes the text in the range specified.  If there is a "/" after the text, see the next field.  Remember, if you really want to search for a "/", escape it in the normal UNIX manner: "\/".

        "/lo" -> replacement text.  Once an instance of the search text is found, it gets replaced with this text.

                

        "/g"  -> This means do the search/replace for every instance in a line.  So, if you're trying to replace "hi" with "lo" and there's 3 instances of "hi" in a line, adding the "/g" will search/replace all 3.  If you leave this off, then it'll do it to the first "hi" it comes to.

 

Another line mode function is the delete function.  This command is similar to the above

substitute command, but it deletes lines.  It looks like this:

 

        :.,$d

 

This is exactly the same as above, so I won't go into what everything means.  The only difference is the "d" instead of an "s".  This specifies "delete" the lines within ".,$" instead of substitute.

 

        * As far as I know, all of the commands that I've explained in this and the previous

        HOWTO work in the traditional vi as well as vim.  Vim, however, has a whole lot more

        features.  To see them all, go to http://www.vim.org/html/ .  It also has syntax

        highlighting for tons of languages.  The syntax highlighting is one of the best

        reasons to use vim over vi.

 

        * The majority of these commands can be combined with numbers to do multiple times.

        For instance, if you type "8~", then the next 8 characters will have their cases

        changed.  The same for "c4w".  This changes the next 4 words.

 

 

 

The most common commands in vi

:q quit
:q! quit without changing
:w write
:wq write and quit
:n edit next file

i Add in front of the cursor
I Add in front of the line
a Add behind cursor
A Add the end of the line
o Add next line
o Add previous line

^D scroll down
^U scroll up
^F scroll screen forward
^B scroll screen back
^L clear screen and put text back
^E scroll line up
^Y scroll line down

 

 

M scroll cursor to middle of the screen
h cursor one position left
l cursor one position right
k cursor one position up
j cursor one position down
0 cursor to begin of line
- cursor to begin last line

x delete character
X delete character left of cursor
dd delete current line

cw change word
cc change line

 

:se ff=unix change text file format to unix.

 

 

 

 

Reformatted & edited by Arron Cusimano (arron-c@lycos.com).