October 02, 2001, 4:22 PM — The response to part 1 of our introduction to
vi (the first Unix101 column) was terrific. I'm glad that so many of
you liked the article. There were a lot of great suggestions -- some of
which are included in this article, and some of which I will
save up for when we revisit vi in a future column.
This article covers manipulating vi's default behavior and
cutting and pasting within as well as across files.
Customizing vi's behavior and learning to manage its buffers
can greatly improve its performance.
Vi's search function is a prime example of this. Searching is done
from command mode by typing a slash followed by the search text as in:
/Encyclopedia
This will search for the string Encyclopedia. Once an "Encyclopedia" is
found, typing an "n" will search for the next "Encyclopedia." Having
grown accustomed to PC editors that tend to default to a
case-independent search, vi annoyed me with the fact that /Encyclopedia
would find "Encyclopedia" but not "encyclopedia" or "ENCYCLOPEDIA." Vi
has a setting that will cause the search to ignore case. This is an ex
mode command, so from command mode you type it as a colon followed by
ic as in:
:set ic (and press RETURN)
Ic is shorthand for ignorecase. If you need to turn this off
for a case-dependent search, type:
:set noic (and press RETURN)
Another useful set option is showmode. If you tend to type
and then break for something (research or a fresh coffee), you might
walk away from the computer leaving vi in insert or edit mode.
:set showmode
will place a message in the last line of the screen on the
right indicating that you are in INSERT MODE, APPEND MODE, or
whatever.
This can be turned off with
:set noshowmode
To turn line numbering on and off use:
:set number
and
:set nonumber
Numbers are displayed at the beginning of
each line. These numbers do not appear in the document, simply on the
screen. They can be helpful if you are trying to compose something that
must fit in 55 lines (for a printed page).
Wrapping text
Setting the wrap margin will cause the text to auto wrap while you
are typing, but the command is quirky in a vi-ish sort of way. The
command is
:set wrapmargin=nn (where nn is a number)
The odd thing about this command is that it sets the amount of space to
the right that will not contain text. It would seem natural to set
wrapmargin=60 and assume that you would have 60 characters worth of
space to type in, but wrapmargin=60 sets a margin of 60, and you only
have 20 characters in which to write.
To set margins determine the width of text you want on the
screen and use the formula
:set wrapmargin=(80 - text width)
You can see all of the :set options that are available by
typing
:set all
A list of set options will be displayed that allow you to set auto
indenting, auto saving, word wrapping margins, how many lines to scroll
for CTRL-D and CTRL-U commands, and to set up the editor to use
defaults for certain programming languages.
You can issue set commands at the bottom of the screen while
you are editing, or you can set them up as defaults for your
personal vi use.
A file in your home directory named .exrc is a vi
initialization file that is loaded each time you run vi. You may have
one already, or you can create one using vi by changing to your home
directory and editing the file.
cd vi .exrc
The set commands (without the leading colon) can be set up in this
file, and all of the options listed in .exrc will be set
each time you run vi.
Listing 1 is an example of an .exrc file. This
.exrc file sets vi up to use case-independent searches, to
word wrap at 65 characters (80-15), to show INSERT MODE at the bottom
of the screen when in insert mode, and to show line numbering. It also
changes the default scrolling of 12 lines for CONTROL-D or CONTROL-U to
20 lines.













