As the heading suggests, in this section I attempt to present a subset of the vi commands large enough to enable you to accomplish basic tasks, but small enough that a beginner can remember most of them.
You open a file by typing "vi
You close a file by getting into command mode (use the
You insert new text by getting into edit mode. There are a lot of ways to get into edit mode. An incomplete list is presented next:
i Insert new text at current cursor position.
A Append new text to the end of the current line.
Delete text by getting into command mode (press
x Delete a character.
dw Delete a word.
dd Delete a line.
Get into command mode by pressing
h Left one space
j Down one line
k Up one line
l Right one space
The undo key is "u". Get into command mode (press
1. Getting into command mode (press
2. Press the forward slash "/". A forward slash character should appear in the lower left corner of your screen.
3. Typing the string you wish to search for.
vi can be invoked from the command line as follows:
vi -r file Recover a buffer saved when vi terminates.
vi +n file Open file on line n.
vi + file Open a file at the last line of file.
vi +/pattern file Open file at the first instance of pattern.
vi file file... Open multiple files.
Once inside vi, to get to the next file, type :n from command mode.
view file Open file in read-only mode.
The following commands will either get you out of vi, write any changes you may have made, or both:
:w file Write to the specified file.
:w! file Write to specified file, regardless of file permissions.
:wq Write and quit.
:wq! Write and quit, regardless of access mode.
, ZZ Quit and save any changes.
:q Quit.
:q! Quit without saving changes.
Q Quit vi and start ex.