[rfc791.ORG]
Bill Joy, inventor of the vi editor and the C Shell (csh), and co-designer of the the SPARC microprocessor and the Java programming language, among other things.

[ rfc791.ORG : Unix Help : The Vi Editor ]

vi - full-screen text editor
Vi was written by Bill Joy, a grad student at the University of California at Berkeley in 1977. It is one of the first full-screen editors. For those of you who are familiar with other editors, vi takes a bit of getting used to. Despite its antiquity and obscurity, however, vi is worth having a basic understanding of for it is the one editor that comes standard on every UNIX installation. Even modern-day Linux distributions all come with at least some form of vi workalike (vim, elvis, nvi, etc). Learning vi allows you to sit down at a terminal of any unix system, and be able to jump right into editing a file. And after you get used to it, it's actually pretty efficient.

The simplest way to start vi and open a file you would like to edit is by typing the following:
vi <filename> 
for example:
vi rc.conf
Here are some other startup options:

vi +216 <filename>
Start editing at line 216 of filename. This is quite handy when your compiler tells you you have an error on line 216.
vi +/tropospheric <filename>
Start editing at the first position in filename containing string tropospheric (This works with regular expressions too).
vi -r <filename>
Recover file from a vi backup. This allows you to continue editing a file in the case that the editor was killed by a signal. So, if you were in the middle of editing a file when the system reboots, you can probably resume editing this way,
vi file1 file2
Edit multiple files. This will open up "file1" first, and when you are done with file1, you type
:n
to move to "file2". If you want to save file1, you'd instead enter
:wn
The main reason for doing all this instead of editing the files seperately is that you can yank and put between files this way. This will all make sense as you read the section on buffers.
There are three modes in vi:
  1. Command Mode.
    Every keystroke is interpreted as part of a command.
  2. Input mode.
    Keystrokes are interpreted as text input.
  3. EX mode.
    This mode allows you to perform functions that were availiable in vi's predecessor, ex.
vi starts in command mode. To change into input mode you must press one of the editing mode keys (shown below). To change from input mode back to command mode simply press the ESC key.

Always make sure you are out of input mode by pressing ESC before attempting to use command mode keys. If you are already in command mode and press ESC, vi will simply beep at you. Vi will not be looking for arrow keys when you are in input mode, so if you use them, your file is likely to get a little garbage in it. Some vi clones are smart enough to handle this, but I recommend you try to avoid using arrow keys in input mode, as some will not, and you'll forget you are even in input mode and start trying to do other commands. Regular arrow keys can be used in place of the h, j, k, and l keys in command mode if your terminal is working properly. So, for those of you using Microsoft's badly broken telnet client shipped in Windows XP and 2000, for example, you need to use h, j, k, and l.

Getting the hang of the h, j, k, and l keys is handy for situations where your arrow keys aren't available. I recommend playing the game "hunt" for an hour or so with another friend who is trying to learn vi. Hunt is available on many unix systems in the /usr/games directory, and the motion keys in hunt are the same as vi. The man page for hunt(6) is fairly amusing also.

Movement Commands

j
moves the cursor down one line
k
moves the cursor up one line
h
moves the cursor left one column
l
moves the cursor right one column
CTRL-f
moves the cursor down one screen
CTRL-b
moves the cursor up one screen
G
moves the cursor to the end of the doc.
nG
moves the cursor to line n
w
moves the cursor forward one word
b
moves the cursor back one word
^
moves the cursor to start of the line
$
moves the cursor to end of the line


Mode switching commands

i
change to input mode (characters are inserted at current position)
a
change to input mode (characters are inserted AFTER current position)
I
change to input mode (characters are added at the beginning of the current line)
A
change to input mode (characters are added at the end of the current line)
R
change to input mode (replaces or overwrites old text)
r
change to input mode (overwrites only the current character)
C
change to input mode (rest of the current line is replaced by new text)
o
change to input mode (after current line a new line is added for text)
O
change to input mode (before current line a new line is added for text)
cw
change to input mode (the rest of the current word is replaced and put into the default buffer)


Buffer commands

The following commands involve buffers, which are similar to what other operating systems call the clipboard. But this editor from 1977 goes one step further than those other systems and has 27 buffers! You can copy (called "yanking" in vi parlance), paste (called "putting"), or cut (which is called "deleting") into and out of any one of these 27 buffers at any time. If you're happy with having just one buffer, vi provides a "default buffer" which requires a few less keystrokes to use. Keep in mind, though - if you delete, you will overwrite a buffer!

x
deletes the current character and puts it into the default buffer
dd
deletes the current line and puts it into the default buffer
dw
deletes the rest of the current word and puts it into the default buffer
yy
yank current line into the default buffer
p
put text in the default buffer after the current cursor position
P
put text in the default buffer before the current cursor position
Now, we can take any one of those commans and prefix it with a double-quote and a lowercase letter and we've chosen a buffer besides the default to operate on. Try this, it's way handy!

"ayy
Yank the current line into buffer "a"
"xy3w
Yank the three words following the cursor into buffer "x"
"fp
put text in buffer "f" after the current cursor position


Other editing commands

u
undoes the last command
J
appends the next line to the end of the current line
~ (tilde)
Swaps case of one character then moves the cursor ahead one character.
.
repeats the last vi command
:
changes to ex mode (: is the first character of the command, see below)

These commands can be modified in different ways to do different things. You can add counts and motion commands to editing commands:

4b
move four words to the left
y2l
yanks two characters from the right of the cursor into the default buffer
y2w
yanks two words from the right of the cursor into the default buffer
d4b
deletes four words from the left of the cursor
4yy
yanks four lines into the default buffer
4dd
deletes four lines
c4w
replace four words after cursor with what you type next
cc 
replace current line with what you type next



Vital ex mode commands
:q
quits vi without saving changes
:w <filename>
saves in <filename>
:x
saves changed file and quits (x! may be required to save and quit read only files)
:e <filename>
edits <filename>. If filename is not specified and the command is followed by a bang (!), the current file is reloaded from disk (aborting changes).
:E <filename>
opens <filename> in a new window. You can switch to the new window and back with CTRL+W, and yank and put operations work between windows. This appears to only work in the nvi clone of vi (the only official replacement for the original CSRG vi). If you're not using nvi and want to yank text from one file and put it in another, there is another way, opening two files from the command line, which is explained at the top of this page.
:file
Tells you the file name and line number, among other things.
:nu
Tells you the line number and its contents.
:s/text1/text2/
Substitutes text2 for text1 in the current line the first time it occurs.
:s/text1/text2/g
Substitutes text2 for text1 in the current line every time it occurs.
:17,26s/text1/text2/g
Substitutes text2 for text1 in every line from line 17 through 26 every time it occurs.
:%s/text1/text2/g
Substitutes text2 for text1 in every line every time it occurs.
:%s/text1/text2/gc
Substitutes text2 for text1 in every line every time it occurs, after prompting you to be sure for each substitution.
All of the EX mode substitute commands can take regular expressions.