Shy vi, the Princess of Text Editors
The vi text editor can claim a unique status among UNIX editors: Almost every UNIX system in the universe has vi . This fact makes it a good editor to know if you plan to be moving around from system to system, because you can always count on it being there. Someone may have other reasons for using vi , but ease of use is not foremost among them.To run vi , type vi, a space, and the name of the file you want to edit, and then press Enter.Tip If you get an error message when you try to run vi , talk to your system administrator. If the screen looks weird, your terminal type may not be set right — another reason to talk to your system administrator.
Editor la mode
The most distinctive feature of vi (and the one that has spawned legions of vi -haters, along with a few devotees) is that it is a modal editor. The vi program is always waiting for one of two things: commands or text (also known as input). When vi is waiting for a command, it is in command mode. When it is waiting for text, it is in input mode. Normally, it is up to you to figure out which mode vi is in at any particular moment — it doesn’t give you a clue.Most vi commands are one letter long. Some are lowercase letters, and others are uppercase letters. When you type vi commands, be sure to use the correct capitalization.If you are in input mode and want to give a command, press the Esc key.
Tip Emergency exit from vi
To escape from vi , follow these steps:
Press Escape at least three times.
The computer should beep. Now you are in command mode, for sure.
Type the following line and press Enter:
:q!
This line tells vi to quit and not save any changes.
Tip Whenever we tell you to type a command, it works only if you are in command mode. If you are not sure which mode you’re in, press Esc first. If you are already in command mode, pressing Esc just makes vi beep.To switch from command mode to input mode, you tell vi to add the text after the character the cursor (the point at which you are working) is on (by using the a command) or to insert the text before the current cursor position (by using the i command).
Help! I need somebody!
The guy who wrote vi (remember Bill, the grouchy guy who’s 6'4" and in excellent physical condition? — same guy) didn’t believe in help, so there wasn’t any.Tip Fortunately, vi has been used in so many introductory computing courses that Bill eventually relented and added "novice" mode. Rather than type vi to run the editor, type vedit to get the same editor with some allegedly helpful messages. In particular, whenever you’re in input mode rather than command mode, vi displays, at the bottom of your screen, a message such as INPUT MODE , APPEND MODE , CHANGE MODE , or OPEN MODE . All these messages mean the same thing (except to Bill, evidently): Text you type when these messages are visible is added to the file rather than interpreted as commands.
Easy text-entry techniques
Make a new file with some deathless prose so that you can practice entering text in vi . Run vi with a new filename:
vi madeline
To add text after the current position of the cursor, type the letter a (you do not press Enter after a command):We tell you in a minute how to move the cursor, when you have some text to move around in. You can type a, for example, to add this text to the newly created xanadu file: In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.
To get back to command mode, press Esc. Press Esc whenever you finish typing text so that you are ready to give the next command.
Other commands you can use to enter text include i to insert text before the current cursor position, A (that’s an uppercase A, which to vi is totally unrelated to a lowercase a) to add the text at the end of the line the cursor is on, and O to add the text on a new line before the current line.The vi program shows you a full-screen view of your file. If the file isn’t long enough to fill the screen, vi shows tildes (~) on the blank lines beyond the end of the file. Figure 10-1, for example, shows a text file called eating.peas (created in a later discussion about ed ) as it appears in vi .

Figure 10-1: Tildes fill up the blank lines on the vi screen.The cursor appears at the beginning of the first line of the file.
All kinds of ways to move the cursor
You can use dozens of commands to move the cursor around in your file, but you can get to where you want with just a few of them:
The arrow keys Tip Sadly, on some terminals vi does not understand the arrow keys. If this statement is true for you, press h to move left, j to move down, k to move up, and l to move right. Bill chose these keys on the theory that, because those keys are a touch typist’s home position for the fingers on the right hand, you can save valuable milliseconds by not having to move your fingers. Really. In some versions of vi , the arrow keys work only in command mode; in other versions, they also work in input mode.
Enter or + moves the cursor to the beginning of the next line.
The hyphen ( - ) moves the cursor to the beginning of the preceding line.
G (the uppercase letter) moves the cursor to the end of the file.
1G moves the cursor to the beginning of the file. (That’s the number 1, not the letter l. Why ask why?)
Giving your text a makeover
To modify the text you typed, follow these steps:
Move the cursor to the beginning of the text you want to change.
To type over (on top of) the existing text, press the R key.
Type the new text. What you type replaces what is already there. Press Esc when you finish replacing text.
To insert text in front of the current cursor position, press the i key.
Type the new text. What you type is inserted without replacing any existing text. Press Esc when you finish inserting text.
Removing unsightly text
To delete text, follow these steps:
Move the cursor to the beginning of the text you want to delete.
To delete one character, type x. To get rid of five characters, type xxxxx. You get the idea.
To delete text from the current cursor position to the end of the line, type an uppercase D.
To delete the entire line the cursor is on, type dd (the letter d twice).
Nobody undoes it better
Like many text editors, vi has a way to undo the most recent change or deletion you made. Type the letter u to undo the change. If you type an uppercase U, vi undoes all changes to the current line since you moved the cursor to that line.
Write me or save me — just don’t lose me
To save the updated file, type the following (be sure that you press Esc first so that you’re in command mode):
:w
That’s a colon and a w, and then press Enter. You should give this command every few minutes, in case the confusing nature of vi commands makes you delete something important by mistake.
Good-bye, vi
To leave vi , type
ZZ
Be sure to press Esc a few times so that you are in command mode before giving this command. To quit and not save the changes you have made, type this line:
:q!
Then press Enter. This line means, “Leave vi and throw away my changes. I know what I’m doing." Warning Most other letters, numbers, and symbols are also vi commands, so watch what you type when you are in command mode. Table 10-1 lists the most common commands you use with vi .
Command | Description |
---|---|
Esc | Return to command mode |
Enter | Move to beginning of next line |
+ | Move to beginning of next line |
- | Move to beginning of preceding line |
a | Add text after cursor |
A | Add text at end of current line |
dd | Delete entire current line |
D | Delete from cursor to end of line |
G | Move (go) to end of file |
1G | Move to beginning of file |
h | Move one space left |
i | Add text before cursor |
j | Move down one line |
k | Move up one line |
l | Move right one space |
O | Add text on new line before current line |
:q! (followed by Enter) | Quit vi , even if changes aren’t saved |
R | Replace text |
u | Undo last change |
U | Undo changes to current line |
x | Delete one character |
:w (followed by Enter) | Save (write) file |
ZZ | Quit vi and save changes |