Copy & Paste in Vim / Vi
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
- Press esc to return to normal mode. Any character typed in normal mode will be interpreted as a vim command.
- Navigate your cursor to the beginning of where you want to copy or cut.
- 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.
- Press v (lowercase) to enter visual mode. This will start selecting from where your cursor is.
- Press V (uppercase) to enter visual line mode. This will select the entire line.
- Press CTRL+V to enter visual block mode.
- Move your cursor to the end of where you want to copy or cut.
- Press y to copy. Press d to cut.
- Move the cursor to where you want to paste your selection.
- 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.
Related articles
Go To End of File in Vim / Vi
Select and delete to the end of a file in Vim
Select all in Vim / Vi
Select all and copy, paste, or delete the contents
Searching in Vim
Search forward, backward, case insensitive, and more
Show & Hide Line Numbers in Vim / Vi
Toggle absolute and relative line numbers
Undo & Redo in Vim / Vi
Keyboard shortcuts and summary of how Vim tracks changes
Vim Go To End/Start of Line
Go to the end or beginning of a line in Vim
Go To Line In Vim
Variety of approaches to go to lines
Go To Top of File in Vim
Learn how to jump the top of the file in Vim, then navigate, search, highlight, and delete.
Vim / Vi Page Up and Down Controls
Quick reference for Vim's page up and down controls
How to delete lines in Vim / Vi
The best answer depends on which mode you use
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.