The vi text editor uses two main modes: command mode and insert mode. The first part of this tutorial focuses on navigating a file, which is done in command mode. When you're in command mode, normal keypresses are used to execute commands rather than to create text. When you enter insert mode, the keyboard is used to enter text, for instance, on the command line. To exit command mode, press the Esc key.
Commands in vi can be either single keypresses, combined keypresses using Shift or Ctrl, or key sequences. Any time a capital letter is referenced for a command, you should use the Shift key combined with that letter. Any time a command is referenced that uses two letters or symbols, you press the keys in sequence rather than simultaneously.
To begin, you'll create a blank file by opening vi from the command line followed by the name of your new file. In this tutorial, you make a document from scratch in vi and edit the document to learn useful vi commands. By the end of the tutorial, you'll have learned all the major vi commands that are used in day-to-day editing, as well as some powerful commands that you can leverage when the time is right. Throughout this tutorial, please feel free to use the reference sheet from Cal Tech in the Resources section.
Log in to your favorite UNIX-like operating system and open vi with a new file (see Figure 1).
Figure 1. Open vi with a new file
vi opens with a new blank file called tutorial.txt (see Figure 2). You'll immediately notice something odd: The far left column of the text editor is filled with tilde characters. Don't worry -- that's how vi illustrates undefined portions of the document. In other words, these lines don't exist yet, because the file has no content.
Figure 2. A blank file in vi
Before you do anything else, you should learn how to save a file and how to exit vi. These types of commands are entered by pressing the colon key followed by a sequence of letters describing your desired action. To save your new file, press the colon key, press the w key, and then press the Enter key. To quit vi, press the colon key, the q key, and the Enter key. Now, open vi again on the command line. If you try to exit vi without saving your changes, it warns you to press the exclamation point key to confirm your action. Any time you want to override a system warning, append your key sequence with an exclamation point; the key sequence to quit is:
key, q key, ! key, Enter key
can combine commands, such as
wq, to save the file and then exit vi.
The first thing you need to do is insert some text so that you can learn to edit and manipulate your file. By default, you start in command mode. So, if you just start typing, you might be surprised by the results. Each mode is described in detail later; for now, press the i key to enter insert mode, and then type a few lines of text as you would in a normal text editor. When you're finished, press the Esc key to get back into command mode. See Figures 3 and 4.
Figure 3. When you enter insert mode, the bottom line of the editor indicates you have done so
Figure 4. Type these lines in your example document so that you have text to work with
After you press the Esc key, the cursor remains on the last character typed, and you're back in command mode. The next section teaches you how to navigate the file.