[an error occurred while processing this directive]
vican 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
viediting session, give the following UNIX command.
If there is a file named filename then
begin by displaying a portion of the file.
Otherwise, a file named filename is created by
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
You can also start up
vi without giving a file name on the
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
You can then change to the file you want by giving the following command
vi starts up.
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
It is called the cursor.
vi: an overview
viruns 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
viprogram. 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
You can always get to command mode by hitting the escape key
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
In command mode, you use scrolling or
searching commands to position a view window in
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.
vicommands. 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,
viscrolling 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-lettermeans hold down the control key while pressing the letter key.
?commands are used for locating text patterns in the text.
ncommand searches for the pattern specified in the previous
?command, and it searches in the same direction. You will need to hit the return or enter key to complete the
?commands, but not the
The pattern can contain any string of characters, but some
]) have special meanings.
These characters should be preceded by a backslash if you need them in the
Each time you use a
? 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 for search
Ncommand does not change the established search direction. Later
ncommands will search in the direction as before. You will need to hit the return or enter key to complete the
?commands, but not the
vi. You do not need to hit the return or enter key for any of these commands.
visimplifies finding the offending line with the following commands:
icommand is needed to add text to the beginning of a line. The
acommand is needed to add text to the end of a line. The
Ocommand is needed to add text at the beginning of the file. The
ocommand is needed to add text at the end of the file.
When you are through with any insert mode,
vi, this is enough to know. Later, you will probably want to learn how to delete larger text objects.
visession with :q
vi, changes that you make do not immediately affect the file that you are editing.
viis 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.
visession with :wq
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
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 (!).
visession after unwanted changes with :q!
vimode in which
vidoes 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
vican be set up for automatic indenting. If auto-indent is enabled and
viis 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
vithat each level of indenting is 4 spaces. You can also configure
viso 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.
viauto-indent mode then there are two character sequences that soon become second nature to you:
visession. When you are in a
visession, you can issue UNIX commands from command mode.
vito 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.
vican 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
cdcommand. Then create and edit the file named
.exrcand add the following line.
set ai sw=4 wm=5 sm
.exrcfile is read every time you run
vi. If you put the above line into it, it sets the following mode switches.
vito use auto-indent for insertions.
vito use 4 spaces for each level of indentation in auto-indent mode.
vito wrap a line 5 spaces from the right margin. If you start a word that ends within 5 spaces of the right margin
viwill place the word on a new line.
vito show matching parentheses, brackets, and braces in insert mode. When you type a closing parenthesis, bracket, or brace the cursor will briefly jump to the matching parentesis, bracket or brace.
Later you can
virecognizes several kinds of text objects including words, sentences, and paragraphs. There are cursor move commands for each king of object. For example,
virecognizes 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:
vi also recognizes sentences, defined by periods followed by
vi has a simple concept of a paragraph.
vi, paragraphs are terminated by blank lines.
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]