To start vi:
Example: > vi letter will open a new file called letter to edit, or if letter already exits, open the exiting file.
Command |
Effect |
vi filename |
edit filename starting at line 1 |
vi +n filename |
edit filename beginning at line n |
vi +filename |
edit filename beginning at the last line |
vi -r filename |
recover filename after a system crash |
vi +/patter filename |
edit filename starting at the first line containing pattern |
Command Mode vs. Insert Mode
Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc.
When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in insert mode, enter command mode by hitting the escape, <esc>, key.
To insert text:
Command |
Insert Text |
i |
before cursor |
a |
after cursor |
A |
at the end of the line |
o |
open a line below the current line |
O |
open a line above the current line |
r |
replace the current character |
R |
replace characters until <ESC>, overwrite |
To move the cursor:
You must be in Command Mode to use commands that move the cursor. Each of these commands can be preceded with a Repeat Factor.
Examples:
8j will move the cursor down 8 lines
3w will move the cursor 3 words to the right.
Command |
Moves the cursor |
SPACE, l (el), or right arrow |
space to the right |
h or left arrow |
space to the left |
j or down arrow |
down one line |
k or up arrow |
up one line |
w |
word to the right |
b |
word to the left |
$ |
end of the line |
0 (zero) |
beginning of the line |
e |
end of the word to the right |
- |
beginning of previous line |
) |
end of the sentence |
( |
beginning of the sentence |
} |
end of paragraph |
{ |
beginning of paragraph |
To Delete Text:
The d command removes text from the Work Buffer. The amount removed depends on the Repeat Factor and the Unit of Measure you enter after d. If you delete by mistake: give the command u (undo) immediately after you give the delete command.
Examples: 3dd will delete 3 lines beginning with the current line.
3dw or d3w will delete 3 words
Command |
Action |
d0 |
delete to beginning of line |
dw |
delete to end of word |
d3w |
delete to end of third word |
db |
delete to beginning of word |
dW |
delete to end of blank delimited word |
dB |
delete to beginning of blank delimited word |
dd |
delete current line |
5dd |
delete 5 lines starting with the current line |
dL |
delete through the last line on the screen |
dH |
delete through the first line on the screen |
d) |
delete through the end of the sentence |
d( |
delete through the beginning of the sentence |
x |
delete the current character |
nx |
delete the number of characters specified by n. |
nX |
delete n characters before the current character |
Viewing Different Parts of the Work Buffer:
^Character means that you should hold down the Control key while striking the indicated character key.
Command |
Moves the cursor |
^D |
forward one-half screenful |
^U |
backward one-half screenful |
^F |
forward one screenful |
^B |
backward one screenful |
nG |
to line n (Ex: 25G moves the cursor to line #25) |
H |
to the top of the screen |
M |
to the middle of the screen |
L |
to the bottom of the screen |
^L |
refresh the screen |
Yanking (copy) and Putting (paste) Text:
Example: 3yy will yank (copy) 3 lines
p will put the 3 lines just yanked on the line below the current cursor.
In the following list M is a Unit of Measure that you can precede with a Repeat Factor, n.
Command |
Effect |
yM |
yank text specified by M |
y3w |
yank 3 words |
nyy |
yank n lines |
Y |
yank to the end of the line |
P |
put text above current line |
p |
put text below current line |
Changing Text
Example: cw allows you to change a word. The word may be replaced by as many word as needed. Stop the change by hitting < esc >.
c3w allows you to change 3 words.
Ending an Editing Session
Command |
Effect |
:w |
writes the contents of the work buffer to the file |
:q |
quit |
:q! |
quit without saving changes |
ZZ |
save and quit |
:wq |
save and quit |
:w filename |
saves to filename (allows you to change the name of the file) |
Miscellaneous commands
Command |
Effect |
J |
join the current line and the following line |
:set number |
number the lines on the screen (not actually added to file) |
:set nonumber |
turns off numbering of lines |
:r filename |
reads filename into the current file at the location of the cursor |
:set showmode |
displays INPUT MODE at the lower right hand corner of screen |
~ |
change uppercase to lowercase and vice-versa |
Searching for an expression
When in command mode if your enter / the cursor will go to the bottom of the screen. Follow this with an expression and vi will find the next occurrence of that expression in the file. Example: /Bill will find the next occurrence of Bill. If this is followed by the command: n, the next occurrence of Bill is found.