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

问题描述 投票:1回答:2

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

+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然后将其粘贴到任何字符,但是似乎该命令不适用于粘贴位置,特别是在每行的末尾。我该怎么办呢?

vim paste
2个回答
3
投票

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

"aD
:%norm $"ap

或使用默认缓冲区D。Ben Knoble的伪装

D
:%norm! $p

1
投票

我的尝试

在第一行:

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

我们甚至可以创建一个包含所有功能的函数:

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

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

:@+

现在只需按<F5>

© www.soinside.com 2019 - 2024. All rights reserved.