How to Copy, Cut and Paste in Vim / Vi?

The Vim (Vi Modified) is a screen-based editor configurable to create any kind of customized document in Linux. The Vim editor is open source with options to change (cut, copy, paste) text in an efficient way. This editor is fast & powerful that can be run in the terminal to make changes in the text.

This particular post aims to demonstrate the methods to copy, cut, and paste in Vim. To achieve this, the following content is addressed:

Let’s get started!

How to Copy in Vim?

In the Vim editor, the copy/yanking can be performed with the “y” shortcut key. You can copy a single character, word, line, or document using the assigned shortcuts with the “y” key. Let’s check the options for copying the text in Vim:

yyThis command copies the entire line where the cursor is placed.
2yyTo copy multiple lines in Vim, the “[number]yy” command is used. To copy two lines, including the line where the cursor is placed and the next line, use the “2yy” command.
y%To copy the text between two similar characters like (), {} or [], use the y% command.
ywTo copy from a specific character to the end of the word, the “yw” command is used.
yiwTo copy the current word where the cursor is placed, press the yiw from the keyboard.
y$This command is used to copy the text from the cursor character to the end of the line.
y^It copies start till the cursor.
ytxThe “ytx” stands for “yank (copy) till x (specified character)” To copy the text between the cursor to the specified character in the line.
yfxThis command copies the text of the cursor position of a specific character.

Example 1: Copy an Entire Line in Vim

To copy the entire line in Vim, press the “y” key twice.

$ yy

Open the desired file in Vim utilizing the “vim <FileName>” command; by default, the file will open in normal mode. Navigate to the specified line, and press the “y” key two times. The clipboard copies the entire line.

Now, you can paste copied lines using the “p” shortcut (As discussed in the next sections).

Note: Click the “Esc” button to switch the “normal” & “insert” mode.

Example 2: Copy a Word in Vim

The option “w” (word) is used with the “y” key to copy a word in Vim. Move the cursor to a specific word and click the y and w keys to copy that word:

$ yw

How to Cut in Vim?

In normal editors, cut and delete are two different options, but in Vim, the cut option is called delete. The “d” character is utilized for cutting the line, word, or characters. The possible cutting scenarios are discussed below:

ddThis command cuts the entire line where the cursor is present.
2ddThis command cuts the two lines of text, including the cursor line and the next line.
d%d% command cuts the text of two similar characters {} or () and others.
dwThis command cuts the specific word from the character where the cursor is placed to the end of the word.
diwTo cut the current word of the cursor position, use the “diw” command.
d$To cut the whole line from the cursor to the left/end of the line.
d^To cut the whole text from the cursor to the right/start of that line.
dtxThis command cuts the text from the cursor to before the specified character.
dfxTo

Example 1: Cut/Delete the Entire Line

Open the document in Vim utilizing “vim <FileName>” in the terminal, and navigate to the desired line. Move the cursor to a line and press the “d” two times; the text will be deleted from that line and is cut to the clipboard:

$ dd

Example 2: Cut Two Lines in Vim

If you want to cut several lines, press that number and click the “d” key twice. For instance: to cut the two lines, point your cursor to the line and enter “2dd” the cursor line and the next line will be removed from the document and copied to the clipboard:

$ 2dd

Example 3: Cut Text from Cursor Word to End of Line Vim

Navigate to a specific word and press the “d” and dollar sign “$” keys, the text will disappear from the document and will be copied to the clipboard:

$ d$

How to Paste in Vim?

The copied text can be pasted with the “p” key before or after the cursor in Vim.

Example 1: Paste Text After the Cursor Line in Vim

To paste text in the next line of the specified cursor position, use the “p” command:

$ p

Example 2: Paste Text at the Cursor Line in Vim

If you want to paste the text at the same line of the Vim, use the Capital “P” key:

Note: It will move the cursor line down and add the pasted text on the cursor line.

$ P

Let’s work in “Visual” mode for copying, pasting, and cutting.

Bonus Tip: Copy, Cut, and Paste in Visual Mode in Vim

Visual mode, as the name indicates, will be highlighted/selected by moving the arrow keys, while in normal mode, we use the shortcut keys for that. Experienced users prefer this mode of operation in Vim.

The visual mode can be used by pressing a single key, “v” (visual). In visual mode, you will use the arrow keys from the keyboard to select the text for copy, paste, and cut. The Visual mode can be seen in the below picture:

Select the desired text and use the same shortcuts for coping, cutting, and pasting as done in the previous sections.

That’s all from this article.

Conclusion

In the normal and visual modes of Vim, use “y” to copy, “d” to cut, and to paste “p” or “P” shortcut keys are used. We can use the different shortcut keys to cut, copy and paste in Vim. This post has explained the way to copy, cut, and paste in Vim with the help of suitable examples.