Rob's web

vi

vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by (and thus standardized by) the Single Unix Specification and POSIX.

In newer distros vi start vim (Vi IMproved).

Interface

vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or normal mode (where keystrokes are interpreted as commands that control the edit session). For example, typing i while in normal mode switches the editor to insert mode, but typing i again at this point places an "i" character in the document. From insert mode, pressing the escape key switches the editor back to normal mode. A perceived advantage of vi's separation of text entry and command modes is that both text editing and command operations can be performed without requiring the removal of the user's hands from the home row. As non-modal editors usually have to reserve all keys with letters and symbols for the printing of characters, any special commands for actions other than adding text to the buffer must be assigned to keys which do not produce characters, such as function keys, or combinations of modifier keys such as Ctrl, and Alt with regular keys. Vi has the property that most ordinary keys are connected to some kind of command for positioning, altering text, searching and so forth, either singly or in key combinations. Many commands can be touch typed without the use of Shift, Ctrl or Alt. Other types of editors generally require the user to move their hands from the home row when touch typing:

For instance, replacing a word is cwreplacement textEsc which is a combination of two independent commands (change and word-motion) together with a transition into and out of insert mode. Text between the cursor position and the end of the word is overwritten by the replacement text. The operation can be repeated at some other location by typing ., the effect being that the word starting that location will be replaced with the same replacement text.

Synopsis

vi(m) [options] [file ..]

Options

file ..         A list of filenames. The first one will be the current file and read into the buffer. The cursor will be positioned on the first line of
                the buffer. You can get to the other files with the ":next" command. To edit a file that starts with a dash, precede the filelist with
                "--".
-               The file to edit is read from stdin. Commands are read from stderr, which should be a tty.
-t {tag}        The file to edit and the initial cursor position depends on a "tag", a sort of goto label. {tag} is looked up in the tags file, the
                associated file becomes the current file and the associated command is executed. Mostly this is used for C programs, in which case {tag}
                could be a function name. The effect is that the file containing that function becomes the  current  file  and  the cursor is positioned
                on the start of the function. See ":help tag-commands".
-q [errorfile]  Start in quickFix mode. The file [errorfile] is read and the first error is displayed. If [errorfile] is omitted, the filename is
                obtained from the "errorfile" option (defaults to "AztecC.Err" for the Amiga, "errors.err" on other systems). Further errors can be
                jumped to with the ":cn" command. See ":help quickfix".

Command mode

Navigation
jMove down a line.
kMove up a line.
lMoves the cursor one space to the right.
hMoves the cursor one character to the left.
wMoves one word forward.
bMoves one word backwards.
0Move to the start of the current line.
$Move to the start of the current line.
Ctrl+fMoves forward one screen.
Ctrl+bMoves back one screen.
Ctrl+dMoves forward half a screen.
Ctrl+vMoves back half a screen.
number GJumps to line number.
GJumps to the last line in the file.
ggJumps to the first line in the file.
vSelect.
/Find.
Input modes
iText is added before the cursor.
aText is added after the cursor.
IThe text you type will be added to the start of the current line.
AAdds text to the end of the current line.
oAdds text to a new line below the current line.
OAdds text to a new line above the current line.
cwReplaces the current word with whatever you type.
RReplacing characters from the cursor's current position.
CReplaces characters in the current line until you stop typing.
ccDeletes the current line entirely and replaces it with what you type.
rIf you want to change a single character, r makes the change without actually putting you into insert mode.
EscReturn from insert mode to command mode.
Deleting and putting in buffer.
xDelete a single character without leaving command mode. To delete a set number of characters starting with the cursor, use number of characters x.
DRemoves the rest of the line to the right of the cursor.
dwRemoves the end of the current word.
ddRemoves the entire current line.
uShould you find that you have made a mistake, you can take advantage of Vim's undo feature by pressing u. Vim has a large Undo history, so if necessary, you can regress a long way. Of course, if you really have trouble, you might have to exit without saving any of your work.
Copying
yyCopy current line into buffer.
Pasting
pPaste text in buffer after cursor position.
PPaste text in buffer before cursor position.

Ex mode

:helpOnline help.
:q!Quit without saving.
:w [file]Write to file [file].
:xSave changes and quit.