Vim Copy and Paste Multiple Lines

The majority of the Unix variants come with Vim, a popular open-source text editor, installed by default. The text editor has two modes of operation: as a standalone application in a GUI or from a command-line interface. One of the most frequent jobs while working with the text files is copying, cutting, and pasting text. In all Linux variants, Vim or its predecessor Vi is already installed. When your preferred editor is unavailable, having a rudimentary understanding of Vim can be useful. Vim provides a variety of methods for copying and pasting the lines if you need to modify some lines within a file while using the editor rather than having to rewrite the entire line. The words “yank” and “put” are interchangeable terminologies for the actions of copying and pasting. Therefore, in Vim, you must yank the lines if you need to copy them, and you must paste them at a precise position. The Vim editor’s copy and paste functions are demonstrated in this article.

Example 1:

Before copying and pasting, we need to create the Vim file as follows:

The Vim editor’s “Insert” mode can be accessed by pressing “Esc” and tapping “I.” The selected text is copied using the “yank” or “y” command. The important thing to remember is that Vim allows you to read any already created file. If not, just launch the Vim editor and begin adding the text. The Vim file has some new, straightforward text lines as shown in the following image:

We accessed the Vim’s Visual mode by using the “Esc” key. Then, we tapped the key “v” on the keyboard to attempt the copy command. The visual mode is activated as shown in the following image:

The first character in the first line was there, where the cursor was. By hitting the “j” or “next” key arrow button on the keyboard, we chose the highlighted text from the first line. To replicate this line, we came to a full stop and pushed the “y” key.

After clicking the Esc key and the “I” key, we are now back in the insert mode. The sixth line is reached by scrolling down below and the cursor is set at its beginning.

The extracted text was then pasted by pressing the “p” button. In this instance, we used a straightforward technique to copy and paste within the Vim editor.

The delete command can also be used for copying and pasting. We, therefore, have a little example of it. From the Visual block, we chose the text’s first line displayed.

As you can see, the highlighted text was cut and pasted in a different location once we pushed “d.” Now that we switched to the insert mode, we used the “p” command to paste the text to the last empty line. The results are displayed in the following:

Example 2:

The “yy” yank command is intended to copy only the line that contains the new line where the mouse currently resides. Therefore, we used the Insert mode of Vim to add some lines to the file. Currently, the cursor is located at the start of the first line. To replicate the first line with a newline character, switch to the regular mode and hit “yy.”

The line is where the cursor is currently located after reopening the Insert mode. The outcome is seen in the following image after the “p” key is pressed.

Example 3:

Vim’s yank command allows you to copy many lines at once. This is also what we do. In the insert mode, the cursor is set to the first character of the third line. By selecting the regular mode and pressing “3yy,” we are able to duplicate the three lines. As you can see, Vim also displays the action taken to yank the three lines.

The output appears as in the image once you press “p”. The final paste operation involves all the three lines.

Example 4:

The y$ command copies the text from the end of a certain line to the location where the cursor is now located. Therefore, we inserted multiple lines using the insert mode, setting the cursor to the first character of the first line. The second line is reached after pressing the “y$” command.

In this case, we used the put command, or the “p” command, to paste the copied text.

Example 5:

The text is copied using the “y^” yank command from the spot where the cursor is currently located at the beginning of that specific line. After the word “paste”, we set the cursor at a blank space. Then, in normal mode, we press the “y^” key.

The last text line must be scrolled down in order to utilize the place command or “p” to paste the copied text into the insert mode while using the regular mode. As you can see, the entire last line is pasted from the very first character through the word “paste”.

Example 6:

The text can be copied from the current location to the following word by using the “yw” symbol. As a result, we click the “yw” after placing the mouse there.

The cursor is placed in the regular mode after the next line and “p” is entered. An illustration of the output is provided in the following:

Example 7:

To copy a single word, use the “yiw” command. Then, as illustrated in the following, you must press “yiw” while keeping the pointer over that word.

We obtained the following result after executing the put command:


This article includes a thorough description and example of a few straightforward commands for copying and pasting the text or data into a Vim file. The “yank” command and the “put” command are both used in a variety of ways for copying and pasting. You are now familiar with using the Vim’s copy and paste functions. It is possible that you’ll frequently need to modify your package’s setup. The majority of things may be completed easily in Vim by using the cut and paste functions.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.