Next: Searching and replacing Up: Advanced Vi Tutorial Previous: Modifing Text

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.

  1. Yanking (copying) the text to a buffer.
  2. Moving the cursor to the destination location.
  3. 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 tempory buffer that is easily currupted by other common commands. On occations the text my 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 draging the mouse cursor over text. Holding down the left mouse button and draging 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. Remmember the put vi into insert mode as the input could be interpreted as commands and the result will be unknown. Using the same techinque 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.

  1. Delete text to a named or unnamed buffer.
  2. Moving the cursor the to destination location.
  3. 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 buffer
See 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 Usefull commands).



Next: Searching and replacing Up: Advanced Vi Tutorial Previous: Modifing Text