如何将复制缓冲区中的一行粘贴到每行的末尾?

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

我有一个文件,其内容如下所示(超过几百行):

+incdir+/a/b/c/

file_interface.c
file_fifo.c
file_thing.c

我需要转换文件,使其看起来像这样:

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

我知道我可以将第一行复制到复制缓冲区,然后使用 Control-V 然后 P 将其粘贴到任何字符,但似乎此命令不适用于粘贴位置具体在最后每行。我该怎么做呢?

您可以将整行删除到命名缓冲区中,然后使用普通命令将缓冲区附加到每一行

"aD
:%norm $"ap

或使用默认缓冲区 cudo's to D. Ben Knoble

D
:%norm! $p

我的尝试

第一行:

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

现在让我们运行一个全局命令:

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

工作原理:

:%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

我们甚至可以创建一个包含 all 的函数:

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

然后您可以将此功能复制到您的剪贴板并且运行:

:@+

现在只需按 <F5>

删除第一行:

:1delete

删除行尾:

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

粘贴并加入:

:global/^file/put | join