$ vi /home/john/letter
Opening and Closing Files | 7
Problems Opening Files
When you invoke vi , the message [open mode] appears.
Your terminal type is probably incorrectly identified. Quit the editing session im-
mediately by typing :q. Check the environment variable $TERM. It should be set to
the name of your terminal. Or ask your system administrator to provide an ade-
quate terminal type setting.
You see one of the following messages:
Visual needs addressable cursor or upline capability
Bad termcap entry
Termcap entry too long
terminal : Unknown terminal type
Block device required
Not a typewriter
Your terminal type is either undefined, or theres probably something wrong with
your terminfo or termcap entry. Enter :q to quit. Check your $TERM environment
variable, or ask your system administrator to select a terminal type for your
environment.
A [new file] message appears when you think a file already exists.
Check that you have used correct case in the filename (Unix filenames are case-
sensitive). If you have, then you are probably in the wrong directory. Enter :q to
quit. Then check to see that you are in the correct directory for that file (enter
pwd at the Unix prompt). If you are in the right directory, check the list of files in
the directory (with ls) to see whether the file exists under a slightly different name.
You invoke vi , but you get a colon prompt (indicating that youre in ex line-editing
mode).
You probably typed an interrupt before vi could draw the screen. Enter vi by typing
vi at the ex prompt (:).
One of the following messages appears:
[Read only]
File is read only
Permission denied
Read only means that you can only look at the file; you cannot save any changes
you make. You may have invoked vi in view mode (with view or vi -R), or you do
not have write permission for the file. See the section Problems Saving Files on
page 10.
One of the following messages appears:
Bad file number
Block special file
Character special file
Directory
Executable
8 | Chapter 1:The vi Text Editor
Non-ascii file
file non-ASCII
The file youve called up to edit is not a regular text file. Type :q! to quit, then
check the file you wish to edit, perhaps with the file command.
When you type :q because of one of the previously mentioned difficulties, this message
appears:
No write since last change (:quit! overrides).
You have modified the file without realizing it. Type :q! to leave vi. Your changes
from this session will not be saved in the file.
Modus Operandi
As mentioned earlier, the concept of the current mode is fundamental to the way
vi works. There are two modes, command mode and insert mode . You start out in
command mode, where every keystroke represents a command. In insert mode, every-
thing you type becomes text in your file.
Sometimes, you can accidentally enter insert mode, or conversely, leave insert mode
accidentally. In either case, what you type will likely affect your files in ways you did
not intend.
Press the ESC key to force vi to enter command mode. If you are already in command
mode, vi will beep at you when you press the ESC key. (Command mode is thus
sometimes referred to as beep mode.)
Once you are safely in command mode, you can proceed to repair any accidental
changes, and then continue editing your text.
Saving and Quitting a File
You can quit working on a file at any time, save your edits, and return to the Unix
prompt. The vi command to quit and save edits is ZZ. Note that ZZ is capitalized.
Lets assume that you do create a file called practice to practice vi commands, and
that you type in six lines of text. To save the file, first check that you are in command
mode by pressing ESC , and then enter ZZ.
Keystrokes
Results
ZZ
"practice" [New file] 6 lines, 320 characters
Give the write and save command, ZZ. Your file is saved as a regular Unix file.
ls
ch01 ch02 practice
Listing the files in the directory shows the new file practice that you created.
Opening and Closing Files | 9
You can also save your edits with ex commands. Type :w to save (write) your file but
not quit vi; type :q to quit if you havent made any edits; and type :wq to both save
your edits and quit. (:wq is equivalent to ZZ.) Well explain fully how to use ex com-
mands in Chapter 5; for now, you should just memorize a few commands for writing
and saving files.
Quitting Without Saving Edits
When you are first learning vi, especially if you are an intrepid experimenter, there are
two other ex commands that are handy for getting out of any mess that you might create.
What if you want to wipe out all of the edits you have made in a session and then return
to the original file? The command:
:e! ENTER
returns you to the last saved version of the file, so you can start over.
Suppose, however, that you want to wipe out your edits and then just quit vi? The
command:
:q! ENTER
quits the file youre editing and returns you to the Unix prompt. With both of these