How to Copy, Cut, Paste in Vim or Vi

How to Copy, Cut, Paste in Vim or Vi
How to Copy, Cut, Paste in Vim or Vi

Copy, Cut, Paste in Vim

Vim is one of the popular text editors among the software developers and system administrators. On the macOS as well as on different Linux Distros it comes preinstalled. When you work with Vim copy paste or cut pasting text is one of the common tasks we do most of the time. In this tutorial, you are going to learn how to Copy, Cut, Paste in Vim or Vi.

Copy, Cut, Paste in Visual Mode

If you are in any other mode press Esc to come in Normal Mode so you can run Vim commands and navigate through the file.

  1. Place the cursor at the line where you want to start copying or cutting.
  2. Here you can press v (lowercase) to start copy from the position where the cursor located or press V (uppercase) to copy full line where cursor located.
  3. Now use h, j, k, l keys to move the cursor left, down, up, right where the end of text located.
  4. Here you can press y to copy text or press d to cut text.
  5. Then place the cursor where you want to paste the text.
  6. Finally, press p (lowercase) to paste the text after cursor position and press P (uppercase) to paste the text before the cursor position.

You can do Copy-Paste or Cut Paste operations by using the above instructions. If you want to learn more short commands for copying to cutting text then read below points.

Copying or Yanking in Vim

The process of copying also known as yanking in Vim. To copy the text you can also use the following commands directly in Normal Mode.

ywCopy the text to the start of next word
yiwCopy the current word where the cursor is located.
y$Copy text from the cursor to the end of the line.
yyCopy the current line with the newline character.
3yyCopy three lines from the line where the cursor is located.
4yyCopy four lines from the line where the cursor is located.
y%Copy text until matching pair character like (), [], {}.
y^Copy text from the cursor to the start of the line.

Cutting or Deleting in Vim

The process of cutting also known as deleting in Vim. To cut the text you can also use the following commands directly in Normal Mode.

dwCut the text to the start of the next word.
diwCut the current word where the cursor is located.
d$Cut text from the cursor to the end of the line.
ddCut the current line with the newline character.
3ddCut three lines from the line where the cursor is located.
4ddCut four lines from the line where the cursor is located.
d%Cut text until matching pair character like (), [], {}.
d^Cut text from the cursor to the start of the line.

Paste in Vim

You can paste the text in Vim using p key to paste after the cursor position and P to paste before the cursor position.

Conclusion

In this tutorial, you have learned How to Copy, Cut, Paste in Vim or Vi. If You have any queries regarding this please comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here