Using the Unix Text Editor vi
R1172 May 2009
This document explains the basics of vi: opening and closing a file, moving around in a file, and elementary editing. The online tutorial vilearn is available on the ITS Login Service by typing vilearn at the Unix prompt (%).
NOTE: This document assumes you are using Unix on the ITS Login Service (login.itd.umich.edu). These instructions may also apply to vi on many other Unix machines.
TIP: If you are just beginning to learn Unix, you will probably find the Pico editor easier to use than vi. Pico is a full-screen text editor with a layout almost identical to the Pine e-mail program. For more information, see Using the Unix Text Editor Pico (R1168).
Table of Contents
The vi editor is available on almost all Unix systems. You can use vi from any type of connection to Unix because it does not depend on arrow keys and function keys: it uses the standard alphabetic keys for commands.
vi is short for visual editor; it displays a window into the file being edited that shows 24 lines of text. vi is a text editor, not a "what you see is what you get" word processor. vi lets you add, change, and delete text but does not provide formatting capabilities such as centering lines or indenting paragraphs.
Create a new file by typing vi newname at the Unix % prompt, where newname is the name you wish to give the new file.
On the screen you will see blank lines, each with a tilde at the left, and a line at the bottom giving the name and status of the file:
~ ~ "testvi" [New file]
Use vi to open an already existing file by typing vi filename where filename is the name of the existing file. If the file is not in your current directory, you must use the full pathname. See List Contents and Navigate Unix Directories (S4149) for more on pathname.
vi has two modes: command mode and insert mode. In command mode, the letters of the keyboard perform editing functions (like moving the cursor, deleting text, and so on). In insert mode, the letters you type form words and sentences. Unlike word processors, vi starts up in command mode.
To begin entering text in an empty file, you must change from command mode to insert mode. To do this, type the letter i. You may see INSERT MODE at the bottom right of the screen, or nothing may appear to change, but you are now in insert mode and can begin typing text. In general, you are not required to press the Return key to execute vi commands.
Type a few short lines and press Return at the end of each line. If you type a long line, you will notice that vi does not word wrap; it merely breaks the line unceremoniously at the edge of the screen. If you make a mistake, pressing the Backspace or Delete key may or may not remove the error, depending on the configuration of the software you are using to connect to the Login Service.
Moving the Cursor
To move the cursor to another position, you must be in command mode. If you have just finished typing text, you are still in insert mode. Go back to command mode by pressing the ESC key. (If INSERT MODE was displayed on your screen, it will disappear when you are in command mode.)
TIP: Depending on the software program you are using to connect to the Login Service or the options you have set, you may hear a beep or see the screen flash when you switch to command mode.
You may find that the arrow keys on your keyboard will move the cursor up, down, left, and right. If they do not appear to work, the cursor can be controlled with the four keys h, j, k, and l, as follows:
When you have gone as far as possible in one direction, the cursor stops moving and you may hear a beep or see the screen flash. For example, you can't use l to move right and wrap around to the next line; you must use j to move down a line. See the section Moving Around In a File for ways to move more quickly through a file.
Editing commands require that you be in command mode. Many of the editing commands have a different function depending on whether they are typed in upper or lower case. Often, editing commands can be preceded by a number to indicate a repetition of the command.
NOTE: Unless specified otherwise, the instructions in this section assume that you are in the command mode.
Deleting characters. To delete a character from a file, move the cursor until it is on the incorrect letter, then type x. The character under the cursor disappears. To remove four characters (the one under the cursor and the next three) type 4x. To delete the character before the cursor, type X (upper case).
Deleting words. To delete a word, move the cursor to the first letter of the word and type dw. The command deletes the word and the space following it. To delete three words, type 3dw.
Deleting lines. To delete a whole line, type dd. The cursor does not have to be at the beginning of the line. dd deletes the entire line containing the cursor and places the cursor at the start of the next line. 2dd deletes two lines.
To delete from the cursor position to the end of the line, type D (upper case).
Replacing characters. To replace one character with another, move the cursor to the character to be replaced. Type r, then the replacement character. The new character will appear, and you will still be in command mode.
Replacing words. To replace one word with another, move to the start of the incorrect word and type cw. The last letter of the word to be replaced will turn into a $. You are now in change mode and may type the replacement. The new text does not need to be the same length as the original. Press ESC to get back to command mode. 3cw allows you to replace three words.
Replacing lines. To change text from the cursor position to the end of the line, type C (upper case). Type the replacement text and press ESC. Press ESC again to get back to command mode.
Inserting text. To insert text in a line, position the cursor where the new text should go and type i. Enter the new text. The text is inserted before the cursor. Press ESC to get back to command mode.
Appending text. To add text to the end of a line, position the cursor on the last letter of the line and type a. Enter the new text. This adds text after the cursor. Press ESC to get back to command mode.
Opening a blank line. Type o (lower case) to insert, or open, a blank line below the current line. Type O (upper case) to insert a blank line above the current line. Press ESC to get back to command mode.
Joining lines. The command J (upper case) lets you join two lines together. Put the cursor on the first line to be joined and type J. 3J lets you join 3 lines together.
Undoing. Undo your most recent edit by typing u (lower case). U (upper case) undoes all the edits on a single line, as long as the cursor stays on that line. Once you move off a line, you can't use U to restore it.
Moving Around In a File
These shortcuts, which work in command mode, allow you to move more quickly through a file.
Moving by searching. To move quickly in a file, search for text. In command mode, type a / (slash) followed by the text to search for. Press Return. The cursor moves to the first occurrence of that text. Repeat the search in a forward direction by typing n (lower case), or in a backward direction by typing N (upper case).
Closing and Saving a File
With vi, you edit a copy of the file rather than the original file. The original is changed only when you save your edits.
To save the file and quit vi, type ZZ (upper case) in command mode.
The vi editor is built on an earlier Unix text editor called ex. ex commands can be used within vi. ex commands begin with a : (colon) and end with a Return. The command is displayed on the status line as you type. Some ex commands are useful when saving and closing files.
To save (write) the edits you have made, but leave vi running and your file open, press ESC, then type :w and press Return.
To quit vi, and discard any changes you have made since last saving, press ESC, then type :q! and press Return.
Displaying Line Numbers
To display the line numbers of the file being edited, type :set number and press Return. To cause vi to always display line numbers, add the following line to a file called .exrc in your home directory:
Visit ITS's Information System to obtain ITS computer documentation and other resources. A list of relevant documents follows:
The ITS Service Center provides a variety of computing help resources.
For further help with this or any other topic, call 734-764-HELP  or submit an online service request.