How to paste a line from the copy buffer to the end of each line?

Viewed 123

I have a file whose content looks like this (over a few hundred lines):

+incdir+/a/b/c/

file_interface.c
file_fifo.c
file_thing.c

I need to transform the file so that it looks like this:

file_interface.c +incdir+/a/b/c/
file_fifo.c +incdir+/a/b/c/
file_thing.c +incdir+/a/b/c/

I know that I can copy the first line to the copy buffer then paste it to any character using the Control-V then P, but it seems that this command does not work with the paste location is specifically in the end of each line. How could I do that instead?

3 Answers

You can delete the entire line into a named buffer and use a normal command to append the buffer to each line

"aD
:%norm $"ap

or using the default buffer cudo's to D. Ben Knoble

D
:%norm! $p

My attempt

On the first line:

gg0yg_ ................. this will copy the first line without carriage return

Now let's run a global command:

:%g/^file_/exec "normal! A \<c-r>0"

How it works:

:%g/^file_/exec ................. over the whole file
" ............................... using double quotes we use keystroke notation
normal! ......................... normal command
A ............................... start insert at the end of line and add one space
\<c-r>0 ......................... inserts the copy register

:1,2d ........................... delete the first two lines

We can even create a function with all on it:

fun! CopyToEnd()
    :normal gg0"+yg_
    :1,2d
    :g/file_/exe "normal! A \<c-r>+"
endfun
nnoremap <F5> :call CopyToEnd()<cr>

Then you can copy this funtion to your clipboard and run:

:@+

Now just press <F5>

Delete the first line:

:1delete

Remove the end-of-line:

:let @" = substitute(@", '\n', '', '')

Paste and join:

:global/^file/put | join
Related