The vi editor is a modal editor. This means that in command mode, the keys on the keyboard have special meanings; these meanings control movement throughout the file, searching, saving, text replacement, and the various ways to change to insert mode. In insert mode, the keys produce their letters text is being entered.
When the editor is invoked, you are in command mode by default. If you are
in insert mode and wish to change to command mode, hit ESC (the escape
key).
Most commands in vi do not require the user to press the ENTER key.
Those that do require the user to press ENTER all begin with
a colon (:). Thus, the command cc does not require the
user to press ENTER; whereas to execute the command :f,
the user is actually required to press :, then f,
then ENTER.
Control keys are generally denoted by, for example, ^D;
the letter is actually lower case, however. To enter a control character,
simultaneously
hold down the lower case version of the letter and the control key (generally
labeled Ctrl).
To save text without quitting the editor, type :w. To quit the editor
without saving, type :q!. To quit the editor and save your changes,
type :wq.
vi allows you to move around in the file several ways. You can move around
a screen at a time, one-half screen at a time, a line at a time, a character
at at time, a word at a time, etc. You may also move to specific lines, to
the beginning of the file, or to the end of the file. You may move either
forwards or backwards.
h | move left one character (left arrow also works) |
j | move down one line (down arrow also works) |
k | move up one line (up arrow also works) |
l | move right one character (right arrow also works) |
w | move right one word |
b | move left one word |
^F | move down one screen |
^B | move up one screen |
^D | move down one-half screen |
^U | move up one-half screen |
G | move to the end of the file |
:1 | move to the beginning of the file |
:n | move to line n of the file |
0 | move to the beginning of the current line |
$ | move to the end of the current line |
vi allows the user to change existing text by characters, words, or lines.
rc | replace the character under the cursor by c |
c<SPACE> | replace the character under
the cursor with text; terminate
with ESC |
cw | replace the remainder of the word under the cursor with
text; terminate with ESC |
cc | replace the line under the cursor with text;
terminate with ESC |
R | overwrite all characters under the cursor;
terminate with ESC |
x | delete the character under the cursor |
X | delete the character to the left of the cursor |
dw | delete the remainder of the word under the cursor |
dd | delete the entire line under the cursor |
D | delete from under the cursor through the end of the line |
J | delete the line feed and carriage return characters at the end of the line; that is, join this line to the next to create a single line |
vi allows commands to be repeated or undone. Commands may either be
repeated in the sense of do the last command again, or they may be
given a repetition factor, for example, do this command 6 times.
u | undo the last command |
. | repeat the last command |
nC | repeat command C n times |
c3w change three words beginning with the one under the cursor
3dd delete three lines beginning with the one under the cursor
3x delete three characters
vi allows text to be moved or copied. Moving a block of text means deleting
it from its present position and inserting it elsewhere; copying a block of
text means duplicating it elsewhere.
yy | yank the line under the cursor into the buffer; do not delete the line |
nyy | yank n lines into the buffer,
beginning with the one under the cursor |
p | put the contents of the buffer below the line the cursor is on (the yank command in conjunction with this command copies text) |
P | put the contents of the buffer above the line the cursor is on (the yank command in conjunction with this command copies text) |
dd | delete the line under the cursor and copy it into
the buffer (this is the same as the dd
command above); the p command will restore
the deleted lines following the position of the cursor,
wherever it has been put (this sequence moves text) |
vi allows the user to enter insert mode in a variety of ways. Lines can be
opened below or above the cursor; text may be appended; text may be inserted.
o | open a new line below the current line; go to insert mode |
O | open a new line above the current line; go to insert mode |
i | move to insert mode; characters are typed beginning under the cursor |
a | move to insert mode; characters are typed beginning after the cursor |
vi allows the user to search for text, and optionally to replace found text
with other text.
fc | find the first occurrence of character c
on the current line |
fnc | find the nth occurrence
of character c on the current line |
/text | search forward in the file for text |
?text | search backward in the file for text |
:%s/text/new/g | replace text with
new throughout the file |
vi allows part of the current file to be written to another file, and for
the contents of another file to be read into the current file.
:w filename | write the contents of the current
file to the file filename |
:n,m w filename | write lines n through
m of the current file
to the file filename |
:r filename | read the contents of filename
into the current file beginning at the line
immediately below the cursor |
:f | gives the name and current line number of the file |
^G | gives the name and current line number of the file |
:! | executes <command>
at the shell level (this is useful for
listing the contents of a directory, for
example, without leaving vi) |
vi allows the user to edit multiple files, and to move selected lines from
one file to another, without all without leaving the editor. It also allows
the user to mark text for easier deletion or moving.
:e filename | edit the file filename
without exiting vi |
:n | if vi was invoked with a list of file names, begin editing the next file in the list |
m<letter> | mark the current line with the
name <letter> |
y'<letter> | yank from the current line through
the line marked <letter> |
d'<letter> | delete from the current line through
the line marked <letter> |
"<letter>yy | yank the current line into the buffer
named <letter> |
"<letter>dd | delete the current line into the buffer
named <letter> |
"<letter>p | put the contents of the buffer named
<letter> below the cursor |
"<letter>P | put the contents of the buffer named
<letter> above the cursor |
"<letter1>y'<letter2> | yank from the current position
through the marked location
<letter2> into
the buffer named
<letter1> |
"<letter1>d'<letter2> | delete from the current position
through the marked location
<letter2> into
the buffer named
<letter1> |
Yanking (or deleting) into a named buffer and then putting the contents of the named buffer is the only way to copy (or move) selected lines of one file into another file. An example is:
ma (mark the current location with a)
"zy'a (yank through location a into buffer z)
:e filename (without leaving the editor, edit the file filename
filename)
"zp