->Title Page
->Intro
->Unix
->Vi editor
-->Basics
--->Moving around
--->Deleting text
--->Saving and quitting
->Editing
--->Search and Replace
--->Undo
--->Repeat
-->Vi reference
-->Miscellaneous tips
->Mirror sites
->Further reading

[ Up ]
[Prev][Home][Next]
[Author]

Vi ---> Copy, delete, and moving text

Knowing how to copy, delete, and move text is a prerequisite to any serious text editing task. If you have a small amount of text to delete or copy you may find it convenient to use dd or yy. These commands delete or yank the line of text that the cursor is on. dd deletes the current line of text and places it in a buffer. yy copies the current line of text to a buffer while leaving the original text unaltered. Many commands can be preceded by a number. This number indicates the number of times the command is repeated. These commands are no exception, e.g. 3dd deletes the line the cursor is on as well as the two lines below it and places them in a buffer. Text can be retrieved from the text buffer by typing p. The "pull" command inserts the text from the buffer into the text file beginning on the line below the cursor. This method of deleting and yanking works well for text blocks of known length or an easily countable number of lines, but is less satisfactory of large blocks of text.

As a result, vi has another method of text manipulation that involves marking text. Vi is capable of marking 26 different locations in a file. To mark a location in a text file move the cursor to the desired location and type m followed by the name you want to use. Each lowercase letter of the alphabet is a name.

Suppose we have a portion of text we wish to move from one location to another. We can do this by marking the beginning of the text block with the name q, i.e., mq will give the current cursor location the name q. Then we move to the end of the portion of text we wish to move and type d'q. This command deletes everything from the marked position to the cursor position and places it in a buffer. Text in the buffer is retrieved using the "pull" command already described. The "yank" command allows you to copy the text to the buffer instead of deleting it. Typing y'q instead of d'q will place a copy of the text in the buffer and leave the original text unaltered.1

Please notify owners of webpages with outdated links to these pages

1 Using ' in d'q begins deleting text at the beginning of the line that the q mark is on. Using ` instead, i.e. d`q begins deleting text at the exact location of the mark. This holds for the yank command as well.

Find this site useful? Want to give something back?

© 1993-2001 Christopher C. Taylor