So - Michael's bluffers guide to "vi" version 1.0.a
If you sit down, digest this, and take note of everything in it, then you should be able to use vi just abut as well as anyone. It is not intended as a complete reference, it is just a reference that should be complete for most people's needs.
You start vi with: vi filename
vi has three modes - edit mode, insert mode and command mode. Commands in vi are case sensitive, so "h" is different from "H"
When you load vi, you will be in edit mode - You can move around the screen with the cursor keys, or, if they screw up (as they do over some links and on some terminals) you can use:
Also, if you put a number in front of the command, it will do it that number of times, so to go down 250 lines, try "250j"
To move around quickly:
To search for some text, use "/" - Searching is case sensitive so, to search for say, "Teapot" you would type:
/Teapot
If you were not sure of the case, and wanted to match "teapot"
as well, you could either do another search with "/teapot" or
use a regular expression:
/[Tt]eapot
Searching works from the current cursor position downwards, if
you want to repeat the last search, simply press "n".
Ok, so now we can move around, we may as well do something -
first, deleting.
To replace the current word with something else, use "cw" (change
word) type in the new word and hit ESCAPE (control-[ on some terminals)
to go back into Edit-Mode. So, if your cursor was at the start of
the word "tree" and you wanted to change it to "shop" type "cwshop
The rest of the commands in this section take us into Insert-Mode,
again, to exit from Insert-Mode to Edit-Mode, simply press Escape.
The easiest way to show these is to just give examples:
:set showmode
This is useful, it shows you at the bottom right hand corner
of the screen what mode you are in.
:q!
Quit from the current session, saving nothing.
:w hello.txt
Save the current file as "hello.txt".
:r /tmp/textfile.txt
Inserts the contents of the file "/tmp/textfile.txt" after
the current line.
:1,$s/michael/fred
Replaces the first occurence of the word "michael" with "fred"
in every line in the file.
:1,$s/[Mm]ichael/Fred
Replaces the first occurence of the word "Michael" or "michael"
with "Fred" in every line in the file.
:1,$s/[Mm]ichael/Fred/g
As above, but this will replace every occurence, even if there
is more than one on a line.
:1,$s/\/tmp\/teapot/\/tmp\/coffeepot/g
(Have I lost you yet?) - Replaces every occurence of
"/tmp/teapot" with "/tmp/coffeepot" throughout the file.
Basically the "\" before the "/"'s indicate that the
character following the "\" is a special character, and
shouldn't be interpreted as part of the command.
There you go - You are now a "vi" expert.
© 1997 Michael Lawrie
To replace the current character with something else, use "r"
so, if your cursor was over a "+" sign, and you wanted to change
it to a "-" sign, simply type "r-".
Cutting, copying and pasting is a bit more complicated, but you get
used to it eventually. Basically, you either delete a block of text
with "dd" (delete line(s)) or, yank it with "Y" and then paste it
back with "p" or "P" ... Sooooo...
Finally, a couple more useful ones:
Command mode:
To get into command mode, press ":" from edit mode. When you hit
RETURN at the end of the command, it will return you to edit mode.