The vi editor is the visual interface to an older and even more basic editor called ex . ex displays only a single line at a time, because it was created for use on typewriter-like terminals. vi added full-screen capabilities for use on video display systems. Full screen means that vi takes up the entire Terminal window. vi uses the last line of the Terminal window, called the status line , to display information.
In order to keep your files neatly organized, start by creating a new directory in which to practice editing files (you might want to review Chapter 5, "Using Files and Directories").
To create a directory for use in practicing with vi:
This takes you to the Documents directory in your home directory.
This creates the new directory, called vi-practice .
This sets your current directory as
vi is a complete application, with dozens of options, features, and commands. In order to work with vi , though, you have to have a file, so here is a quick task to start vi , enter some text, save it, and quit from vi . Later in the chapter, we will go into the details of each step. (See also the sidebar " vi Improvedthe vim Editor.")
Learning vi with the vimtutor Program
Another resource for learning vi is the vimtutor program. This is a command-line program that gives you a short tutorial in using the vim editor. (See the sidebar below.)
The tutorial should take about 30 minutes to complete and is available in several languages. The default language is whatever your system default is, which is what you get if you just run vimtutor . You can specify the language with a two-letter code ( en for English, es for Espanol, fr for French, and so on). So for Spanish, you'd use vimtutor es .
vi Improvedthe vim Editor
The version of vi that comes with Mac OS X is actually an improved version called vim (short for vi improved ); since not all Unix systems have vim (but virtually all have vi ), we are sticking with the basic vi information in this book. Most distributions of Linux also provide vim .
vim includes multiple levels of undo, the ability to move the cursor with the arrow keys even in edit mode, and other features.
The vi command /usr/bin/vi is actually a symlink to /usr/bin/vim (see "About Links [the Unix Version of Aliases]" in Chapter 5), and when you run vi , the program is able to tell that it was invoked as vi instead of vim . But anywhere we tell you to use vi , you can type vim instead.
More information about vim is available at www.vim.org.
To create a file using vi:
path is the path to the file you want to edit and will typically be the name of a file in the current directory. For example:
This brings up the vi editor and fills your Terminal window (note that the name of the file appears in the status line at the bottom of the window) ( Figure 6.1 ).
Figure 6.1. This is what you see when you start up vi .
If the file does not already exist, it will be created when you first save from within vi . This is very similar to the procedure with word processors familiar to you, except that you are specifying the new filename when you start vi .
The highlighted rectangle at the top left of the window is the cursor , which is similar to the insertion bar you see in a regular Macintosh word processor, except that this type of cursor always sits on a character, not between two characters . (Your cursor might look different. You can select a different cursor in the Terminal application preferences; in this book we assume you are using the default block cursor .)
You also see a series of tilde ( ~ ) characters running down the left side. They are vi 's way of telling you that those lines are empty lines at the bottom of the window. In this case they almost fill the window because you haven't entered anything yet.
The next step is to switch to edit mode (also called insert mode because it lets you insert text) and enter some text:
Do not press .
That is just the lowercase letter i . (There are many ways to enter edit mode in vi we'll go into detail on them in the section "About vi 's Two Modes.")
The text -- INSERT -- appears in the status line. Not all versions of vi behave this way, so you cannot always rely on this indication.
Now that you are in edit mode, you can do the following:
Type in at least five lines of text just so you have enough to work with in the rest of the task. You must press at the end of each line (see the sidebar "Word Wrap in vi ").
Figure 6.2 shows a sample view after you have entered several lines of text.
Figure 6.2. Our shopping list entered into the file using vi .
Save the file. To do that, you must switch back to command mode, so press .
The text -- INSERT -- disappears from the status line.
There is no need to press after pressing .
If you are unsure whether you switched back to command mode, press again. Terminal will beep if you are already in command mode. There is no harm in pressing several times (that is, if you don't mind the beep).
Now you can save the file:
Notice how the cursor drops to the status line as soon as you type the colon character ( Figure 6.3 ).
Figure 6.3. When you type a colon in command mode, the cursor drops to the vi status line.
Also, the status line shows that you saved the file after you pressed . (Remember to press at the end of a line unless we tell you not to.)
You are back at the shell prompt, and you've created a file using vi .
Run the cat command to verify that the new file exists and contains what you typed into it ( Figure 6.4 ) .
Figure 6.4. Using the cat command to verify that the new file exists and contains what you typed into it.
localhost:~/Documents/vi-practice vanilla$ cat shopping-list 5 pounds of flour 1 dozen eggs A couple of bunches of fresh spinach 1 pound unsweetened chocolate 5 pounds sugar 4 pounds pasta 2 bunches garlic lots of tomatoes - roma if they have them cinnamon Earl Grey tea localhost:~/Documents/vi-practice vanilla$
Obviously, there is a lot more to vi than this example. Now that you have created a file, we will explore vi 's different modes, as well as how to move around inside a file, change and delete text, copy and paste, and search and replace.
If you are using a version of vi where
-- INSERT --
doesn't appear in the status line when you are in insert mode, then you might be able to turn it on by typing
from the command mode. An example would be in Mac OS X prior to 10.4.
The vi command can be started with or without specifying filenames on the command line.
The most common way to start vi is to give it one pathname as an argument. If the file already exists, it will be loaded into vi when you launch the editor. If the file does not exist, it will be created the first time you save from within vi . You can also start vi with no arguments, in which case you must supply a filename when you first save the file.
vi also allows you to enter multiple arguments and provides a way to edit each file in turn.
To start vi and edit or create a single file:
To start vi without specifying a filename:
To start vi with multiple filenames:
You can use command-line wildcards to open several files. For example,
opens all the files in the current directory whose names end in .txt.