Vi editor: Copy/Paste between files
Assuming you have some basic knowledge on vi editor and familiar with the basic operation of vi editor. It might come into one stage where you will start asking around on how to copy some text from one file into another file without exiting from the vi editor.
I don’t know what exactly the features are, but it uses some kind of buffer to store the information that you want to copy. The good thing of this feature are, you can actually copy several blocks of text at the same time and jump into the other file and paste it there, instead of you have to do it one at a time. OK, let’s begin. Lets say you have a file named file1.txt
User# cat file1.txt; this is Para1 line1 of file1 this is Para1 line2 of file1 this is Para1 line3 of file1 this is Para1 line4 of file1 this is Para2 line1 of file1 this is Para2 line2 of file1 this is Para2 line3 of file1 this is Para2 line4 of file1 this is Para3 line1 of file1 this is Para3 line2 of file1 this is Para3 line3 of file1 this is Para3 line4 of file1 this is Para4 line1 of file1 this is Para4 line2 of file1 this is Para4 line3 of file1 this is Para4 line4 of file1 user#
and let’s say you have another file named file2.txt
User# cat file2.txt this is Para1 line1 of file2 this is Para1 line2 of file2 this is Para1 line3 of file2 this is Para1 line4 of file2 user#
The target is to copy paragraph1 and paragraph3 from file1.txt into file2.txt and copy paragraph4 from file1.txt and paragraph1 from file2.txt into new file named file3.txt.
First we will edit file1.txt using vi editor.
User# vi file1.txt 1 this is Para1 line1 of file1 2 this is Para1 line2 of file1 3 this is Para1 line3 of file1 4 this is Para1 line4 of file1 5 6 this is Para2 line1 of file1 7 this is Para2 line2 of file1 8 this is Para2 line3 of file1 9 this is Para2 line4 of file1 10 11 this is Para3 line1 of file1 12 this is Para3 line2 of file1 13 this is Para3 line3 of file1 14 this is Para3 line4 of file1 15 16 this is Para4 line1 of file1 17 this is Para4 line2 of file1 18 this is Para4 line3 of file1 19 this is Para4 line4 of file1 ~ ~
You can enable the line number (as example above) by using the command “:set nu” in vi’s command mode. first we need to do copy paragraph 1. Press Esc to make sure you’re in the command mode, then position your cursor at line 1 (column is not important as you’ll copy the whole line anyway), and type:
mk
Then move your cursor to line 4 and type:
“ay’k
If successful, the cursor will move automatically to line 1 (where you do the mk earlier).
The Explanation: mk is to mark the beginning text block that you want to copy, and the “ay’k is to mark the end of the text block. The a is the buffer tag, you can store another text block into another buffer for example buffer b, or c or so on. Now lets continue our example.
Move your cursor to line 11 which is the start of the paragraph 3, and type mk and then move the cursor to line 14 and type “by’k. As you can see, now we store the paragraph 3 into buffer b. One more paragraph to copy.
Move your cursor to line 16 which is the start of the paragraph 4, and type mk and then move the cursor to line 19 and type “cy’k. As you can see, now we are store the paragraph 4 into buffer c. Done with the copy process. Now lets jump into file2.txt
Still in VI’s command mode, type : (colon) (the cursor will move to the bottom-left of the screen) and followed by e file2.txt as shown below, and then press Enter key:
17 this is Para4 line2 of file1 18 this is Para4 line3 of file1 19 this is Para4 line4 of file1 ~ ~ :e file2.txt
This will open file2.txt.
1 this is Para1 line1 of file2 2 this is Para1 line2 of file2 3 this is Para1 line3 of file2 4 this is Para1 line4 of file2 5 ~ ~ ~ ~
First, remember that we need to store paragraph 1 of file2.txt into buffer. Move the cursor to line 1 and type mk and then move the cursor to line 5 and then type “dy’k. This will store paragraph 1 with one blank line (line 5) into buffer d.
We will paste the Paragraph 1 and paragraph 3 from file1.txt at the end of this file. Move the cursor to line 5 (the end of the file), and type “ap. This will paste the text block from buffer a as shown below:
1 this is Para1 line1 of file2; 2 this is Para1 line2 of file2 3 this is Para1 line3 of file2 4 this is Para1 line4 of file2 5 6 this is Para1 line1 of file1 7 this is Para1 line2 of file1 8 this is Para1 line3 of file1 9 this is Para1 line4 of file1 ~ ~ ~ ~
Move your cursor to line 9 (the end of the file). If you wish to add an empty line after the line 9 you can do so by using the command o (smallcase o) which will insert a blank line after line 9 and change the VI mode into editing mode, just press ESC key to go back to the command mode. Now move the cursor to line 10, and paste the buffer b by typing this command: “bp ,you should get result as shown below:
1 this is Para1 line1 of file2 2 this is Para1 line2 of file2 3 this is Para1 line3 of file2 4 this is Para1 line4 of file2 5 6 this is Para1 line1 of file1 7 this is Para1 line2 of file1 8 this is Para1 line3 of file1 9 this is Para1 line4 of file1 10 11 this is Para3 line1 of file1 12 this is Para3 line2 of file1 13 this is Para3 line3 of file1 14 this is Para3 line4 of file1 ~ ~ ~ ~
Done with file2.txt, to save file2.txt, simply type : (colon) followed by w (for Write). Now left with one more operation, to paste text block in buffer c and d into new file file3.txt.
From file2.txt, type : (colon) followed by e file3.txt and then press Enter as shown below:
12 this is Para3 line2 of file1 13 this is Para3 line3 of file1 14 this is Para3 line4 of file1 ~ ~ ~ ~ :e file3.txt
You will see a blank file, which is a new file file3.txt, This time we want to paste text block from buffer d first and then from buffer c. Now type “dp to paste the text block from buffer d, you should see as below:
1 this is Para1 line1 of file2 2 this is Para1 line2 of file2 3 this is Para1 line3 of file2 4 this is Para1 line4 of file2 5 ~ ~ ~ ~
Then move the cursor to line 5 and then type “cp to paste text block from buffer c
1 this is Para1 line1 of file2 2 this is Para1 line2 of file2 3 this is Para1 line3 of file2 4 this is Para1 line4 of file2 5 6 this is Para4 line1 of file1 7 this is Para4 line2 of file1 8 this is Para4 line3 of file1 9 this is Para4 line4 of file1 ~ ~ ~ ~
Now type : (colon) followed by w to save file3.txt. Done.
Happy editing.