5.4 Cut and Paste



The one of advantages of computer is the copy function. Using copy function, it is not necessary to input the same string in several times.


 Command  Action
 x  Cut (Delete) a character
 dd  Cut (Delete) a line
 yy  Copy a line
 p  Paste (Insert) the next character or line of cursor
 P  Paste (Insert) the previous character or line of cursor
 u  Undo the previous action such as cut and paste

Table 5-7: Commands for Cut & Paste

5.4.1 Cut and paste characters

The function of the cut and paste of a character is command mode, and uses the command which cuts a character, and the command which pastes the cut character.


Practice: Cut and paste characters


Open a file "vicutpaste.txt", input some strings like "iThis is aa pen.[Enter] That is book.[Esc]" (There are two spaces between "is" and "book".)


$ vi vicutpaste.txt ↵
"vicutpaste.txt" [New File]


Open a file by vi command.

This is aa pen.
That is book.


Figure 5-7: Screen

Firstly, use "x" command to delete a character, and "p" command to paste a character.
There are some mistakes in the above file. Delete "a" in "aa" using "x" command.

This is a pen.
That is book.


Figure 5-8: Screen

The characters which were deleted by "x" command store to buffer, then they can be used by "p" command. "p" command paste characters in buffer after cursor.
Here, move cursor to space after "is" in 2nd line, then input "p" command.

This is a pen.
That is a book.


Figure 5-9: Screen

"p" command paste after cursor. If you want to paste before cursor, you can use "P" command.
Let's delete space in beginning of 2nd line, then paste to beginning of 1st line.
Move cursor to beginning of 2nd line, delete a space using "x" command, then move cursor to beginning of 1st line, input "P" command.

TThis is a pen.
That is a book.


Figure 5-10: Screen

5.4.2 Undo previous cut or paste

If you want to cancel operations such as cut and paste, you can use undo function with "u" command.


Practice: Cancel cut and paste


Inputting "u" twice, cancel cut and paste.
Save and exit using ":wq" command.


:wq ↵
"vicutpaste.txt" [New] 2L, 32C Write


5.4.3 Cut and paste lines

The function of the cut and paste of a line unit is command mode, and uses the command which cuts a line, and the command which pastes the cut character string.

Practice: Cut and paste a line


Open "vicutlinepaste.txt" with vi command, input some strings like "i This is a pen.[Enter]That is a book.[Esc". (There is a space in beginning of 1st line, and no space in beginning of 2nd line.)


$ vi vicutlinepaste.txt ↵
"vicutlinepaste.txt" [New File]           0,0-1    All


When you want to cut a line, use "dd" command. Because the text of cut line are stored to buffer, you can paste using "p" or "P" command same as paste characters.
Here, cut the 1st line with "dd" command, then paste with "p" command in order to exchange the 1st line with the 2nd line.

That is a book.
This is a pen.



That is a book.
This is a pen.



That is a book.
This is a pen.


Figure 5-11: Screen

In order to save the new text and quit, we use ":wq" command.


:wq ↵
"vicutlinepaste.txt" 2L, 32C Write


5.4.4 Copy and paste lines

We can copy and paste a line character string. The copy in a line unit is the yy command (yank command), and the character string of the line which executed the yy command goes into a buffer. The text in a buffer can be pasted by p or P.


Practice: Copy and paste a line


Open "vicopypaste.txt" with vi command, input some strings like "i This is a pen.[Enter]That is a book.[Esc". (There is a space in beginning of 1st line, and no space in beginning of 2nd line.)


$ vi vicopypaste.txt ↵
"vicopypaste.txt" [New File]


In order to copy the 2nd line, copy the 2nd line with "yy" command, then paste with "p" command.


This is a pen.            (1) Input ”yy” command
This is a book.          (1) Input ”p” command



This is a pen.
This is a book.
This is a pen.


Figure 5-12: Screen

In order to save the new text and quit, we use ":wq" command.


:wq ↵
"vicopypaste.txt" [New] 3L, 48C Write



Previous Next