Vim, like other editors, has cut, copy, and paste operations, but in Vim these operations have different names: delete delete, copy yank, and paste put.

When copying, cutting and other operations are carried out in general editors, the clipboard of the operating system, Clipboard, will be used to temporarily store text. However, the “clipboard” of Vim and the clipboard of the system do not communicate with each other. Instead, Vim uses the built-in “register” to save text.

Cut (delete), copy, and paste operations

Cut (Delete) Delete

In other editors, clipping generally means that when you delete a piece of text, you save it to the clipboard for pasting somewhere else. In Vim, there is no clear line between cutting and deleting, because in Vim, all deleted text is put into registers so that we can paste it the next time we want to use it.

We’ll talk more about which registers we put in and how to operate them later. Let’s take a look at the Vim commands related to cut/delete:

The command use
c{motion} Delete the character and enter insert mode
cc Delete the entire row and enter insert mode (===S)
C Delete from the cursor to the end of the line and enter insert mode
d{motion} Delete the character and enter insert mode
dd Delete the entire line
D Clear all characters of the current line (without deleting rows)
s Delete the character under the cursor and enter insert mode
S Delete the entire row and enter insert mode (===cc)
x Delete the character under the cursor
X Deletes the character before the cursor

In Vim, C stands for change and D stands for delete.

Simply put, C and S related commands go into insert mode when characters are deleted, while D and X remain in normal mode when characters are deleted. C and D can delete segments of characters, while S and X delete only one character.

The {motion} in C {motion} and D {motion} stands for action commands, and you can read more about action commands in this article. For example: ciw – deletes the current word and goes into insert mode, dt” – deletes to the front of “, di] – deletes the included content in [], and so on.

In addition to action commands, you can use visual mode to select text and delete it with C or D.

In addition, there are convenient cc and dd commands that can be used to delete entire lines directly.

Copy the Yank

In Vim, yank stands for copy. YANK: Pull up, pull out, pull up, pull up Using yank for replication is mainly because C was already occupied by change at the time Vim was developed, so only the spare y was used.

The y command is used in a similar way to the c and d command. Y {motion} can copy the corresponding text to each other, or select the text in visual mode and copy it with y.

Like cc and dd, yy can copy an entire line.

Paste the Put

Put the text you just deleted/cut/copied after the cursor. If it is an entire line of text, place it on the line below the cursor. And P is in front/on top.

In visual mode, p replaces the selected text while pasting.

In addition, there are the gp and gp commands, which, unlike p p, move the cursor to the end of the text instead of the beginning after pasted.

Register Register

With this in mind, we’ve learned a few things about deleting/cutting/copying, which can be used in many everyday situations.

But imagine this scenario:

I want to copy text a in one place and replace text b in the other place. I copied the text A with Y, then moved the cursor over the text B, selected or used the action command paired with D to delete the text B, then pressed P.

Something strange happens. Instead of text A, we get text B pasted out. Oops, I lost the copy!

Of course, you might say, after copying text A, select text B without deleting it, and replace it with text P. That’s true, and that’s what I prefer to do, it doesn’t copy and delete, it replaces in two steps.

To understand why the text b is put, you need to understand the concept of registers.

What is a register

Generally speaking, there is only one clipboard for the operating system, and the later clipboard overwrites the previous clipboard. As the new content is saved to the clipboard, the old content is deleted.

Vim registers are different. There are many registers. You can specify which registers to use by “{reg} “. Where, “is used to tell Vim to do register related operations, and {reg} stands for the name of the register.

The names of registers can be [a-z0+”*#%.:/], etc., among which [a-z] has a total of 26 registers available for us to use at will, the other ones are Vim’s built-in special registers.

Use register

Use the “{reg} plus command to specify which registers we want to use, such as “ep-paste the contents of register e,” FDD – delete the current line and put it in register f, etc.

Nameless register (default)""

When no register is specified to use, Vim uses the nameless register “”.

As mentioned earlier, the c, d, s, x, and y commands all put the corresponding text into registers, but when one of [a-z] is not declared, the text is put into the default register “”. And when we paste with p, we also read “”.

This is bad, I lost the copy! The reason why. When we perform the delete operation, the deleted text replaces the content in “”, which is the text a that we just copied.

So, as we mentioned earlier, there is no clear line between cutting and deleting in Vim, and deleted text is also placed by default in the nameless register “”, which may overwrite what we just copied, so be careful when using it.

Of course, we can also take advantage of this feature of Vim. Such as:

XP can delete the character below the cursor and put it behind the cursor, swapping the position of the two characters below the cursor.

The DDP can delete the current line and paste it under the cursor, swapping the two lines below the cursor.

Named register"[a-z]

Vim provides a complete set of 26 English letters for us to name registers, which means we can cut/copy 26 pieces of text at the same time.

Naming a register with an uppercase letter appends the corresponding contents to the register named with the corresponding lowercase letter.

Copy special register"0

When copying using y, the copied text is stored not only in the nameless register “”, but also in “0 “, and c, d, s, and x do not override this register. This register will be overwritten only the next time we copy.

So, you can also use this register to avoid the terrible fact that I lost the copy! The problem.

System clipboard register"+

The registers used by Vim are isolated from the operating system clipboard, but this isolation is not complete. The “+ register “can be used to interact with the system clipboard. For example, we can use “+p” to paste the contents of the system clipboard, and we can use “+y “to save the contents to the system clipboard. This means that we can copy text directly in Vim and paste it into other programs.


In addition, Vim provides a number of other registers for our use, which are also known as read-only registers:

register content
"% Current file name
"# Rotating file name
". Text that was inserted last time
": The last command executed
"/ The pattern you looked up last time