How to Perform Copy, Cut and Paste Operations in Vim and Vi

Suraj Joshi Feb 02, 2024
  1. Copy Operation in Vi and Vim
  2. Cut Operation in Vi/Vim
  3. Paste Operation in Vi/Vim
How to Perform Copy, Cut and Paste Operations in Vim and Vi

Vim and Vi are one of the most popular text editors among Linux users. Basically, Vim and Vi are the same; however, Vim can be considered as the improved version of Vi with the additional feature of code highlighting.

Copy, Cut, and Paste are one of the most popular operations for text files.
To copy the text in Vi/Vim, we place the cursor at the desired position and use the y command followed by the required movement command. We use the d command for cutting or deleting the text in the normal mode of operation. Similarly, the cursor is placed at the desired position and p to paste the text after the cursor or P to paste before the cursor.

Copy Operation in Vi and Vim

In Vim, the copy operation is known as yank operation. To copy the text in Vi/Vim, we place the cursor at the desired position and use the y command followed by the required movement command.

Some of the popular Copy (Yank) commands are:

yy Copy the current line in which cursor is placed, including the newline character
2yy Copy two lines beginning from the line in which cursor resides
y$ Copy everything beginning from the position where the cursor is placed to the end of the line
y^ Copy everything beginning from the position where the cursor is placed to the start of the line
yw Copy everything beginning from the position where the cursor is placed to the start of another word
yiw Copy the current word
y% Copy text between matching characters like brackets. e.g. Used to copy everything between ( ).

Cut Operation in Vi/Vim

In Vim, the cut operation is known as delete operation. To cut the text in Vi/Vim, we place the cursor at the desired position and use the d command followed by the required movement command. Movement commands applied for Copy(Yank) are also valid for Cut(Delete).

Some of the popular Cut (Delete) commands are:

dd Cut the current line in which the cursor is placed, including the newline character
2dd Cut two lines beginning from the line in which cursor resides
d$ Cut everything beginning from the position where the cursor is placed to the end of the line
d^ Cut everything beginning from the position where the cursor is placed to the start of the line
dw Cut everything beginning from the position where the cursor is placed to the start of another word
diw Cut the current word
d% Cut text between matching characters like brackets. e.g. Used to copy everything between ( ).

Paste Operation in Vi/Vim

In Vim, the paste operation is known as Put operation.

To perform paste operation in Vi/Vim, the cursor is placed at the desired position and p to paste the text after the cursor or P to paste before the cursor.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn