Cover by Linda Lamb, Elbert Hannah, Arnold Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Opening and Closing Files

You can use vi to edit any text file. vi copies the file to be edited into a buffer (an area temporarily set aside in memory), displays the buffer (though you can see only one screenful at a time), and lets you add, delete, and change text. When you save your edits, vi copies the edited buffer back into a permanent file, replacing the old file of the same name. Remember that you are always working on acopy of your file in the buffer, and that your edits will not affect your original file until you save the buffer. Saving your edits is also called “writing the buffer,” or more commonly, “writing your file.”

Opening a File

vi is the Unix command that invokes the vi editor for an existing file or for a brand new file. The syntax for the vi command is:

$vi [filename]

The brackets shown on the above command line indicate that the filename is optional. The brackets should not be typed. The $ is the Unix prompt. If the filename is omitted, vi will open an unnamed buffer. You can assign the name when you write the buffer into a file. For right now, though, let’s stick to naming the file on the command line.

A filename must be unique inside its directory. A filename can include any 8-bit character except a slash (/), which is reserved as the separator between files and directories in a pathname, and ASCII NUL, the character with all zero bits. You can even include spaces in a filename by typing a backslash (\) before the space. In practice, though, filenames generally consist of any combination of uppercase and lowercase letters, numbers, and the characters dot (.) and underscore (_). Remember that Unix is case-sensitive: lowercase letters are distinct from uppercase letters. Also remember that you must press ENTER to tell Unix that you are finished issuing your command.

When you want to open a new file in a directory, give a new filename with the vi command. For example, if you want to open a new file called practice in the current directory, you would enter:

$vi practice

Since this is a new file, the buffer is empty and the screen appears as follows:

~
~
~
"practice" [New file]

The tildes (~) down the lefthand column of the screen indicate that there is no text in the file, not even blank lines. The prompt line (also called the status line) at the bottom of the screen echoes the name and status of the file.

You can also edit any existing text file in a directory by specifying its filename. Suppose that there is a Unix file with the pathname /home/john/letter. If you are already in the /home/john directory, use the relative pathname. For example:

$vi letter

brings a copy of the file letter to the screen.

If you are in another directory, give the full pathname to begin editing:

$vi /home/john/letter

Problems Opening Files

  • When you invoke vi , the message [open mode] appears.

    Your terminal type is probably incorrectly identified. Quit the editing session immediately 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 adequate terminal type setting.

  • You see one of the following messages:

    Visual needs addressable cursor or upline capability
    Bad termcap entry
    Termcap entry too longterminal:  Unknown terminal type
    Block device required
    Not a typewriter

    Your terminal type is either undefined, or there’s 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 you’re 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.

  • One of the following messages appears:

    Bad file number
    Block special file
    Character special file
    Directory
    Executable
    Non-ascii filefile non-ASCII

    The file you’ve 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, everything 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.

Let’s 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.

KeystrokesResults
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.

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 haven’t made any edits; and type :wq to both save your edits and quit. (:wq is equivalent to ZZ.) We’ll explain fully how to use ex commands in Chapter 5; for now, you should just memorize a few commands for writing and saving files.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required