VI Tutorial - Lesson 9 - Manipulating Text

The VI editor allows the user to alter a file by way of copying or deleting a section of material to be inserted at another location. The user may select to complete the entire rearrangement with "yank and put" or "delete and put". In either action, this adjustment to the text is comprised of two separate command steps: the first part involves the original location and the second part deals with where the material is to be placed. Warning: yank & put and delete & put must be used in tandem. Inserting some other command action between the two steps, like correcting a typo, may cause the editor to become confused and produce unexpected results. A named buffer is another method to move or duplicate text. The advantage to using named buffers is that the delete or yank step can be done now and the put step handled later in the editing session.

9.1 Copying Text and Moving the Copy

In order to duplicate a section of text, you must first position the cursor at the material you want to copy. Next make a copy of the desired text by using the yank command. This places the copied text into a temporary buffer. Then immediately relocate the cursor at the point where you want the copied text placed and instruct VI to put the copy.

 

Step 1: Copying Text with the Yank Command (y, Y)

The yank command is used to make copies of words, lines, and sections of text being edited and place them into the unnamed buffer associated with the editor. The yank command is available for use in either the lower-case "y" format as an operator, or the upper-case "Y" format as a command. The lower-case "y" can be thought of as a yank operator which will combine with scopes to make an operator-scope command much in the same way as the delete and change operators. The scope to be yanked must be specified after the "y" command. Some of the most common scopes used with "y" are summarized next:

Yank Operator & ScopeResulting Action
ywyank word forward
ybyank word backward
y$yank from cursor to end of line (same as Y)
yLyank from current line to end of screen
yGyank from current line to end of file
y)yank from cursor to start of sentence
y(yank from cursor to end of sentence
yyyank complete line

 

It is important to remember, as in previous examples of operator-scope commands, that the cursor serves as the reference point and the requested action is initiated from the cursor backwards or forwards. Again the editor will permit you to address multiple scopes when using the yank operator; for example, the command "3yw" copies the three words to the right of the cursor.

The function of the upper-case "Y" command is to copy whole lines of text into the unnamed buffer; however, the lower-case "y" operator can also be used to yank a whole line by typing it twice, "yy". You can yank more than one line by preceding either "Y" or "yy" with a number. Typing "6Y" or "6yy" copies the current line and the next 5 lines into the unnamed buffer.

Step 2: Relocating the Copy with the Put Command (p, P)

The put command is used to place the contents of the unnamed buffer back into the file being edited. Returning whole lines into the text is handled differently than word and sentence fragments. If the text contained in the unnamed buffer forms a line segment or is a scope which partially spans more than one line, it will be placed within the current line after the cursor if you use lower-case "p", or before the cursor if you use upper-case "P". On the other hand, whole lines are returned to the file from the unnamed buffer without changing the current line. The lower-case "p" places the line or lines below the current line and the upper-case "P" places them above the current line.

A handy feature of yank & put is the ability to insert copy repeatedly within the same file. The format for this action is yank, relocate cursor, put, relocate cursor, put, etc. until all needed copies have been placed.

See below for an example of copying a line and moving the copy.

Yank 'Bobcats wail' with the "yy" command

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?

reposition cursor to "But what does the fox say"

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?

put 'Bobcats wail' above the current line with "P" command

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.Bobcats wail.But what does the fox say?

Yank 'Bobcats wail' with the "yy" command

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?

reposition cursor to "But what does the fox say"

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?

put 'Bobcats wail' below the current line with "p" command

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?Bobcats wail.

 

9.2 Deleting Text and Moving It

In order to move a section of text from one location in a file to another location, you must first position the cursor then delete the text to be moved into a temporary buffer with the delete command. Immediately relocate the cursor to the point in the file where you want to insert the previously deleted material and instruct VI to put the contents of the unnamed temporary buffer back into the file.

Step 1: Removing Text with the Delete Command (d, D)

Deleting a portion of text with the delete command does not simply cause the removed material to disappear into thin air; instead, the deleted text is placed in an unnamed editor buffer. Thus, all you have to do to move text within a file is delete it into the unnamed buffer and put it back into the file where you want it. Since the delete command forms the first step in moving text, it is possible to move any portion of text which the delete command can delete, such as characters, words, and lines. A complete description of the delete command can be found in the Correcting Text section.

Step 2: Relocating the Deleted Text with the Put Command (p, P)

The put command works in the same manner with delete & put as it does with yank & put. Partial sentence fragments are placed after the cursor within the current line when the lower-case "p" is pressed and before the cursor when the upper-case "P" is used. Whole lines, on the other hand, are returned to the file from the unnamed buffer without changing the current line. The lower-case "p" placing the line or lines below the current line and the upper-case "P" placing them above the current line. The cursor, while on the current line when the put command is issued, moves to the first character space on the first line of the freshly placed text.

Below is an illustration of how delete & put works when you delete a whole line and then put the contents of the buffer back into the file.

Yank 'Bobcats wail' with the "dd" command

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?

reposition cursor to "But what does the fox say"

Aardvarks sniff.@Cats purr.Dogs bark.But what does the fox say?

put 'Bobcats wail' above the current line with "P" command

Aardvarks sniff.@Cats purr.Dogs bark.Bobcats wail.But what does the fox say?

Yank 'Bobcats wail' with the "dd" command

Aardvarks sniff.Bobcats wail.Cats purr.Dogs bark.But what does the fox say?

reposition cursor to "But what does the fox say"

Aardvarks sniff.@Cats purr.Dogs bark.But what does the fox say?

put 'Bobcats wail' below the current line with "p" command

Aardvarks sniff.@Cats purr.Dogs bark.But what does the fox say?Bobcats wail.

 

9.3 Named Buffers

Named buffers offer another way to copy (yank) or remove (delete) text and then reenter (put) it into the file. Usually this is a feature used by more advanced users. Every time you open a file with the VI editor, 26 named buffers (a-z) are created for your use. The advantage of using named buffers to duplicate and rearrange text over the previous discussed yank & put and delete & put method is that you have the luxury of copying or removing now and resubmitting the text later in the editing session. Since the unnamed buffer associated with the editor only saves the last deleted or yanked text, you must put its contents back into the file when moving or copying before performing some other editor command. The named buffer permits access to its contents at anytime during editing session. Warning: VI's 26 named buffers remain only for the life of the current editing session. If you do not use the material placed in a named buffer during the same session it is filled, the material is lost.

To use a named buffer with the yank and delete commands, you must first locate the cursor in the text at the point where the material to be placed in the buffer is situated. The VI editor is informed you intend to use a named buffer when the double quote (") is used followed by the name of the buffer (a-z) and then the command you want carried out. Thus typing:

 

     "g7yy            "g7dd

would tell the editor you are:

        "             calling forth a named buffer        g             its name is  gor      7yy           yanking 7 lines        7dd           deleting 7 lines

Anytime during a session you wish to append more information into a named buffer, it is done by recalling the buffer with a capital letter for a name. To append information to the bottom of the buffer used above, you would type:

 

     "G3yy            "G3dd

Warning: These named buffers are not write protected. If a named buffer contains information and it is called a second time with its lower-case name, the original material is over-written.

Later when you are ready to make use of the content of the named buffer, you would type:

 

     "gp              "gP

telling the editor you are:

     "      calling forth a named buffer     g      its name is  gor   p      putting the contents below the current line     P      putting the contents above the current line

The "putting" action of a named buffer may be exercised again and again during an editing session and a copy of its contents put into the file as often as needed. Different users select named buffers in different ways. Some users select a particular buffer because of the material to be placed in it (z buffer for zebras, m buffer for money). Other users only use favorite buffers (like x, y, z or their initials). Whatever method you devise for buffer selection, you must remember its name. The editor provides no easy way to find out which buffer has which text segment. For the user who gets totally mucked-up and can not remember in which buffer that necessary file section was hidden, a possible (but painful) solution is to go to the end of the file and begin emptying buffers one-by-one until the mystery buffer is rediscovered.

The procedure to accomplish this is:

  1. go to the last line of the file
  2. empty a buffer with " buffer name p
  3. check buffer contents
  4. remove dumped buffer contents with "u" (undo)
  5. if not mystery buffer, precede to step 2 and repeat with next named buffer

previous | MENU | next