how to add lines to a vim register without overwriting it

VimKeyboard Shortcuts

Vim Problem Overview


I'd like to yank a line in a register: "{register}y but without overwriting what was previously in the register. I often need to copy non-contiguous lines in a register, and I'd like to use sometimes the registers like a stack.

Example:

line1
line2
line3

I want to copy line1, by putting the cursor on it and entering "ay, then going on line3 and do "ay. Then, when I will do "ap, BOTH line1 AND line3 will be pasted.

Is this possible without plugins ? with plugins?

Vim Solutions


Solution 1 - Vim

If you want to append to a named register use it's corresponding upper case character. i.e. In your example:

"ayy
"Ayy
"ap

Solution 2 - Vim

Just to expand on MarkB's response, did you know you can also use markers to select a block of text for your yank?

Go to the first line of the block you want to yank and enter the mark command after selecting a letter as the marker, e.g.

ma  (entered in command mode, i.e. no colon)

then go to the bottom of the block you want to yank and enter the command:

:'a,.ya A

this command means take the block of text from the line containing my marker called a up to the current line and yank it into buffer a. Same rules as MarkB mentioned apply, use lowercase buffer name to overwrite the buffer. Use uppercase buffer name to append to the buffer. So in this case this will append to the contents of buffer a.

N.B. The 'a' used for your marker has nothing to do with the 'a' used to select your register. (AFAIK but YMMV)

BTW 'a (apostrophe a) refers to the line containing the marker a. `a (backquote a) refers to the character under the cursor when you entered ma.

d`a (also entered in command mode)

is useful because it will delete the text between the character marked with marker a up to the character just before the character where you cursor is currently located.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMapadView Question on Stackoverflow
Solution 1 - VimMarkBView Answer on Stackoverflow
Solution 2 - VimRob WellsView Answer on Stackoverflow