Chapter 7. Advanced Editing
This chapter introduces you to some of the more advanced capabilities
of the vi and ex editors.
You should be reasonably familiar with the material presented in
the earlier chapters of this book before you start working with the
concepts presented in this chapter. This chapter is divided into five parts.
The first part discusses a number of ways to
set options that allow you to customize your editing environment.
You'll learn how to use the set command and how to create
a number of different editing environments using .exrc files. The second part discusses how you can execute UNIX commands from
within vi, and how you can use vi to filter text through
UNIX commands. The third part discusses various ways to save long sequences
of commands by reducing them to abbreviations, or even to commands
that use only one keystroke (this is called mapping keys).
It also includes a section on @-functions, which allow you to
store command sequences in a buffer. The fourth part discusses the use of ex scripts from
the UNIX command line or from within shell scripts.
Scripting provides a powerful way to make repetitive edits. The fifth part discusses some features of vi that are
especially useful to programmers. vi has options that
control line indentation and an option to display invisible characters
(specifically tabs and newlines).
There are search commands that are useful with program code blocks or
with C functions.
You have seen that vi operates differently on various terminals.
(For instance, on "dumb" terminals, vi inserts @ symbols
in place of deleted lines; on intelligent terminals, vi redraws
the screen with each edit.)
On modern UNIX systems,
vi gets operating instructions
about your terminal type
from the terminfo terminal database.
(On older systems, vi uses the
original termcap database.)[27]
There are also a number of options that you can set from within
vi that affect how it operates.
For example, you can set a right margin that will cause vi
to wrap lines automatically,
so you don't need to hit RETURN.
You can change options from within vi by using the ex command
:set.
In addition, whenever vi is started up, it reads a file in your
home directory called .exrc for further operating instructions.
By placing :set commands in this file,
you can modify the way vi acts whenever you use it.
You can also set up .exrc files in local directories to
initialize various options that you want to use in different
environments.
For example, you might define one set of options for
editing English text, but another set for editing source programs.
The .exrc file in your home directory will be executed first, then
the one in your current directory.
Finally, any commands stored in the shell variable EXINIT will
be executed by vi on startup.
The settings in
EXINIT take precedence over those in the
home directory
.exrc file.
7.1.1. The :set Command
There are two types of options that can be changed with the :set
command: toggle options, which are either on or off, and
options that take a numeric or string value (such as the location
of a margin or the name of a file).
Toggle options may be on or off by default.
To turn a toggle option on, the command is:
:set option
To turn a toggle option off, the command is:
:set nooption
For example, to specify that pattern searches should ignore case, type:
:set ic
If you want vi to return to being case-sensitive in
searches, give the command:
:set noic
Some options have a value assigned to them.
For example, the window option sets the number of lines shown
in the screen's "window."
You set values for these options with an equal sign (=):
:set window=20
During a vi session, you can check which options vi is
using.
The command:
:set all
displays the complete list of options, including options that you
have set and defaults that vi has "chosen."
The display should look something like this:[28]
autoindent nomodelines noshowmode
autoprint nonumber noslowopen
noautowrite nonovice tabstop=8
beautify nooptimize taglength=0
directory=/var/tmp paragraphs=IPLPPPQPP LIpplpipnpbp tags=tags /usr/lib/tags
noedcompatible prompt tagstack
errorbells noreadonly term=vt102
noexrc redraw noterse
flash remap timeout
hardtabs=8 report=3 ttytype=vt102
noignorecase scroll=11 warn
nolisp sections=NHSHH HUuhsh+c window=23
nolist shell=/bin/ksh wrapscan
magic shiftwidth=8 wrapmargin=0
nomesg showmatch nowriteany
You can find out the current value of any individual option by name,
using the command:
:set option?
The command:
:set
shows options that you have specifically changed, or set, either in
your .exrc file or during the current session.
For example, the display might look like this:
number sect=AhBhChDh window=20 wrapmargin=10
7.1.4. Some Useful Options
As you can see when you type :set all,
there are an awful lot of options that can be set.
Many of them are used internally by vi and aren't usually changed.
Others are important in certain cases,
but not in others (for example, noredraw and window can
be useful on a dialup line at a low baud rate).
The table in Section 3.1
contains a brief description of each option.
We recommend that you take
some time to play with setting options—if an option looks interesting,
try setting it (or unsetting it) and watch what happens while you edit.
You may find some surprisingly useful tools.
As discussed earlier in this book,
one option,
wrapmargin,
is essential for editing non-program
text. wrapmargin specifies the size of the right margin that
will be used to autowrap text as you type.
(This saves manually typing carriage returns.)
A typical value is 7 to 15:
:set wrapmargin=10
Three other options control how vi acts when conducting a search.
Normally, a search differentiates between uppercase and
lowercase (foo does not match Foo), wraps around to the beginning
of the file
(meaning that you can begin your search anywhere in the
file and still find all occurrences), and recognizes wildcard
characters when pattern matching.
The default settings that control
these options are noignorecase, wrapscan,
and magic, respectively.
To change any of these defaults, you would set the opposite toggle options:
ignorecase, nowrapscan, and nomagic.
Options that may be of particular interest to programmers include:
autoindent,
showmatch,
tabstop,
shiftwidth,
number, and
list,
as well as their opposite toggle options.
Finally, consider using the autowrite option.
When set, vi will automatically write out the
contents of a changed buffer when you issue the :n
(next) command to move to the next file to be edited, and before
running a shell command with :!.
 |  |  | 6.5. A Final Look at Pattern Matching |  | 7.2. Executing UNIX Commands |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|
|