Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [355]

By Root 1102 0
unless you want to replace only the first occurrence of pattern on each line.

You can also use marks to refer to lines. Marks are just single-letter names that are given to cursor locations within the document. Moving the cursor to a location in the file and typing ma will set the mark a at that point. (Marks may be named any of the letters a-z or A-Z.) You can move the cursor directly to the mark a with the command `a (with a backquote). Using a regular single quote (as in 'a) will move the cursor to the beginning of the line that the mark a is on.

Marks allow you to "remember" cursor locations that denote a region of text. For example, if you want to search and replace a block of text, you can move the cursor to the beginning of the text, set a mark, move the cursor to the end of the text, and use the command:

:'a,.s/weeble/wobble/

where 'a refers to the line containing mark a, and . refers to the current line.

Moving Text and Using Registers

One way to copy and move text is to delete it (using the d or dd commands) and then replace it with the P command, as described earlier. For example, if you want to delete 10 lines, starting with the line that contains your cursor, and paste them somewhere else, just use the command 10dd (to delete 10 lines), move the cursor to the new location for the text, and type p. You can copy text in this way as well: typing 10dd followed by P (at the same cursor location) deletes the text and immediately replaces it. You can then paste the text elsewhere by moving the cursor and using p multiple times.

Similar to dd is the yy command, which "yanks" text without deleting it. You use p to paste the yanked text as with dd. But note that each yank operation will delete the previously yanked text from the clipboard.

The deletion and yank commands can be used on more general regions than lines. Recall that the d command deletes text through a move command; for example, d$ deletes text from the cursor to the end of the line. Similarly, y$ yanks text from the cursor to the end of the line.

Let's say you want to yank (or delete) a region of text. This can be done with marks as well. Move the cursor to the beginning of the text to be yanked and set a mark, such as ma. Move the cursor to the end of the text to be yanked and use the command y`a. This yanks text from the cursor position to the mark a. (Remember that the command `a moves the cursor to the mark a.) Using d instead of y deletes the text from the cursor to the mark.

The most convenient way to cut, copy, and paste portions of text within vi is to use registers. A register is just a named temporary storage space for text you wish to copy between locations, cut and paste within the document, and so forth.

Registers are given single-letter names; any of the characters a to z or A to Z are valid. The " command (a quotation mark) specifies a register; it is followed by the name of the register, as in "a for register a. The lowercase letters and their uppercase counterparts refer to the same registers: using the lowercase letter overwrites the previous contents of the register, and using the uppercase letter appends to it.

For instance, if we move the cursor to the first line, as in Figure 19-14, and use the command "ayy, the current line is yanked into the register a. We can then move the cursor to the second line, and use the command "ap to paste the text from register a after the current line (see Figure 19-15).

Figure 19-14. vi buffer before a yank

Figure 19-15. vi buffer after a yank

Similarly, the command "ay`a yanks text from the cursor to mark a into register a. Note that there is no correspondence between mark and register names!

Using registers allows you to copy text between files. Just copy the text to a register, use the :e command to edit a new file, and paste the text from the register.

Extending vi

vi is extensible in many ways. Most of the commands we've introduced can be generalized to arbitrary regions of text. As we've already seen, commands such as d and y operate on the text from

Return Main Page Previous Page Next Page

®Online Book Reader