我想合并Vim中的两个行块,即取行k到l并将它们附加到行m到n。如果您喜欢伪代码解释:[line[k+ I] + line[m+ I] for I in range(min(l-k, n-m)+1)]。

例如,

abc
def
...

123
45
...

应该成为

abc123
def45

有没有一种很好的方法来做到这一点,而无需手动逐行复制和粘贴?


当前回答

正如在其他地方提到的,块选择是可行的方法。但是你也可以使用以下的任何变体:

:!5 .尾部-n - 6% |粘贴-d '\0' % - |头部-n

此方法依赖于UNIX命令行。创建粘贴实用程序是为了处理这种行合并。

PASTE(1)                  BSD General Commands Manual                 PASTE(1)

NAME
     paste -- merge corresponding or subsequent lines of files

SYNOPSIS
     paste [-s] [-d list] file ...

DESCRIPTION
     The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character,
     and writes the resulting lines to standard output.  If end-of-file is reached on an input file while other input files still contain data, the file is treated as if
     it were an endless source of empty lines.

其他回答

我觉得不要弄得太复杂。 我会设置virtualedit (:设置virtualedit =) 选择123块和下面所有的。 把它放在第一列之后:

abc    123
def    45
...    ...

并将其中的多重空格移除为1个空格:

:%s/\s\{2,}/ /g

正如在其他地方提到的,块选择是可行的方法。但是你也可以使用以下的任何变体:

:!5 .尾部-n - 6% |粘贴-d '\0' % - |头部-n

此方法依赖于UNIX命令行。创建粘贴实用程序是为了处理这种行合并。

PASTE(1)                  BSD General Commands Manual                 PASTE(1)

NAME
     paste -- merge corresponding or subsequent lines of files

SYNOPSIS
     paste [-s] [-d list] file ...

DESCRIPTION
     The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character,
     and writes the resulting lines to standard output.  If end-of-file is reached on an input file while other input files still contain data, the file is treated as if
     it were an endless source of empty lines.

样本数据与rampion的相同。

:1,4s/$/\=getline(line('.')+4)/ | 5,8d

我将使用复杂重复:)

鉴于这种:

aaa
bbb
ccc

AAA
BBB
CCC

将光标放在第一行,按下以下命令:

qa}jdd''pkJxjq

然后按下@a(随后可以使用@@),需要多少次就按多少次。

你应该得到:

aaaAAA
bbbBBB
cccCCC

(加上换行符。)

解释:

Qa开始记录一个复杂的重复 }跳转到下一个空行 JDD删除下一行 回到上次跳跃前的位置 P将删除的行粘贴到当前行下面 kJ将当前行附加到前一行的末尾 x删除J在合并行之间添加的空格;如果你需要空间,你可以省略这个 看下一行 Q结束复杂的重复记录

在此之后,您可以使用@a运行存储在a中的复杂重复,然后您可以使用@@重新运行上次运行的复杂重复。

有很多方法可以做到这一点。我将使用以下两种方法中的任何一种来合并两个文本块。

假设第一个块在第1行,第二个块从第10行开始,游标的初始位置在第1行。

(\n表示按回车键。)

1. abc
   def
   ghi        

10. 123
    456
    789

与宏使用命令:复制,粘贴和连接。

y \ npkJjq2@a10G3dd qaqqa: + 9

使用宏命令在第n行号移动一行并连接。

qcqqc: 10 m。\ nkJjq2@c