vi is a somewhat arcane editor that is shipped with most variants of Unix.
It has been around for a long time, and therefore retains some aspects of a
line-based editor. vi is a screen-based editor, but functions in two modes:
command-based and edit-based. In command mode, you can move the cursor, issue
editing commands (see the table below), and exit the
editor. In edit-based mode, you can type text and move around within the text
you just entered. vi is a difficult editor to learn, but it is available on
almost every Unix platform you may encounter. For an interactive tutorial, type
VILEARN (upper case is necessary) at the sunny unix prompt.
Using the vi editor is easier if you first understand some of the
conventions used to enter and exit the command and editing modes. When you
start up vi, you are in command mode. To enter editing mode, you must use one
of the editing-mode keys, such as i for insert, a
for append, or o for a line insert. More on these commands can be
found in the table below. In command mode, the arrow
keys will usually let you navigate around the document. The h j k l
keys can be used in place of the arrow keys for moving left, up, down or right
respectively.
Most editing commands can be modified to perform the desired action a
multiple number of times by preceding the command with a number. For example,
8x will delete 8 characters rather than have to press x
8 times.
| Desired Function | Command |
|---|---|
| General Commands | |
| Start the vi editor (at the $ prompt) | vi [filename] |
| Write the editor contents to a file | :w
[filename] |
| Save the file and exit vi | ZZ or :wq |
| Exit vi without saving changes | :q! |
| Escape to the unix shell $ prompt | !/bin/sh
then exit at the $ prompt to return to the editor |
| Moving around in your file | |
| Move the cursor left, up, down or right | h
j k l or the arrow keys |
| Move to the beginning of the file | 1G |
| Move to the end of the file | G |
| Move to the beginning of the next word | w |
| Move to the beginning of the previous word | b |
| Move to the end of the current word | e |
| Move cursor to the end of the line | $ |
| Move to beginning of next line | [Return] |
| Scroll down one screen | [Ctrl]
f |
| Scroll up one screen | [Ctrl] b |
| Searching for text in your file | |
Find
[pattern] | /[pattern]/ |
Find next occurence of [pattern] | / |
Find prev
occurence of [pattern] | ? |
| Cutting, pasting and deleting text | |
| Delete a character at current cursor position | x |
| Delete a character after current cursor position | X |
| Paste a character at current cursor position | p |
| Paste a character after current cursor position | P |
| Enter insert mode at current cursor position | i enter text
[Esc] |
| Enter insert mode after current cursor position | a enter text [Esc] |
| Replace character at current cursor position | r replacement character |
| Replace multiple characters at current cursor position | R replacement text [Esc] |
| Delete a line of text | dd |
| Paste text | p |
| Copy a line of text | yy |
| Append another file at current cursor position | :r [filename] |
| Append next line of text to current line of text | J |
viqref