Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to the clipboard

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?

like image 849
Reman Avatar asked Jun 10 '11 08:06

Reman


2 Answers

use:

let @+ = @+ . my_expression

or shorter:

let @+ .= my_expression

Reference: :help :let.=

like image 81
Benoit Avatar answered Sep 28 '22 07:09

Benoit


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.


Copying with a register (cursor at [T]):

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).

  • Type p (* see below) to immediately paste it from the default register.
  • Type "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.

Appending to a register:

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 register f immediately after you yanked into f? Because your default register holds a pointer to the last selection, and apparently doesn't simply hold what you added to f, but pulls everything that's in f 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 typed yyp with the default clipboard."

But if you were now to yy a new line into the default register, you can hit "f to select the f register and then p to paste that previous value.

like image 38
ruffin Avatar answered Sep 28 '22 07:09

ruffin