cutting, and pasting can be reduced to three characters that use Vim-specific terminology
Understanding these terms will help you remember the correct keyboard shortcut
. And it means “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 paste.
I deliberately use the phrase “conceptually similar to” because these actions are not one and the same. If you want to dig deeper into this explanation, scroll down to the section below titled “What happens under the hood?”
Copy, Cut and Paste 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.
- Move the 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 is clearer to see what is happening. However, we have the keyboard shortcuts for normal mode (which achieve exactly the same effect) in the section below.
- Press v (lowercase) to enter visual mode. This will start selecting from where the cursor is.
- Press V (uppercase) to enter line-of-sight mode. This will select the entire line.
- Press CTRL+V to enter visual lock mode
- Move the cursor to the end of where you want to copy or cut.
- y to copy. Press d to cut.
- Move the cursor to where you want to paste the selection.
- Press P (uppercase) to paste before the cursor. Press p (lowercase) to paste after the cursor.
Using the arrow keys (or if you are an expert Vim user – h, j, k and l) to move around in your vim file can be time-consuming
Here are vim keyboard shortcuts to copy and cut if you want to be even more efficient than the basic steps described above.
- Copy the current line in vi
- 3yy: To pull multiple lines in vim, type the number of lines followed by yy. This command will copy (pull) 3 lines from the cursor position.
- ^: Copy everything from the beginning of the line to the cursor
- yiw: Copies the current word.
y$: Copy everything from the cursor to the end of the line y
Cut (delete)dd: Cut the
- current line
- 3dd: Cut 3 lines, starting
- d$: Cut everything from
from the cursor
the cursor to the end of the line
- uppercase): Paste before the cursor p (lowercase):
- paste after the cursorWhat
happens under the hood?
Vim terms are slightly different from their conceptual counterparts we mentioned earlier because these actions by default do not interact with the operating system’s clipboard. For example, you cannot paste text removed 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 record is named with a character, which you can use to interact with that record. For example, you can pull line 50 of the current file to record “a” and pull line 14 of the same file to record “b”, because you intend to paste both line 50 and line 14.
For more information about vim logs, see this stack overflow page.
This should be all you need to start copying, cutting and pasting into Vi. If you didn’t find what you were looking for, it may be worth checking out the official vim documents.