[an error occurred while processing this directive]

A vi Reference


Starting vi

The UNIX program vi can be used to create and enter text into new files, or to modify the text in existing files, or just to browse through the contents of a file. To start a vi editing session, give the following UNIX command.
vi filename
where filename is the name of the file that you want to create, modify, or browse.

If there is a file named filename then vi will begin by displaying a portion of the file. Otherwise, a file named filename is created by vi. If a new file is created then vi begins by displaying a screen full of lines that begin with a tilde (~). These lines are not part of the contents of the file. They are displayed by vi to show you where the end of the file is.

You can also start up vi without giving a file name on the command line. Then you will see a screen full of lines that begin with a tilde (~). You will also see this if you started vi with an incorrect filename. You can then change to the file you want by giving the following command after vi starts up.

:e filename
where filename is the name of the file that you want to create, modify, or browse.

After you have started a vi session, you will see a character (possible a blank) that is either underlined or highlighted. This indicates a position in the text where you can add characters or make changes. It is called the cursor.

Using vi: an overview

vi runs in two modes: command mode and insert mode. When you start vi, it is in command mode. In command mode, characters that you type are interpreted as commands to the vi program. In insert mode, characters that you type are entered into the text or replace characters in the text.

It is important to know which mode you are in. If you see characters that you type entering into the file then you are in insert mode. You can always get to command mode by hitting the escape key (Esc). It will not hurt to hit the escape key in command mode. The only thing that will happen is that the terminal will beep.

vi can be used either for browsing files or for editing (creating and modifying) files. For browsing a file you only need to run vi in command mode. In command mode, you use scrolling or searching commands to position a view window in the file. You can also use file manipulation commands to change to a different file or to quit.

For editing files, you use browsing skills to locate places where changes are needed. In addition, you need the ability to move the cursor to a particular character, where you can delete text or give a command to start insert mode. And finally, you need to be able to save the changes that you have made.

vi commands

There are two different kinds of vi commands. One kind of command begins with a colon(:), or a slash(/), or a question mark(?). These commands are used for file operations (changing files, saving changes, and quitting), executing UNIX commands, and searching for text. You will need to hit the return or enter key to complete these commands.

The other kind of command is a one or two character combination. These commands are used for starting insert mode, scrolling, deleting text, moving the cursor, and undoing inserts and deletes. You do not need to hit the return or enter key after typing these commands. These commands, except the undo command, can be preceded by an integer that specifies the number of times the command should be repeated. For example,

Scrolling through a file

The vi scrolling commands are useful if you just want to read text sequentially. These commands are all control characters. You will not need to hit the return or enter key after typing the command. The notation ctrl-letter means hold down the control key while pressing the letter key.

Searching for text

The / and ? commands are used for locating text patterns in the text. The n command searches for the pattern specified in the previous / or ? command, and it searches in the same direction. You will need to hit the return or enter key to complete the / or ? commands, but not the n command.

The pattern can contain any string of characters, but some ($, ^, *, ., /, [, and ]) have special meanings. These characters should be preceded by a backslash if you need them in the pattern.

Each time you use a / or ? command, it establishes a search direction for future searches. If you want to search mainly in that direction, but occasionally go in the other direction, you can use n and N for search continuation.

The N command does not change the established search direction. Later n commands will search in the direction as before. You will need to hit the return or enter key to complete the / or ? commands, but not the N command.

Moving the cursor

The following commands are sufficient while you are getting accustomed to vi. You do not need to hit the return or enter key for any of these commands. After you are more comfortable with the commands listed above, you will probably want to learn some other commands for moving the cursor. The following commands are useful. Many UNIX compilers report errors by giving a short message about the error and telling you the line number where it occurred. vi simplifies finding the offending line with the following commands:

Inserting text

In insert mode, most characters that you type are entered into the text in front of the cursor. As you type characters, the cursor moves to the right so that they appear in the order that you typed them. When you are in insert mode, you can add as many lines of text as you want. From command mode, you can start up insert mode with one of the following commands. The i command is needed to add text to the beginning of a line. The a command is needed to add text to the end of a line. The O command is needed to add text at the beginning of the file. The o command is needed to add text at the end of the file.

When you are through with any insert mode,

Deleting text

After you have positioned the cursor you can When you first learning vi, this is enough to know. Later, you will probably want to learn how to delete larger text objects.

Undoing inserts and deletes

Everyone makes mistakes. If you have erroneously inserted or deleted text, you can

File manipulations

Many of the file manipulation commands are colon commands. You will need to hit the enter key to complete these. While you are editing with vi, changes that you make do not immediately affect the file that you are editing. vi is actually just making changes in an internal copy of the file. You will need to use colon commands to write the internal copy to the file. You will need to hit the enter key to complete these commands. If you created a new file without giving a file name on the command line, you will need to supply a name before terminating the program.

What to do after accidental changes

When browsing a file, you might accidentally enter insert mode. If you do, you can return to command mode by hitting the escape (Esc) key.

While you are using vi, changes that you make do not immediately affect the file that you are working with. vi is actually just making changes in an internal copy of the file. But when you have made changes, vi will not let you quit or switch to another file unless you either write the internal copy to the file or give an insistent version of a command. A colon command can be made insistent by adding an exclamation point (!).

What to do when vi freezes up

There is an undocumented vi mode in which vi does not respond to any commands and does not enter typed characters into the text. You may accidently get into this mode if you forget to start an insert mode before trying to enter text. If this happens to you, you can usually get back to normal command mode by giving the command :v.

Using auto-indent

For writing code in various programming languages, indentation is an important part of style. vi can be set up for automatic indenting. If auto-indent is enabled and vi is in insert mode, then when you start a new line it has the same amount of indentation as the previous line. If you want to use auto-indent you need to get into command mode (hit the escape key) and Since this is a colon command, you will need to hit the return key to terminate the command. The sw=4 tells vi that each level of indenting is 4 spaces. You can also configure vi so that it always starts up with auto-indent enabled.

When auto-indent is enabled, you can control indentation by typing one of the following characters. These characters should only be typed at the beginning of a new line while in insert mode.

You can also indent or outdent existing lines while in command mode with the following commands. These commands may be preceded by an integer to indent or outdent multiple lines. Most good C programming styles will indent code between braces and place opening braces at the end of a line. One good style places closing braces at the beginning of a line. If you observe this style and use vi auto-indent mode then there are two character sequences that soon become second nature to you:

Executing UNIX commands

Programmers spend a lot of time in a seemingly endless loop: edit the program file, compile it, and run it to check it out. To make this loop a little more tolerable, you can do it all within a vi session. When you are in a vi session, you can issue UNIX commands from command mode. where command is any UNIX cammand and can include command-line arguments. The output from command appears on the screen as usual. When command terminates, you will get a prompt from vi to resume editing.

If you are issuing a command to compile the file that you are editing, be sure to save the file before issuing the command.

Configuring vi

vi can be configured with various features, such as auto-indent, automatic line wrapping, and displaying matching parentheses, brackets, and braces. To do this, first make sure you are in your home directory by giving the UNIX cd command. Then create and edit the file named .exrc and add the following line.
set ai sw=4 wm=5 sm
The .exrc file is read every time you run vi. If you put the above line into it, it sets the following mode switches.

Marking and returning to positions

After moving the cursor to a position, you can also mark that position and label it with a letter by giving the following command: where letter is the letter that you want to use to designate the position.

Later you can

where letter is the letter designates the position that you want to return to.

Text objects and commands

vi recognizes several kinds of text objects including words, sentences, and paragraphs. There are cursor move commands for each king of object. For example, vi recognizes two different kinds of word: a string of alphabetic characters or a string of non-blank characters. Lower case commands are used for the first kind of word and upper-case commands are used for the second kind: You will probably find the upper-case word move commands most useful for programming since the moves skip over punctuation, parentheses, brackets, and braces.

vi also recognizes sentences, defined by periods followed by spaces. You can

The sentence moves are useful for editing ordinary text, but are not very useful for programming since periods are not common in programs.

Finally, vi has a simple concept of a paragraph. In vi, paragraphs are terminated by blank lines. You can

When you write programs with several procedures or functions these commands can be very useful for moving long distances in the code. With most programming styles, blank lines are used mainly between functions and between function headers and local variable declarations. So these commands can be used to step from one function to another.

In addition to using objects to specify cursor moves, there are various delete operations that can be done with objects. Whenever text is deleted, vi saves the deleted text in a storage area called a buffer. vi also has yank operations that copy a text object into the buffer without deleting it.

[an error occurred while processing this directive]