Vim, like any other editor, has cut, copy, and paste operations, but these operations are called different names in Vim: Delete Delete, copy yank, and paste PUT.

General editors use the clipboard of the operating system to temporarily store text when copying and cutting, but Vim’s “clipboard” does not communicate with the system’s clipboard. Vim uses the built-in “register Register” to store text.

Cut (delete), copy, and paste operations

Cut (Delete) Delete

In other editors, clipping generally means deleting a piece of text while saving the text to the clipboard so that it can be pasted elsewhere. In Vim, there is no clear line between cutting and deleting because all deleted text is placed in a register so that we can paste it the next time we want to use it.

Which register is put in, and how to operate the register, we will talk about later. Let’s take a look at Vim’s cut/delete commands:

The command use
c{motion} Delete characters and enter insert mode
cc Delete the entire line and enter insert mode (===S)
C Delete from the cursor to the end of the line and enter insert mode
d{motion} Delete characters and enter insert mode
dd Delete the entire line
D Clears all characters of the current line (no lines are deleted)
s Deletes the character under the cursor and enters insert mode
S Delete the entire line and enter insert mode (===cc)
x Deletes the character under the cursor
X Deletes the character before the cursor

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

To put it simply, c and s-related commands go into insert mode after deleting characters, while D and x stay in normal mode after deleting characters. C and D can be deleted as segments, while S and X delete only one character.

{motion} in C {motion} and D {motion} represent action commands. See this article for more information on action commands. For example: ciw – deletes the current word and goes into insert mode, dt” – deletes to the front of “, di] – deletes the contents contained in [], and so on.

In addition to action commands, text can also be selected in visual mode and deleted 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 originally means to pull up, pull out. The reason why yank stands for copy is that c was already occupied by change at the time of Vim’s development, so y was used instead.

The y command is similar to c and D. You can use y{motion} to copy the corresponding text or select the text in visual mode and use Y to copy the text.

Similar to CC and DD, YY can copy entire lines.

Paste the Put

You can put the text you just deleted/cut/copied after the cursor. If it is a full line of text, the line below the cursor. And P is in the front/top.

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

There are also gp and gp commands, which, unlike P and P, move the cursor to the end of the text instead of the beginning after pasting.

Register Register

Now that we’ve learned about delete/cut/copy, we can handle many everyday scenarios.

But consider this scenario:

I want to copy text A in one place and replace text B in the other. I copied text A with y, then moved the cursor over text B, selected or deleted text B with action command d, and then pressed P.

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

Of course, you might say, after copying text A, select text B and don’t delete it, just replace it with p. That’s true, and that’s what I prefer, it doesn’t copy and delete, it does the replacement in two steps.

To understand why b is put, you need to understand the concept of a register.

What is a register

Generally speaking, the operating system has only one clipboard, and the later one overrides the first one. As new content is saved to the clipboard, old content is also deleted.

The Vim register is not quite the same, there are many registers, you can use “{reg} way to indicate the register to use. Where, “is used to tell Vim to do something about the register, and {reg} stands for the register name.

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

Use register

Use the “{reg} plus command to specify which register we want to use, such as “ep – paste the contents of register E, “FDD – delete the current line and put it in register F, and so on.

Nameless register (default)""

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

As mentioned earlier, the c, D, S, x, and Y commands all put the corresponding text in the register, but if one of the [a-z] commands is not declared, the text is put in the default register “”. And when we paste with p, we also read the contents of “”.

That’s bad. I lost the copy! The reason why. When we delete, the deleted text replaces the text in “”, which is the text a we just copied.

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

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

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

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

Named register"[a-z]

Vim provides a full 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 adds the corresponding contents to the register with the corresponding lowercase letter name.

Copy special register"0

When copying with y, the copied text is stored not only in the nameless register “”, but also in the “0 “register, which is not overwritten by c, D, S, or X. This register will only be overwritten the next time we copy.

So, you can also use this register to get around oops, I lost the copy! The problem.

System Clipboard register"+

As mentioned earlier, the registers Vim uses are isolated from the operating system clipboard, but this isolation is not absolute. 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 “+ 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 some other registers for us to use, which are also called read-only registers:

register content
"% Current file name
"# Rotation file name
". The last text inserted
": The last command executed
"/ The pattern from the last lookup