Vim Copy & Paste Terminology
The keyboard shortcuts to copy, cut, and paste can be boiled down into three characters that utilize Vim-specific terminology.
Understanding these terms will help you recall the correct keyboard shortcut.
- Y stands for “yank” in Vim, which is conceptually similar to copying.
- D stands for “delete” in Vim, which is conceptually similar to cutting.
- P stands for “put” in Vim, which is conceptually similar to pasting.
I deliberately use the phrase “conceptually similar to” because these actions are not one and the same. If you want to dive deeper into this explanation, scroll down to the section below titled “What Happens Under the Hood?”
Copy, Cutting, and Pasting in Vim/Vi - The Basics
- 1. Press esc to return to normal mode. Any character typed in normal mode will be interpreted as a vim command.
- 2. Navigate your cursor to the beginning of where you want to copy or cut.
- 3. To enter visual mode, you have 3 options. We suggest using visual mode because the selected characters are highlighted, and it’s clearer to see what’s happening. However, we have the keyboard shortcuts for normal mode (which achieve the exact same effect) in the section below.
- 4. Press v (lowercase) to enter visual mode. This will start selecting from where your cursor is.
- 5. Press V (uppercase) to enter visual line mode. This will select the entire line.
- 6. Press CTRL+V to enter visual block mode.
- 7. Move your cursor to the end of where you want to copy or cut.
- 8. Press y to copy. Press d to cut.
- 9. Move the cursor to where you want to paste your selection.
- 10. Press P (uppercase) to paste before your cursor. Press p (lowercase) to paste after your cursor.
Vim Keyboard Shortcuts
Using arrow keys (or if you are an expert Vim user - h, j, k, and l) to move around in your vim file can take a long time.
Here are vim keyboard shortcuts for copying and cutting if you want to be even more efficient than the basic steps outlined above.
Copying (Yanking)
- yy: Copy the current line in vi
- 3yy: To yank multiple lines in vim, type in the number of lines followed by yy. This command will copy (yank) 3 lines starting from your cursor position.
- y$: Copy everything from the cursor to the end of the line
- y^: Copy everything from the start of the line to the cursor.
- yiw: Copy the current word.
Cutting (Deleting)
- dd: Cut the current line
- 3dd: Cut 3 lines, starting from the cursor
- d$: Cut everything from the cursor to the end of the line
Putting (Pasting)
- P (uppercase): Paste before your cursor
- p (lowercase): Paste after your cursor
What Happens Under the Hood?
Vim terms are slightly different from their conceptual counterparts that we mentioned above because these actions by default do not interact with the OS clipboard. For example, you can't paste yanked text from Vim into a different application with CMD + V.
Yank, delete, and put interact with Vim's notion of “registers”, which are basically Vim -specific clipboards. Each register is named with a character, which you can use to interact with that register. For example, you might yank line 50 of the current file to register “a” and yank line 14 of the same file to register “b”, because you intend to paste both line 50 and line 14.
To learn more about vim registers, check out this Stack Overflow page.
Conclusion
This should be everything you need to get started to copy, cut and paste in Vi. If you didn’t find what you were looking for, it may be worth checking out the official vim docs.
Written by
Jessica Wang
Developer Advocate, Warp
Filed Under
Related Articles
Vim Find And Replace
You can use either the substitute command or the slash and dot method to search for patterns and replace with different values.
Vim Modes
Learn about seven of Vim’s modes
Vim / Vi Page Up and Down Controls
Quick reference for Vim's page up and down controls
Undo & Redo in Vim / Vi
Keyboard shortcuts and summary of how Vim tracks changes
Show & Hide Line Numbers in Vim / Vi
Toggle absolute and relative line numbers
Select all in Vim / Vi
Select all and copy, paste, or delete the contents
Searching in Vim
Search forward, backward, case insensitive, and more
Go To End of File in Vim / Vi
Select and delete to the end of a file in Vim
How to delete lines in Vim / Vi
The best answer depends on which mode you use