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.)
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.
To turn line numbers off, type :set nonu and press Return.
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 |
and press Return.
When specifying 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 |
The basic form of the ex move command is similar to the copy command discussed above:
: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 |
and press Return.
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 |