From: www.itworld.com

Grasping more of the vi editor

October 2, 2001 —

 

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.


Listing 1 /home/mjb/.exrc - a vi initialization script
set ic
set wrapmargin=15
set showmode
set number
set scroll=20

Yanking data

Cut, copy, and paste are powerful vi commands. The commands can also be
used in a variety of different ways. The delete line command (dd)
actually cuts text into a temporary paste buffer. This makes it very
easy to do cut and paste. The paste buffer is dumped (pasted back in to
the text) using the `p' command.

Suppose you are typing a letter and you realize that you need to
move a 7 line paragraph. In command mode position the cursor to the
first line of the paragraph and type: 7dd

This will delete 7 lines. They are removed from the edit buffer and
saved in the delete buffer (sometimes called the unnamed buffer). Now
move the cursor to where you want the paragraph to be placed and type


p

This will paste the contents of the delete buffer at the current
location starting on the line just below the cursor. The
delete buffer always contains the last deleted text (text deleted using
dd, x, or X), and "p" can always be used to paste it. This provides a
quick and simple cut and paste.

Copy and paste is more versatile. There are 26 copy buffers named
with the lower case letters a through z. You copy data into the buffer
by using the yank command.

The yank command has two main forms. One yanks only the current line
(the line is not truly yanked, it is copied to the copy buffer). The
second version of yank yanks the current line plus the additional
number of lines you request. Purists will argue that there is much more
that you can do with yank, and this is true, but these two are enough
to get a great deal of work done. An example of the first version in
command mode is:


"ayy

In this command, the letter "a" is the name of the copy buffer to use,
and the yy indicates that the current line is to be yanked. There's no
real explanation for the initial double quote -- that's just the way vi is
-- a bit obscure at times. This command yanks the current line into
buffer a.

The second version of yank will yank the current line plus the
additional number of lines you request.


"dy2     (press RETURN)

This yanks the current line plus the next two lines into
buffer d, a total of three lines.

The actual sequence of a yank command is double quote followed by
the lower case letter identifying the copy buffer to use followed by a
"y" and either a second "y" to indicate this line only or a number to
indicate the number of lines to yank in addition to the current line.
When a number of lines is used, end the command by typing RETURN. You
can also end a multi-line yank by typing another y as in


"y15y

If you use the lowercase letters a-z for a yank, the buffer is
overwritten with the information that is yanked. If you use uppercase
A-Z, the yanked information is appended to the buffer.


"Ky13y      (or omit the second y and type RETURN)

This command will yank the current line and 13 additional lines and add
them to the end of any already existing text in buffer k.

Where to?

Now that we have yanked all this text we need to paste it somewhere.
The command for pasting a buffer uses the quotes, the buffer character
and a "p" for paste.


"np

This command will paste the contents of buffer n to the line below the
current cursor position.

Copy and paste can also be used across multiple files. To do that, we
need to look at a couple more commands that you might already know.

The first is write, which writes the current changes for the file
you are editing to disk. This command is:


:w

It saves all your editing so far. This is a useful command to know as
it can save you from losing work in embarrassing crashes. Just type :w
once in a while and your edits are written back to your file.

The second command is edit, which allows you to open another
file for editing. This command is:


:e filename

Using these two commands, you can move back and forth between two files
without having to leave vi. When you exit vi the contents of the copy
buffers are lost, so being able to change files while still in vi
preserves the contents of the copy buffers and lets you copy and paste
across files.

Listings 2 and 3 are simple text files shown with line numbers (:set
number). The task is to copy paragraphs 1, 3, and 5 from file1.txt to file2.txt.


Listing 2: file1.txt
     1  Now is the time for every good person to come
     2  to the aid of the party.
     3
     4  A bird in the hand is worth two in the bush.
     5
     6  These are the times that try men's souls. The Metropolitan
     7  Transit Authority, better know as the MTA has attempted to
     8  levy a burdensome tax in the form of a subway increase.
     9
    10  There is more than one way to skin a cat, and more than
    11  one way to cut and paste.
    12
    13  This is the fifth paragraph to be transported
    14  across the inter-file boundary.
    15

Listing 3: file2.txt
     1  May the road rise to meet your feet
     2  May the wind push you ahead.
     3  May the devil not know you're in heaven
     4  Til an hour after your dead.
     5

Listing 4 contains the commands you will type to copy paragraphs
1, 3, and 5 from file1.txt to file2.txt


Listing 4: Copy and paste across files

Command                   Explanation
vi file1.txt    (RETURN)  open file1.txt for editing
:1              (RETURN)  move to line 1
"ay2y                     yank 3 lines (current line + 2) into buffer a
:6              (RETURN)  move to line 6
"by3y                     yank 4 lines into buffer b
:13             (RETURN)  move to line 13
"cy2y                     yank 3 lines into buffer c
:w              (RETURN)  save the current file (if you've edited it)
:e file2.txt    (RETURN)  start editing file2.txt
:$              (RETURN)  move to the end of file2.txt
"ap                       paste in buffer a
:$              (RETURN)  move to the end of file2.txt
"bp                       paste in buffer b
:$              (RETURN)  move to the end of file2.txt
"cp                       paste in buffer c
:w              (RETURN)  save your work
:e file1.txt    (RETURN)  back to file1.txt

Listing 5 is the resulting new file2.txt


Listing 5: file2.txt after editing
     1  May the road rise to meet your feet
     2  May the wind push you ahead.
     3  May the devil not know you're in heaven
     4  Til an hour after your dead.
     5
     6  Now is the time for every good person to come
     7  to the aid of the party.
     8
     9  These are the times that try men's souls. The Metropolitan
    10  Transit Authority, better know as the MTA has attempted to
    11  levy a burdensome tax in the form of a subway increase.
    12
    13  This is the fifth paragraph to be transported
    14  across the inter-file boundary.
    15

Another way to do this would be to use append style copying.
Listing 6 achieves the same result as listing 4.


Listing 6: Copy and paste across files using append style

Command                   Explanation
vi file1.txt    (RETURN)  open file1.txt for editing
:1              (RETURN)  move to line 1
"ay2y                     yank 3 lines (current line + 2) into buffer a
:6              (RETURN)  move to line 6
"Ay3y                     append 4 lines into buffer a
:13             (RETURN)  move to line 13
"Ay2y                     append 3 lines into buffer a
:w              (RETURN)  save the current file (if you've edited it)
:e file2.txt    (RETURN)  start editing file2.txt
:$              (RETURN)  move to the end of file2.txt
"ap                       paste in buffer a
:w              (RETURN)  save your work
:e file1.txt    (RETURN)  back to file1.txt

The :e command has a memory and uses the symbol # to stand
for the name of the last file edited. In both listing 4 and
6, the last line could have been:


:e #  (RETURN)

The delete finale

Now that you know how to paste, we can take a further interesting look
at the delete command. I mentioned that deleted text was placed in the
unnamed buffer. There are actually 10 delete buffers that are given the
names 0 through 9. Buffers 0 and 1 can be thought of as the same
buffer. When you delete any text it is stored in buffer 0. The
contents of buffer 8 are copied into buffer 9, buffer 7 is copied to 8,
6 to 7, and so on. Finally buffer 0 is copied to buffer 1. This isn't
exactly how it happens, but it is easiest to think of it this way.
Remembering that the unnamed buffer is buffer 0, and buffer 1 is a copy
of buffer 0, at any point while you are in command mode, you have the
following commands available to you that allow you to paste previous
deletions back into the file.


Command                   Effect
p                         Paste the last deletion into the text
"0p                       Paste the last deletion into the text
"1p                       Paste the last deletion into the text
"2p                       Paste the 2nd to last deletion into the text
"3p                       Paste the 3rd to last deletion into the text
"4p                       Paste the 4th to last deletion into the text
"5p                       Paste the 5th to last deletion into the text
"6p                       Paste the 6th to last deletion into the text
"7p                       Paste the 7th to last deletion into the text
"8p                       Paste the 8th to last deletion into the text
"9p                       Paste the 9th to last deletion into the text

This gives you a powerful range of options for cut and paste as well as
copy and paste. Cut and paste will work across files just as copy and
paste does.

The delete buffers also give you 9 levels of undo on line
deletions.

I have only scratched the surface of vi. There are of course complex
search and replace commands, macros, quick methods of substituting and
copying text, methods of copying an entire file into the file you are
editing, or ways of executing a shell command and having the output of the
command added to your edit buffer. You can also edit multiple files one
after the other and move between them quickly. Look for these in a
future article when I will have a chance to revisit vi.