In Vim, these operations are handled differently from what you might be used to in almost any other modern editor or word processor (Ctrl-C, Ctrl-X, Ctrl-V). To understand, you need to know a little about registers and motions.
Note: this section will not cover Visual Mode copying and cutting or range yanking as these are beyond the scope of both Normal Mode and basic editing.
Vim uses the concept of registers to handle moving text around within the program itself. Windows has a single clipboard for this purpose, which is analogous to a single register in Vim. When copying, cutting, and pasting in Vim, there are ways to use a similarly simple editing workflow (where you don't have to think about registers), but there are also much more complex possibilities.
A register is targeted for the input/output of a command by prefixing the command with " and a lowercase letter name.
A motion in Vim is any command that moves the cursor position elsewhere. When copying, cutting, and pasting in Normal Mode, the possibilities of text selection for movement are only limited by your knowledge of motions. A few will be illustrated below.
The basic commands copy and cut operations are built on are y ('yank', for copy) and d ('delete', for cut). You'll see the similarities in the following table.
Command | : | Description |
---|---|---|
y{motion} | Copy ('yank') text indicated by the motion into the default register | |
yy | Copy the current line into the default register, linewise | |
Y | Copy the current line into the default register (synonym for yy) | |
"ayiw | Copy the word the cursor is on into register 'a' | |
20"byy | Copy twenty lines, beginning from the cursor, into register 'b' | |
d{motion} | Cut ('delete') text indicated by the motion into the default register | |
dd | Cut the current line into the default register, linewise | |
D | Cut from the cursor to end of line into the default register (NOT a synonym for dd) | |
"adiw | Cut the word the cursor is on into register 'a' | |
20"bdd | Cut twenty lines, beginning from the cursor, into register 'b' |
Note: when something is copied or cut linewise, the paste behavior shown below will place text either before or after the current line (rather than the cursor). Examples follow to clarify.
There are several ways to paste in Vim, depending on what you are trying to accomplish.
Command | : | Description |
---|---|---|
p | Paste whatever is in the default register after the cursor | |
P | Paste whatever is in the default register before the cursor | |
"ap | Paste the contents of register 'a' after the cursor | |
"cP | Paste the contents of register 'c' before the cursor |
If I have the following text:
1 This line should be second
2 This line should be first
I can do the simplest cut-and-paste by placing my cursor somewhere on line 1 and typing ddp. Here are the results:
1 This line should be first
2 This line should be second
What happened? dd 'Cuts' the first line (linewise) into the default register - which will only contain one thing at a time, like the Windows clipboard - and p pastes the line after the current one, which has just changed due to the dd command.
Here's a not-quite-as-simple example. I need to move a couple of words around. (This is contrived and unnecessary, but you can apply this principle to larger chunks of code.)
1 These words order out are of
I can repeat w to get to the 'o' at the front of 'order' (or b if I just typed it and realized my mistake).
Then "adaw to put 'order ' in register 'a'.
Then w to get to the 'a' in 'are'.
Following this, I would type "bdaw to put 'are ' into register 'b'. Now I have this displayed:
1 These words out of
To be clear, now 'order ' is in register 'a' and 'are ' is in register 'b', like two separate clipboards.
To arrange the words correctly, I type b to get to the 'o' in 'out', and then "bP to put 'are ' from register 'b' in front of 'out':
1 These words are out of
Now I type A to get to the end of the line, followed by SpaceEsc (assuming there was no space after 'of') and "ap to put 'order' where it belongs.
1 These words are out of order