[an error occurred while processing this directive]
vi
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
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
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.
vi
: an overviewvi
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
commandsvi
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,
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.
/
and ?
commands are used for locating text
patterns in the text.
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.
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.
vi
.
You do not need to hit the return or enter key for any of these commands.
vi
simplifies finding the offending line with the following
commands:
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,
vi
, this is enough to know.
Later, you will probably want to learn how to delete larger
text objects.
vi
session with :q
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.
vi
session 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
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 (!).
vi
session after unwanted changes with :q!
vi
freezes upvi
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
.
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
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.
vi
auto-indent mode then
there are two character sequences that soon become second nature to you:
vi
session.
When you are in a vi
session, you can issue UNIX commands
from command mode.
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.
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
.exrc
file is read every time you run vi
.
If you put the above line into it, it sets the following mode switches.
vi
to use auto-indent for insertions.
vi
to use 4 spaces for each level of indentation in
auto-indent mode.
vi
to wrap a line 5 spaces from the right margin.
If you start a word that ends within 5 spaces of the right margin
vi
will place the word on a new line.
vi
to 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
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:
vi
also recognizes sentences, defined by periods followed by
spaces.
You can
Finally, vi
has a simple concept of a paragraph.
In vi
, paragraphs are terminated by blank lines.
You can
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]