Solaris Advanced User's Guide

Using ex Commands

ex commands are more accurate and convenient than yank, delete, and put when you're dealing with large blocks of text. Rather than counting lines on the screen and then searching for an insertion point, you give vi a range of lines to be moved or copied and then specify the line before the insertion point. Of course, with a delete command there is no insertion point.

Turning Line Numbers On and Off

To turn line numbers on, type :set nu and press Return.

Line numbers appear in the left margin. Note that these numbers do not appear when you print out the file. They are visible only on the screen.


1 Oh, when I die, take my saddle from the wall,
2 Put it on my pony, lead him out of the stall.
3 Tie my bones to his back, point our faces to the west,
4 And we'll ride the prairies that we love the best.
5
6 Ride around, little doggies,
7 Ride around real slow.
8 Firey and Snuffy are rarin' to go.
~
~
~
~
~
~
:set nu

To turn line numbers off, type :set nonu and press Return.

Copying Lines

The basic form of the ex copy command is:


:line#,line# co line#

The first two numbers (separated by a comma) specify the range of lines to be copied. The third number is the line before the insertion point.

For example, to copy lines 1 through 5 of paint and place the copy after line 12, you would type the following:


:1,5 co 12

Then press Return.

When you specify line ranges, use the abbreviations:

Thus, to copy the range “from the current line through line 5” and insert this block after line 12, you would type:


:.,5 co 12

To copy the range “from line 6 through the end of the file” and insert this block after line 2, you would type:


:6,$ co 2

Moving Lines

The basic form of the ex move command is similar to the copy command that is discussed in the previous section.


:line#,line# m line#

Line ranges and insertion points are specified in the same ways, including use of the abbreviations . and $. The difference in function is simply that “move” removes a block from one location and reinserts it elsewhere.

For example, to move lines 1 through 5 to the line following 12, you would type:


:1,5 m 12

Then press Return.

Deleting Lines

To delete a range of lines, use the command form:


:line#,line# d

For example, to delete lines 1 through 5, you would type:


:1,5 d