Short Introduction to the Vi Editor
Vi is the most commonly available screen editor for UNIX. It's the only one you can count on being installed on almost every system.
This document covers enough of the basic commands to get you started. Like most programs, Vi has a large number of fairly obscure commands, but you'll find yourself using only 4 or 5 most of the time. The rest you can look up when you need them.
To start, type vi filename at the command line, and
you'll see a screen like this:
I typed vi dog.txt to create a new file. It works the same way with an existing file.
The most important thing to know about Vi (and the most confusing) is that it has two modes, Command Mode and Insert Mode.
In Command Mode, you can invoke editing commands, move the cursor, save or exit the file, invoke the shell, or enter Insert Mode.
In Insert Mode, you can insert new text.
By default, vi starts in Command Mode.Insert Mode
Pretty straightforward--in Insert Mode, you insert text. Use the Backspace key to correct errors as you type.
Before you can do anything in Insert Mode, you have to get there, and there are several ways to do that:
- a append new text after cursor
- A append new text at end of line
- c begin change operation
- C change to end of line
- i insert new text before cursor
- I insert new text at beginning of line
- o open a new line below current line
- O open a new line above current line
- R begin overwriting text
- s substitute a character
- S substitute entire line
To get back to Command Mode, hit the Esc key.
Command mode is a little more complex, other than entering Insert Mode, you can Move, Delete, Search, Change and Save.
Commands can be applied to multiple objects. For example, dd deletes the current line. If I type 5dd, it deletes 5 lines.
- cw change word
- cc change line
- r replace character
- R replace text beginning at cursor
- dd delete current line
- D delete the remainder of the line
- dw delete word
- dG delete to the end of the file
- x delete current cursor position
- X delete back one character
Copying and Moving Text
- yy copy (yank) current line
- ye copy to end of word
- p paste yanked text (deleted text can also be pasted)
Cursor Movement in Command Mode
- h move one character left
- l move one character right (yes, letter l moves you right!)
- j move one character down
- k move one character up
- Backspace move back one character
- 0 (zero) move to the beginning of a line
- $ move to the end of a line
- Return first character of next line
- w forward by word
- b backward by word
- e end of word
- CTRL-f forward one screen
- CTRL-b backward one screen
- /text search forward for text
- ?text search backward for text
- n repeat previous search
- N repeat previous search in opposite direction
Undoing Changes and Recovery (see also section on Saving and Exiting)
- u undo last change
- U restore current line
Saving and Exiting
- ZZ quit vi and write the file if changes were made
- :w write file
- :w file save a copy to file
- :q quit file
- :q! quit file and discard edits
- :e! return to version of current file at time of last write (this command and the one previous are useful if you make a serious mistake)
Note that when you use a command that begins with a colon,
or you do a search with / or ?, the cursor jumps to a
command line as shown in the following screen:
After editing this document, I hit Esc to go back into Command Mode and then I typed :w
Now, when I hit Return, my changes will be written to dog.txt.
Tip: If you forget which mode you are in, hit Esc to make sure you are in Command Mode--You can always re-enter Insert Mode, and you won't mess up your typing.
More On Vi
You can edit multiple files, do global substitutions, write macros, escape to the shell, and more, but these are beyond the scope of this document.
They are a many tutorials on the web, and you can read the man page (type man vi at the shell prompt).
Useful books in the Yale Library collection include:email@example.com