-->
Previous | Table of Contents | Next |
You invoke vi from the command line by typing
vi
The screen clears and a column of tildes (~) appears in the leftmost column. You are now editing an empty, unnamed file. Whatever text you place in this file will exist in a buffer until you write the contents of the buffer to some named file. The tilde is vis way of telling you that the line where the tilde appears is empty of text.
vi can also be started with a file or a list of files to edit:vi filename1 filename2 filename3
Typically, you will probably edit only one file per vi session. If you are editing a list of files, vi edits each one in the sequence that they appear on the command line.
Alternatively, vi can be invoked from the command line as
vi +n filename
where n represents the line number where vi will place its cursor in filename. This is useful for programmers debugging large source code files who need to quickly jump to a known line containing an error.
Another example is useful in illustrating the vi editor. Enter
vi asong
at the command line and lets see what happens.
At the bottom of the screen in the left corner, you will see
asong 0 lines, 0 characters
The messages that display on this status line tell you what vi is doing or has just done. In this case, vi is telling you that it has opened an empty buffer whose contents are saved (whenever you do a save) to the file asong.
At this moment, you are in the command mode of vi. This is the major conceptual leap required in working with this editor. When editing text, you must remember whether you are in command mode or text mode. In command mode, any character sequences that you enter are interpreted as vi commands. In text mode, every character you enter is placed in the buffer and displayed as text onscreen.
Four commands are echoed at the bottom of the screen on the status line:
/ | Searches forward |
? | Searches backward |
: | An ex command (ex is a standalone line-based editor used within vi) |
! | Invokes a shell command |
Each of these types of status-line commands must be entered by pressing Return. This is not true for other types of vi commands, such as the ones that do insertions.
Tip:
To find out whether you are in command mode, use the set showmode preference described in the section, Setting Preferences, later in this chapter.
So, knowing that you are in command mode, lets insert some text. Basically, there are two commands for entering text on the current line: the letters i and a. These letters in lowercase insert (i) text to the left of the cursor or append (a) text to the right of the cursor. As with many vi commands, the uppercase versions of these letters have similar effects with subtle differences: uppercase I and A insert and append at the beginning and end of the current line, respectively, regardless of the cursor position.
After you type either of these letters, you are placed in input mode. Any text entered after this point is displayed onscreen.
Type an i and enter the following:
Down I walk<Enter> by the bay,<Enter> Where I can<Enter> hear the water.<Enter> Down we walk<Enter> by the bay,<Enter> My hand held<Enter> by my daughter.<Enter>
To exit from input mode, press Esc. Notice that the letter i does not display before you enter the text, meaning that the i was correctly interpreted as a command. Also, it is important to note that it is not necessary to press Return after pressing i for input mode.
Now that you have some text for your file, lets quit the editor to see the results. The commands used for saving the file and exiting vi are slightly different from the i and a commands used in editing text; you must precede the command with a colon (:).
In this case, you want to do a save and exit, which are actually combined in one command. Enter: and a colon appears at the bottom left of your screen. vi has recognized that you are about to enter an ex command, and it will echo the remaining characters of the command after the colon. Type wq and press Return. vi quickly informs you that it has written the file to disk and tells you how many lines it contains. vi exits, and you find yourself back at the shell prompt. Another way to save and exit is to type ZZ. The difference between this method and using wq is that ZZ writes the file to disk only if it has been modified since the last save.
If no changes have been made to the file you opened, you quit vi by simply typing :q. This does not work if the file has been modified. If you are sure that you dont want to save what you have done, enter :q!. This command forces vi to quit, regardless of any edits.
To make sure that vi saved the file asong correctly, use the cat command to quickly view the files contents:
% cat asong Down I walk by the bay, Where I can hear the water. Down we walk by the bay, My hand held by my daughter. %
Everything is exactly as you typed it in the file, so no surprises here.
Previous | Table of Contents | Next |