98

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mapad
  • 8,407
  • 5
  • 41
  • 40

2 Answers2

142

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

"ayy
"Ayy
"ap
MarkB
  • 5,117
  • 3
  • 22
  • 8
  • 13
    Is there any possible way to append to un-named register? (the main one) – Zaffy Mar 24 '13 at 12:28
  • 3
    @Zaffy http://stackoverflow.com/questions/18536511/vim-how-to-append-yanked-text-to-unnamed-register – PonyEars Sep 28 '13 at 08:58
  • To save people a page visit ^^, IMO it's easier to use the named register. When you type "a think, 'using register a ...' "A 'appending to register a' – Rob Kielty Sep 25 '20 at 08:28
12

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.

Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • 'a,.ya A works but when I try with the tick in front of a --> \`a it says: E492: Not an editor command: \`a,.ya A. Would have helped to just not use a letter other than a for the marker in the example so it is clear which letter is which thing :) – justin w Sep 19 '13 at 04:37
  • When would I use this when I can use visual mode to select text to be yanked? – Ralph Jun 23 '21 at 19:08
  • @Ralph It's just a question of how your brain works when you are coding. If you are used to doing things by using a GUI then just continue to use that. I find that using this technique helps me stay in "code mode" where my mind is thinking in terms of blocks of functionality that can be reused. HTHs – Rob Wells Jun 30 '21 at 12:57
  • @justinw The syntax has changed so that the delete is now "d`a" to delete from current cursor position to position of mark "a" and "d'a" to delete from the current cursor to the line containing mark "a". HTH – Rob Wells Jun 30 '21 at 17:10
  • @RobWells I don't mean to tell you that you're doing something wrong, just want to reduce total keystrokes. AFAIK, using visual mode uses less keystrokes than marks (and AFAIK is just as "reusable" as marks, for e.g. recording mode). For example, to yank a paragraph, assuming you've navigated to the start, simply type `v}"ry` (`v` for visual mode, `}` for end of paragraph, `"` register noun, `r` target register, and `y` for yank). Perhaps you can share have an example of non-contiguous lines where marks would be superior to visual mode? Cheers. – Ralph Jul 07 '21 at 17:15