|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| Old News ;-) | See Also | Recommended Links | Refererence | .vimrc | Humor | History | Etc |
Copying and Moving sections of text
Moving text involves a number of commands all combined to achieve the end result. This section will introduce named and unnamed buffers along with the commands which cut and paste the text.
Coping text involves three main steps.
- Yanking (copying) the text to a buffer.
- Moving the cursor to the destination location.
- Pasting (putting) the text to the edit buffer.
To Yank text to the unnamed use [y] command.
yy Move a copy of the current line to the unnamed buffer. Y Move a copy of the current line to the unnamed buffer. nyy Move the next n lines to the unnamed buffer nY Move the next n lines to the unnamed buffer yw Move a word to the unnamed buffer. ynw Move n words to the unnamed buffer. nyw Move n words to the unnamed buffer. y$ Move the current position to the end of the line.The unnamed buffer is a temporary buffer that is easily corrupted by other common commands. On occasions the text may be needed for a long period of time. In this case the named buffers would be used. vi has 26 named buffers. The buffers use the letters of the alphabet as the identification name. To distinguish the difference between a command or a named buffer, vi uses the ["] character. When using a named buffer by the lowercase letter the contents are over written while the uppercase version appends to the current contents.
"ayy Move current line to named buffer a. "aY Move current line to named buffer a. "byw Move current word to named buffer b. "Byw Append the word the contents of the named buffer b. "by3w Move the next 3 words to named buffer b.Use the [p] command to paste the contents of the cut buffer to the edit buffer.
p Paste from the unnamed buffer to the RIGHT of the cursor P Paste from the unnamed buffer to the LEFT of the cursor nP Paste n copies of the unnamed buffer to the LEFT of the cursor "ap Paste from the named buffer a RIGHT of the cursor. "b3P Paste 3 copies from the named buffer b LEFT of the cursor.When using vi within an xterm you have one more option for copying text. Highlight the section of text you wish to copy by dragging the mouse cursor over text. Holding down the left mouse button and dragging the mouse from the start to the finish will invert the text. This automatically places the text into a buffer reserved by the X server. To paste the text press the middle button. Remember the put vi into insert mode as the input could be interpreted as commands and the result will be unknown. Using the same technique a single word can be copied by double clicking the left mouse button over the word. Just the single word will be copied. Pasting is the same as above. The buffer contents will only change when a new highlighted area is created.
Moving the text has three steps.
- Delete text to a named or unnamed buffer.
- Moving the cursor the to destination location.
- Pasting the named or unnamed buffer.
The process is the same as copying with the change on step one to delete. When the command [dd] is performed the line is deleted and placed into the unnamed buffer. You can then paste the contents just as you had when copying the text into the desired position.
"add Delete the line and place it into named buffer a. "a4dd Delete 4 lines and place into named buffer a. dw Delete a word and place into unnamed bufferSee the section on modifying text for more examples of deleting text.
On the event of a system crash the named and unnamed buffer contents are lost but the edit buffers content can be recovered (See Useful commands).
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: February 28, 2008