Paste an entire cut line at the end of another line in vim
Emily Wong
I cut a line with dd and then I want to paste the entire line at the end of another line. But the problem is that because dd cuts the entire line, it's including the newline character probably too.
And so when I paste via p, it automatically puts it on the next line. And if I do Shift + p, it puts it before the line I'm trying to paste on.
So is there a way to paste the entire line I cut at the end of a line?
4 Answers
To cut a line without new line, you can press d$ or D from cursor location.
To paste it at the end of another line, you can go to its end and just press p. In case, if you are not able to paste the line as you expected, you can go to the end of line where you wanted to paste. Then, press i and then press <C-R> (Control-R) and " (double quotes). Your recently cut or copied text will be pasted.
After you pasted your line after your current one via p just press another j in your previous line to join the two lines, thus eliminating your unwanted line feed...
The best solution I can think up for now is:
A<C-r>+It means: enable insert mode at the end of line and paste the content of the register +.
It will also paste newline. dd is simply operating on full lines (short for 1dd).
Indeed, often the use case is as follows: You want to delete a complete line (including the newline, so d$ / D don't work, as the leave behind an empty line), but paste this at the end or inside some existing line, without creating a new one.
My UnconditionalPaste plugin provides several mappings that automatically convert the register contents to the desired use case. Your example would be dd, move to target line, $gcp. gcp is the mapping for characterwise paste.