Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add lines to a vim register without overwriting it

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?

like image 340
Mapad Avatar asked Dec 19 '08 08:12

Mapad


People also ask

How do I add a line to a file in Vim?

Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o ("open") also switch to insert mode so you can start typing. To add 10 empty lines below the cursor in normal mode, type 10o<Esc> or to add them above the cursor type 10O<Esc> .

How do I yank to a register in Vim?

2.2. Vim is a powerful editor. First, in the Normal mode, we move our cursor on the last word “editor” and press yaw to yank the word editor into the unnamed register “”. Next, we move the cursor to an empty line and press p without giving a register name. We'll see the yanked text “editor” is pasted.

How do I delete a register in Vim?

To clear the a register, for instance, I type q a q to set the a register to an empty string.


2 Answers

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

 "ayy "Ayy "ap 
like image 182
MarkB Avatar answered Oct 19 '22 05:10

MarkB


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.

like image 41
Rob Wells Avatar answered Oct 19 '22 04:10

Rob Wells



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!