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.
- Position the cursor on the line where you want to start copying or cutting.
- You can press here
v
(Lowercase) Start copying from the cursor position,V
(Uppercase) Copy the entire line at the cursor. - Then use the h, j, k, and l keys to move the cursor at the end of the text left, down, up, and right.
- You can press here
y
Copy or press textd
Cut the text. - Then place the cursor where you want to paste the text.
- Finally
p
(Lowercase) Paste text after cursor position and pressP
(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:
yw | Copy text to beginning of next word |
Yup | Copy the current word where the cursor is located. |
y $ | Copies text from the cursor to the end of the line. |
yy | Copy the current line with a newline character. |
3yy | Copy three lines from the line where the cursor is located. |
4yy | Copy 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:
dw | Cuts text to the beginning of the next word. |
w | Cut the current word where the cursor is located. |
d $ | Cuts text from the cursor to the end of the line. |
dd | Cut the current line with a newline character. |
3dd | Cut three lines from the line where the cursor is located. |
4dd | Cut 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.