How to copy, cut and paste in Vim or Vi

How to copy, cut and paste in Vim or Vi

Copy, cut and paste in Vim

Vim is one of the popular text editors for software developers and system administrators. It is pre-installed on macOS and various Linux distributions. When working in Vim, pasting or cutting text is one of the most common tasks. In this tutorial you will learn how to copy, cut and paste in Vim or Vi.

Copy, cut and paste in visual mode

If you are using another mode Esc Start in normal mode so that you can move through the file by running Vim commands.

  1. Position the cursor on the line where you want to start copying or cutting.
  2. You can press here v (Lowercase) Start copying from the cursor position, V (Uppercase) Copy the entire line at the cursor.
  3. Then use the h, j, k, and l keys to move the cursor at the end of the text left, down, up, and right.
  4. You can press here y Copy or press text d Cut the text.
  5. Then place the cursor where you want to paste the text.
  6. Finally p (Lowercase) Paste text after cursor position and press P (Uppercase) Paste the text before the cursor position.

You can perform a copy / paste or cut / paste operation using the steps above. If you want to learn more short commands for copying to cut text, read the points below.

Copy or yank in Vim

Vim’s copy process, also called yank. To copy text, you can also use the following command directly in normal mode:

ywCopy text to beginning of next word
YupCopy the current word where the cursor is located.
y $Copies text from the cursor to the end of the line.
yyCopy the current line with a newline character.
3yyCopy three lines from the line where the cursor is located.
4yyCopy 4 lines from the line where the cursor is.
y%Copy text until it matches a paired character such as (). [], {}.
y ^Copies text from the cursor to the beginning of the line.

Cut or delete in Vim

The cutting process, also called deletion in Vim. To cut text, you can also use the following commands directly in normal mode:

dwCuts text to the beginning of the next word.
wCut the current word where the cursor is located.
d $Cuts text from the cursor to the end of the line.
ddCut the current line with a 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%Cuts text until it matches a paired character such as (). [], {}.
d ^Cuts text from the cursor to the beginning of the line.

Paste to Vim

You can paste text into Vim using p Key to paste after cursor position P Paste before the cursor position.

Conclusion

In this tutorial, you learned how to copy, cut, and paste in Vim or Vi. If you have any questions about this, please comment below.

Sidebar