10.Copy and Paste
60. Delete, Yank, and Put with Vim's Unnamed Register
Vim’s delete, yank, and put commands are designed to make common tasks easy by default. We’ll study a few problems that can easily be solved using Vim’s unnamed register, and then we’ll finish by looking at a task that requires a better understanding of how Vim’s registers work.
In Tip 61, Grok Vim's Registers, we’ll see that Vim has multiple registers, and we can specify which ones we want to use. But let’s start off by looking at what can be done using the unnamed register.
xp
"Transpose the next two characters"ddp
"Transpose the order of this line and its successor"yyp
duplicate the current line
Done by the unnamed register, but won't work if the unnamed register gets overwritten by diw
61. Grok Vim's Register
Addressing a Register
"{register}
specify which register we want to use
"ayw
yank the current word into registera
"ayy
yank the current line into registera
"bdd
cut the current line into registerb
"ap
paste the word from registera
"bp
paste the line from registerb
The Unnamed Register
无名寄存器
""
see :h quote_quote
The x
and d{motion}
are usually referred to as "delete" commands. This is a misnomer. It's better to think of them as "cut" commands.
The Yank Register
复制寄存器 删除、剪切寄存器
"0
see :h quote0
""p
"0p
p
Numbered register 0 contains the text from the most recent yank command
Numbered register 1 contains the text deleted by the most recent delete or change command
With each successive deletion or change, Vim shifts the previous contents of register 1 into register 2, 2 into 3, and so forth, losing the previous contents of register 9
:reg
inspect the contents of unnamed and yank registers
The Named Registers
具名寄存器
"a
-"z
see :h quote_alpha
"ad{motion}
"ay{motion}
"ap
lowercase letter, it overwrites the specified register
uppercase letter, it appends to the specified register
The Black Hole Register
黑洞寄存器
"_
see :h quote_
"_d{motion}
deletes without saving a copy
The System Clipboard and Selection Registers
系统剪贴板寄存器
"+
system clipboard, see :h quote+
"*
primary, see :h quotestar
In Windows and Mac OS X, there is no primary clipboard, so we can use the "+
and "*
registers interchangeably: they both represent the system clipboard.
The Expression Register
表达式寄存器
"=
see :h quote=
Read and test the examples in the book.
More Registers
We can set the contents of the named, unnamed, and yank registers explicitly using the delete and yank commands.
Read-only registers (:h quote.
)
"%
Name of the current file"#
Name of the alternate file".
Last inserted text":
Last Ex command"/
Last search pattern
62. Replace a Visual Selection with a Register
63. Paste from a Register
The Normal mode put command can behave differently, depending on the nature of the text that is being inserted. It can be helpful to adopt different strategies, depending on whether we're pasting a line-wise or a character-wise region of text.
Pasting Character-wise Regions
Pasting Line-wise Regions
Discussion
The p
and P
commands are great for pasting multiline regions of text. But for short sections of character-wise text, the <C-r>{register}
mapping can be more intuitive.
64. Interact with the System Clipboard
Besides Vim's built-in put commands, we can sometimes use the system paste command. However, using this can occasionally produce unexpected results when running Vim inside a terminal. We can avoid these issues by enabling the 'paste' option before using the system paste command.