The next couple of pages should get you ready to create and save text documents using the vi editor.... Lessons 1-4 are all you need for the very basics. I supply 5-9 to get you started with some very useful features.
vi) you are in COMMAND mode.
Any key you type will be interpreted as
a shortcut to a COMMAND. The i key is a shortcut for switching to
INSERT mode. The escape key will exit you out of
INSERT mode and
back into COMMAND mode. Once you are in the INSERT mode, you can
type and type and type. Please hit the return key once
you reach the
end of the line (especially if you are using vi to compose email).
Escape will get you back into the COMMAND mode. You need to be in
COMMAND mode to move your cursor (this may be a flashing or solid box
or underline, and it is a placemarker for where action will take place),
save the file to disk, delete lines, and all the other goodies available.
Again, i will get you back into INSERT
mode from COMMAND mode so you can
start typing and inserting text.
^[^[^Y if you press an arrow key while in INSERT mode).
You should learn the hjkl cursor keys (they are all next to
each other) because not all keyboards have arrow keys:
Other basic movement include:
key where the cursor will end up ---- -------- ^ beginning of the current line $ end of the current line G end of the file :# line number # of the file
key what it does ---- -------- x erases the character that the cursor is at r replaces the character that the cursor is at with the character you type next dd deletes the whole line that the cursor is at u undo: undoes the last thing you did (but pressing u again will undo the undo)
:w file writes the file "file" to disk. :w writes the file to disk (if "file" is already defined) :q quit vi :wq save some typing: same as :w and then :q
/ from the COMMAND mode,
you will again be taken to the
bottom of the screen. Here, type the text you want to find, then hit
return. If the text is in the document, your cursor will
move there.
Type n to find the next occurrence of the text you
are searching for.
The basic form is (g stands for global, s for search, and c for confirm):
:g/search-string/s//replace-string/gc
So if I wanted to replace all occurrences of COMMAND in a file with command, I would type
:g/COMMAND/s//command/gc
Vi will underline and found occurrence of "search-string" and wait for
you to press y or n
, to confirm or deny that specific change.
If you don't want this checking, omit that last c:
:g/search-string/s//replace-string/g
7dd will delete 7 lines.
5j will
move up five lines.
Control L (represented in text like this: ^L)
will replot the screen.
^G displays the file name, and how many lines it has, etc. O Opens a new line above the cursor, then sets INSERT mode o does the same, except below the cursor a append: sets INSERT mode, so you start typing after the character the cursor is at. i insert: sets INSERT mode, but anything you type is inserted before the character the cursor is at. :r file READs the contents of file "file" into the current document. If you omit "file", the current document will be read in again after the cursor, which can be very confusing. :e file EDIT another file "file", quitting the current document.
dd) it goes into the buffer.
When you delete more text, the newly
deleted text replaces the contents of the buffer (the old deleted text
goes away for good). You can delete 7 lines with 7dd
and then those
7 lines will be in the buffer.
yy YANKs text into the buffer without first deleting it from the text. 7yy will yank 7 lines of text into the buffer. p PUTs the contents of the buffer on a new line after the cursor's line P just like p, but puts it ABOVE the cursor's lineThe contents of the buffer change quite a bit, so you may want to use the move stable "named buffers":
"ayy YANKs text into a specially named buffer. The buffer's name is "a". Again, inserting a number before the command will yank that many lines. "ap PUTs text from buffer a 10"zdd deletes 10 lines from the text and yanks them into buffer z.
The book "Learning the vi Editor" is published by O'Reilly and Associates, and is available at most campus bookstores. It is a great source.
Novice users are frustrated by the "man pages" of UNIX systems, but there are tables of commands available by typing "man vi".
You can email comments to me at helpseries@arekdreyer.com.