I know how to copy to the clipboard but how can I append to it?
I use this in my code:
let @+ = my_expression
but that overwrites the clipboard.
I know that I can use the registers a-z to append to:
let @B = my_expression
which appends to register b, but what do I when I want to append to the clipboard?
use:
let @+ = @+ . my_expression
or shorter:
let @+ .= my_expression
Reference: :help :let.=
If you're not macro-ing, it's probably worth checking out registers as well. :help registers
was mind-blowing.
In an oversimplified nutshell, there are 26 additional "customizable clipboards", called registers, where you can store text, starting with a
and going through z
.
You add text to a register in command mode by hitting "
, naming the register (say f
), and then typing the "motion" you want to select text.
NOTE: We're using the named f
register here b/c it's probably under your left index finger. That is, f
is picked just b/c it's handy. You could replace f
with a
or u
or z
or whatever throughout if you wanted.
Initial File State
This is my first line.
[T]his is my second line.
This is my third line.
Type "fyy
in command mode to fill the register with one line (yy
).
p
(* see below) to immediately paste it from the default register."f
to pick the f
register and then p
to paste from the f
register directly. Right now f
and default as the same.
So the result of typing "fyyp
is exactly the same as having typed yyp
with the default clipboard.
Result
This is my first line.
This is my second line.
[T]his is my second line.
This is my third line.
Use the capital letter to append to your existing register.
In the above example after pasting, press j
to go down a line and then "Fyy
. Then type p
to paste. You've appended "This is my third line." to f
's contents.
Result
This is my first line.
This is my second line.
This is my second line.
This is my third line.
This is my second line.
[T]his is my third line.
(Using a lower case f
would have cleared out f
's contents and ended up with it only holding "This is my third line.")
Why does
p
paste what's in registerf
immediately after you yanked intof
? Because your default register holds a pointer to the last selection, and apparently doesn't simply hold what you added tof
, but pulls everything that's inf
when you append. It might be more expository to say, in the first case, "the result of typing"fyy"fp
is exactly the same as having typedyyp
with the default clipboard."But if you were now to
yy
a new line into the default register, you can hit"f
to select thef
register and thenp
to paste that previous value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With