vi Survival Guide
THREE STATES
| State |
How to Get There |
| Command mode (where keys perform commands) |
Hit <ESC> |
Insert mode (where you can type information) |
Hit 'i' or 'a' |
| ed command-line mode (enter ed commands after the :)
| Hit <ESC>:
|
STARTING vi
Run vi <filename> to edit a new or existing file.
MOVING THE CURSOR
The following commands require that you hit <ESC> to enter command mode (unless you are already there...). Commands prefaced by : assume you are in command-line mode.
| Keypress(es) |
Command |
| h |
Left |
| l |
Right |
| k |
Up |
| j |
Down |
| $ |
End of line |
| ^ |
Beginning of line |
| G |
End of document |
| <n>G |
To the nth line from top of document |
| :<n> |
To the nth line from the top of the document |
| /<string> |
To <string> |
UNDOING CHANGES
| Keypress(es) |
Command |
| u |
Undo the latest change. |
| U |
Undo all changes on a line, while not having moved off it (unfortunately). |
| :q! |
Quit vi without writing. |
| :e! |
Re-edit a messed-up file. |
ENTERING TEXT
When in command mode, enter either a or i to enter append or insert mode to begin entering text.
| Keypress(es) |
Command |
| a |
Append |
| i |
Insert |
MISCELLANEOUS
| Keypress(es) |
Command |
| J |
Join current line with next line |
| /fubar |
Find next occurance of 'fubar' |
| n |
Repeat last command one time |
| CTRL-g |
Display current line number |
| ^L |
Refresh the screen (sometimes `^P' or `^R') |
| :set showmode |
Will display INPUT MODE |
DELETING TEXT
| Keypress(es) |
Command |
| x |
Delete the character under and after the cursor |
| <n>x |
Deletes <n> characters and after under the cursor |
| X |
Delete the character before the cursor |
| <n>X |
Deletes <n> characters before the cursor |
| dd |
Delete one line |
| <n>dd |
Delete <n> lines |
| D |
Delete the rest of the line |
| p |
Will paste the deleted text into current line |
SAVING WORK AND EXITING
| Keypress(es) |
Command |
| :q |
Quit vi, unless the buffer has been changed |
| :q! |
Quit vi without writing |
| ^Z |
Suspend vi |
| :w |
Write the file |
| :w <name> |
Write to the file <name> |
| :w >> <name> |
Append the buffer to the file <name> |
| :w! >> <name> |
Overwrite the file <name> |
| :x,y w >> <name> |
Write lines x through y to the file <name> |
| :wq |
Write the file and quit vi; some versions quit even if the write was unsuccessful! Recommend use of ZZ instead. |
| ZZ |
Write if the buffer has been changed, and quit vi. If you have invoked vi with the `-r' option, you'd better write the file explicitly (`w' or `w!'), or quit the editor explicitly (`q!') if you don't want to overwrite the file. |
| :e |
Edit another file without quitting vi - text can be copied from one file toanother this way |
SHELL COMMANDS
| Keypress(es) |
Command |
| :sh |
Execute a subshell, back to vi by `^D' |
| :r |
Read the file into the buffer |