To start vi, simply type vi at the shell prompt.
To create a new file and give it a name or to load an existing file, type vi filename
vi displays the first screen of the file. If the file isn’t long enough to fill the screen, vi shows tildes (~) on the blank lines. The cursor is at the beginning of the file.
Vi is always in either command mode or input mode (also called insert mode that is also the default mode). In command mode, the letters you type are interpreted as commands. In input mode, what you type is entered in the file you’re editing.
To switch from input mode to command mode, press the ESC key. Press it a few times just to be sure. (vi just beeps when you press Esc when you’re already in command mode.)
To switch from command mode back to input mode use a or i command (described later in this section) to tell vi where to put the new text.
1. In input mode, press the Esc key to get into command mode.
2. From command mode, type:
· :q! to exit vi without saving the changes you made to the file.
· :w to save the current file without exit.
· :wq, same as :x, to save the current file and exit.
Command |
What It Does |
Esc |
Returns to command mode from input mode. In command mode, vi just beeps. |
Enter |
Move the cursor to the beginning of the next line. |
0 (zero) |
Move the cursor to the beginning of the current line. |
$ |
Move the cursor to the end of the current line. |
j |
Move the cursor down one line. |
k |
Move the cursor up one line. |
Ctrl + b |
page up. |
Ctrl + f |
page down. |
Ctrl + l |
Refresh the monitor. |
G |
go to the last line. |
a |
Adds text(that is, switches to input mode so that the text you type is added immediately after the cursor). |
A |
Adds text at the end of the current line, switching to input mode. |
i |
Inserts text (that is, switches to input mode so that the text you type is inserted immediately before the cursor). |
I |
Inserts text at the beginning of the current line, switching to input mode. |
o |
Inserts line. |
r |
Replace character in cursor. |
R |
(replace mode) |
D |
Delete line from cursor to the end of line |
yw |
copy |
p |
paste |
u |
Undoes the last change. |
U |
Undoes all changes made to the current line since you moved to it. |
/string |
search string down from cursor. |
?string |
search string up from cursor. ‘?’ or ‘n’ to re-find. |
? |
re-find. |
n |
repeat last search action. |
N |
repeat last search action in the opposite direction. |
. |
repeat last command in vi. |
:help |
help for vi. |
:wq |
write & save & quit. |
:x |
same to wq. |
:q! |
quit without save. |
:w |
save. |
:w filename |
save to other name. |
:set number |
show line number. |
:set nonumber |
not show line number. |
:set |
show option info. |
:g/oldtext/s//newtext/g |
Replaces oldtext with newtext to the whole file. |