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
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.) vi gets operating instructions about your terminal type from a file called /etc/termcap . (In System V, termcap has been replaced with an alternate terminal database called terminfo .)
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 insert carriage returns.
You can change options from within
by using the
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.
There are two types of options that can be changed with the
To turn a toggle option off, the command is:
:set no option
If you want vi to return to being case-sensitive in searches, give the command:
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:
noautoindent nooptimize tabstop=8 autoprint open taglength=0 noautowrite prompt term=wy50 nobeautify noreadonly noterse directory=/tmp redraw timeout noedcompatible remap ttytype=wy50 noerrorbells report=5 warn hardtabs=8 scroll=11 window=20 noignorecase sections=AhBhChDh wrapscan nolisp shell=/bin/csh wrapmargin=10 nolist noshowmatch nowriteany magic noslowopen mesg paragraphs=IPLPPPQP LIpp1pipbb number tags=tags /usr/lib/tags
You can find out the current value of any individual option by name, using the command:
:set option ?
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
The .exrc file that controls your own vi environment is in your home directory (the directory you are in when you first log on). You can modify the .exrc file with the vi editor, just as you can any other text file.
If you don't yet have an
file, simply use
Enter into this file the
set nowrapscan wrapmargin=7 set sections=SeAhBhChDh nomesg map q :w^M:n^M map v dwElp ab ORA O'Reilly & Associates, Inc.
Since the file is actually read by ex before it enters visual mode ( vi ), commands in .exrc should not have a preceding colon.
In addition to reading the .exrc file in your home directory, vi will read a file called .exrc in the current directory. This allows you to set options that are appropriate to a particular project.
For example, you might want to have one set of options in a directory mainly used for programming:
set number lisp autoindent sw=4 terse set tags=/usr/lib/tags
and another set of options in a directory used for text editing:
set wrapmargin=15 ignorecase
You can also define alternate
environments by saving option
settings in a file other than
and reading in that file
Local .exrc files are also useful for defining abbreviations and key mappings (described later in this chapter). When we write a book or manual, we save all abbreviations to be used in that book in an .exrc file in the directory in which the book is being created.
As you can see when you type
As discussed earlier in this book,
Three other options control how
acts when conducting a search.
Normally, a search differentiates between uppercase and
does not match
), 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